前言
Fluent-MyBatis 文档写的不尽人意,踩了不少坑,特此记录一下。
环境准备
- Maven 或 Gralde 构建工具
- 任意代码编辑器或集成开发环境(IntelliJ IDEA, Visual Studio Code, Eclipse)
- JDK 1.8 及以上
- 具备互联网网络连接
引入依赖
仅编译时依赖
- com.github.atool:fluent-mybatis-processor:1.9.10
- org.projectlombok:lombok:1.18.36
实现依赖
- org.springframework.boot:spring-boot-starter:2.7.18
- org.springframework.boot:spring-boot-starter-jdbc:2.7.18
- com.github.atool:fluent-mybatis:1.9.10
测试依赖
- org.springframework.boot:spring-boot-starter-test:2.7.18
运行时依赖
- com.mysql:mysql-connector-j:9.1.0
例:Gradle 配置文件 build.gradle
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
| dependencies { implementation 'org.springframework.boot:spring-boot-starter:2.7.18' testImplementation 'org.springframework.boot:spring-boot-starter-test:2.7.18' implementation 'org.springframework.boot:spring-boot-starter-jdbc:2.7.18' implementation 'com.github.atool:fluent-mybatis:1.9.10' runtimeOnly 'com.mysql:mysql-connector-j:9.1.0' compileOnly 'com.github.atool:fluent-mybatis-processor:1.9.10' annotationProcessor 'com.github.atool:fluent-mybatis-processor:1.9.10' compileOnly 'org.projectlombok:lombok:1.18.36' annotationProcessor 'org.projectlombok:lombok:1.18.36' }
|
快速演示
创建实体类型
创建 entity 包,然后创建一个名为 User 的类。
文件名:User.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
| package me.kanchitsu.entity;
import cn.org.atool.fluent.mybatis.annotation.FluentMybatis; import cn.org.atool.fluent.mybatis.annotation.TableField; import cn.org.atool.fluent.mybatis.base.IEntity; import cn.org.atool.fluent.mybatis.base.RichEntity; import lombok.Data; import lombok.EqualsAndHashCode;
@EqualsAndHashCode(callSuper = true) @FluentMybatis @Data public class User extends RichEntity { private String id; private String username; private String password; @Override public Class<? extends IEntity> entityClass() { return User.class; } }
|
创建启动类
Fluent MyBatis 需要编译一次才能自动生成相关的辅助类和接口,这是和其他工具(如 lombok)的一个显著不同的地方。
生成源代码的步骤不依赖于 Spring Boot。可以使用任意一个类来生成
文件名:Main.java
1 2 3 4 5 6 7
| package me.kanchitsu;
import org.mybatis.spring.annotation.MapperScan;
@MapperScan("me.kanchitsu.mapper") public class Main { }
|
注意需要使用@MapperScan()
指定源代码生成位置。
如果使用 Spring Boot 并启用了相关编译插件,需要在启动类上添加 Spring Boot 启动类注解@SpringBootAppliaction
,并声明main函数,否则会编译失败。
生成相关辅助类和接口
使用你的集成开发环境或构建工具执行编译操作
以 Gradle 为例:
此时在构建输出目录generated/sources/annotationProcessor
中,自动生成了相关辅助类和接口的Java
源文件。不需要将这些构建产物复制到源代码目录中。
创建数据源配置
使用 Spring Boot JDBC 注入数据源配置。
文件名:application.yml
1 2 3 4 5 6
| spring: datasource: driver-class-name: com.mysql.cj.jdbc.Driver username: <数据库用户名> password: <数据库密码> url: <数据库连接 JDBC URL>
|
创建数据表
此步略过。
执行测试
使用 Spring Boot Test 运行测试
文件名:TestDemo.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
| package me.kanchitsu.test;
import me.kanchitsu.mapper.UserMapper; import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.test.context.ContextConfiguration;
@SpringBootTest @ContextConfiguration(classes = Config.class) public class TestDemo { @Autowired private UserMapper userMapper;
@Test void test() { User u = new User(); u.setId(UUID.randomUUID().toString()); u.setUsername("kanchitsu"); u.setPassword("test"); userMapper.insert(u); } }
|
文件名:Config.Java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
| package me.kanchitsu.test;
import cn.org.atool.fluent.mybatis.spring.MapperFactory; import org.mybatis.spring.SqlSessionFactoryBean; import org.mybatis.spring.annotation.MapperScan; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.Configuration;
import javax.sql.DataSource;
@ComponentScan(basePackages = "me.kanchitsu") @MapperScan("me.kanchitsu.mapper") @Configuration public class Config { @Bean public SqlSessionFactoryBean sqlSessionFactoryBean(DataSource dataSource) { SqlSessionFactoryBean bean = new SqlSessionFactoryBean(); bean.setDataSource(dataSource); return bean; }
@Bean public MapperFactory mapperFactory() { return new MapperFactory(); } }
|
Fluent-MyBatis 基于 MyBatis,并且这里使用了 Spring Boot,需要为 MyBatis 指定 SqlSessionFactoryBean。
执行测试后,观察数据表,成功插入一条新数据。
要点总结
- 新创建的实体类文件需要编译一次生成相关辅助类和接口。
- 需要在启动类上使用
@MapperScan
注解指定生成位置
- 生成的源代码文件不需要复制到源代码目录中