mybatis-flex基本使用
- 1.测试sql
- 2.导入坐标
- 3.框架搭建
- 1.pojo层
- 2.mapper层
- 3.service层
- 4.controller层
- 5.启动类
- 6.配置类
- 7.EmpMapper.xml
- 4.启动测试
本片文章在springBoot3,jdk21下测试通过
注意官方网站为:https://mybatis-flex.com/
请点击:直达网站
1.测试sql
SET NAMES utf8mb4;
SET FOREIGN_KEY_CHECKS = 0;
-- 员工表
DROP TABLE IF EXISTS `emp`;
CREATE TABLE `emp` (
`id` bigint NOT NULL,
`ename` varchar(20) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL,
`age` int NULL DEFAULT NULL,
`dept_id` varchar(20) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL,
PRIMARY KEY (`id`) USING BTREE
) ENGINE = InnoDB CHARACTER SET = utf8mb4 COLLATE = utf8mb4_general_ci ROW_FORMAT = DYNAMIC;
INSERT INTO `emp` VALUES (1, '乔峰', 10, '1001');
INSERT INTO `emp` VALUES (2, '段誉', 21, '1001');
INSERT INTO `emp` VALUES (3, '虚竹', 23, '1001');
INSERT INTO `emp` VALUES (4, '阿紫', 18, '1001');
INSERT INTO `emp` VALUES (5, '扫地僧', 85, '1002');
INSERT INTO `emp` VALUES (6, '李秋水', 33, '1002');
INSERT INTO `emp` VALUES (7, '鸠摩智', 50, '1002');
INSERT INTO `emp` VALUES (8, '天山童姥', 60, '1003');
INSERT INTO `emp` VALUES (9, '慕容博', 58, '1003');
INSERT INTO `emp` VALUES (10, '丁春秋', 71, '1005');
SET FOREIGN_KEY_CHECKS = 1;
-- 部门表
DROP TABLE IF EXISTS `dept`;
CREATE TABLE `dept` (
`dept_no` varchar(20) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NOT NULL,
`name` varchar(20) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL
) ENGINE = InnoDB CHARACTER SET = utf8mb4 COLLATE = utf8mb4_general_ci ROW_FORMAT = DYNAMIC;
INSERT INTO `dept` VALUES ('1001', '研发部');
INSERT INTO `dept` VALUES ('1002', '销售部');
INSERT INTO `dept` VALUES ('1003', '财务部');
INSERT INTO `dept` VALUES ('1004', '人事部');
SET FOREIGN_KEY_CHECKS = 1;
2.导入坐标
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.andy</groupId>
<artifactId>myBatis-Flex_boot3</artifactId>
<version>1.0-SNAPSHOT</version>
<!-- 父坐标 -->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>3.2.1</version>
<relativePath/>
</parent>
<properties>
<maven.compiler.source>21</maven.compiler.source>
<maven.compiler.target>21</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<!-- spring数据坐标 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- mybatis-flex -->
<dependency>
<groupId>com.mybatis-flex</groupId>
<artifactId>mybatis-flex-spring-boot-starter</artifactId>
<version>1.7.6</version>
<exclusions>
<exclusion>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
</exclusion>
</exclusions>
</dependency>
<!-- mybatis与spring合成 -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>3.0.3</version>
</dependency>
<dependency>
<groupId>com.mybatis-flex</groupId>
<artifactId>mybatis-flex-processor</artifactId>
<version>1.7.6</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>com.zaxxer</groupId>
<artifactId>HikariCP</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.33</version>
</dependency>
</dependencies>
</project>
3.框架搭建
这里只搭建emp表的,dept表的框架各位博主可以自行尝试搭建
1.pojo层
1.flex同样也支持AR模式(Active Record),使用 Active Record 功能时,项目中必须注入对应实体类的 BaseMapper 对象。用过mybatis-plus的博主都知道这一点
2.使用方式与mybatis-plus相同,只需让 Entity 类继承 Model 即可
3.注解
- @Table:与mybatis-plus的@TableName作用相同
- @Id(keyType = KeyType.Auto):与mybatis-plus的 @TableId(value = “id”, type = IdType.AUTO)相同
- @Column 用来对字段进行更多的配置源码如下:
public @interface Column {
/**
* 字段名称
*/
String value() default "";
/**
* 是否忽略该字段,可能只是业务字段,而非数据库对应字段
*/
boolean ignore() default false;
/**
* insert 的时候默认值,这个值会直接被拼接到 sql 而不通过参数设置
*/
String onInsertValue() default "";
/**
* update 的时候自动赋值,这个值会直接被拼接到 sql 而不通过参数设置
*/
String onUpdateValue() default "";
/**
* 是否是大字段,大字段 APT 不会生成到 DEFAULT_COLUMNS 里
*/
boolean isLarge() default false;
/**
* 是否是逻辑删除字段,一张表中只能存在 1 一个逻辑删除字段
* 逻辑删除的字段,被删除时,会设置为 1,正常状态为 0
*/
boolean isLogicDelete() default false;
/**
* 是否为乐观锁字段,若是乐观锁字段的话,数据更新的时候会去检测当前版本号,若更新成功的话会设置当前版本号 +1
* 只能用于数值的字段
*/
boolean version() default false;
/**
* 配置的 jdbcType
*/
JdbcType jdbcType() default JdbcType.UNDEFINED;
/**
* 自定义 TypeHandler
*/
Class<? extends TypeHandler> typeHandler() default UnknownTypeHandler.class;
}
4.pojo层代码
@Table(value = "emp")
public class Emp extends Model<Emp> {
@Id(keyType = KeyType.Auto)
private Long id;
private String ename;
private Integer age;
private String deptId;
// getter/setter/toString...
2.mapper层
这里和mybatis-plus的mapper层一样
@Mapper
public interface EmpMapper extends BaseMapper<Emp> {
}
3.service层
这里和mybatis-plus一样
1.service层接口
public interface EmpService extends IService<Emp> {
}
2.实现类接口
@Service
@Transactional
public class EmpServiceImpl extends ServiceImpl<EmpMapper,Emp> implements EmpService {
}
4.controller层
这里和mybatis-plus一样
@RestController
@RequestMapping("/emp")
public class EmpController {
@Resource
private EmpService empService;
@GetMapping("/findById")
public Emp findById(@RequestParam("id") Long id){
return empService.getById(id);
}
}
5.启动类
@SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
6.配置类
#设置数据源
spring:
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://127.0.0.1:3306/study_test?characterEncoding=utf-8&allowMultiQueries=true&zeroDateTimeBehavior=convertToNull&useSSL=false
username: root
password: root
7.EmpMapper.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.andy.mapper.EmpMapper">
</mapper>
4.启动测试
测试连接:http://localhost:8080/emp/findById?id=1