注意官方网站为:https://mybatis-flex.com/
请点击:直达网站
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;
<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.0modelVersion>
<groupId>com.andygroupId>
<artifactId>myBatis-Flex_boot3artifactId>
<version>1.0-SNAPSHOTversion>
<parent>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-parentartifactId>
<version>3.2.1version>
<relativePath/>
parent>
<properties>
<maven.compiler.source>21maven.compiler.source>
<maven.compiler.target>21maven.compiler.target>
<project.build.sourceEncoding>UTF-8project.build.sourceEncoding>
properties>
<dependencies>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-webartifactId>
dependency>
<dependency>
<groupId>com.mybatis-flexgroupId>
<artifactId>mybatis-flex-spring-boot-starterartifactId>
<version>1.7.6version>
<exclusions>
<exclusion>
<groupId>org.mybatisgroupId>
<artifactId>mybatis-springartifactId>
exclusion>
exclusions>
dependency>
<dependency>
<groupId>org.mybatisgroupId>
<artifactId>mybatis-springartifactId>
<version>3.0.3version>
dependency>
<dependency>
<groupId>com.mybatis-flexgroupId>
<artifactId>mybatis-flex-processorartifactId>
<version>1.7.6version>
<scope>providedscope>
dependency>
<dependency>
<groupId>com.zaxxergroupId>
<artifactId>HikariCPartifactId>
dependency>
<dependency>
<groupId>mysqlgroupId>
<artifactId>mysql-connector-javaartifactId>
<version>8.0.33version>
dependency>
dependencies>
project>
这里只搭建emp表的,dept表的框架各位博主可以自行尝试搭建
1.flex同样也支持AR模式(Active Record),使用 Active Record 功能时,项目中必须注入对应实体类的 BaseMapper 对象。用过mybatis-plus的博主都知道这一点
2.使用方式与mybatis-plus相同,只需让 Entity 类继承 Model 即可
3.注解
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...
这里和mybatis-plus的mapper层一样
@Mapper
public interface EmpMapper extends BaseMapper<Emp> {
}
这里和mybatis-plus一样
1.service层接口
public interface EmpService extends IService<Emp> {
}
2.实现类接口
@Service
@Transactional
public class EmpServiceImpl extends ServiceImpl<EmpMapper,Emp> implements EmpService {
}
这里和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);
}
}
@SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
#设置数据源
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
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>
测试连接:http://localhost:8080/emp/findById?id=1