共计 3459 个字符,预计需要花费 9 分钟才能阅读完成。
总是喜爱去关注更多的新框架,发现了一个基本上不必写 mapper 和 xml 的框架。让咱们来钻研一下这个框架吧。
1. 新建 Spring Boot 我的项目
1.1 pom.xml 配置
<properties> | |
<java.version>1.8</java.version> | |
<fluent-mybatis.version>1.6.13</fluent-mybatis.version> | |
</properties> | |
<dependencies> | |
<dependency> | |
<groupId>org.springframework.boot</groupId> | |
<artifactId>spring-boot-starter-web</artifactId> | |
</dependency> | |
<dependency> | |
<groupId>mysql</groupId> | |
<artifactId>mysql-connector-java</artifactId> | |
<scope>runtime</scope> | |
</dependency> | |
<dependency> | |
<groupId>org.projectlombok</groupId> | |
<artifactId>lombok</artifactId> | |
<optional>true</optional> | |
</dependency> | |
<dependency> | |
<groupId>org.springframework.boot</groupId> | |
<artifactId>spring-boot-starter-test</artifactId> | |
<scope>test</scope> | |
</dependency> | |
<!-- 引入 fluent-mybatis 运行依赖包, scope 为 compile --> | |
<dependency> | |
<groupId>com.github.atool</groupId> | |
<artifactId>fluent-mybatis</artifactId> | |
<version>${fluent-mybatis.version}</version> | |
</dependency> | |
<!-- 引入 fluent-mybatis-processor, scope 设置为 provider 编译须要,运行时不须要 --> | |
<dependency> | |
<groupId>com.github.atool</groupId> | |
<artifactId>fluent-mybatis-processor</artifactId> | |
<version>${fluent-mybatis.version}</version> | |
</dependency> | |
<dependency> | |
<groupId>org.mybatis.spring.boot</groupId> | |
<artifactId>mybatis-spring-boot-starter</artifactId> | |
<version>2.2.0</version> | |
</dependency> | |
</dependencies> |
咱们这里引入了 fluent-mybatis-processor 就是想主动生成代码,尽量减少咱们本人写代码。
1.2 application.yml 配置
server: | |
port: 8080 # 端口号 | |
spring: | |
datasource: # 数据库参数配置 | |
driver-class-name: com.mysql.cj.jdbc.Driver | |
url: jdbc:mysql://IP:3306/ 数据库名称?useUnicode=true&characterEncoding=utf8 | |
username: 用户名 | |
password: 明码 |
1.3 EntityGeneratorTests.java 主动代码生成
这个我尝试过在非 Test 外面用 main 外面运行,后果总是失败,有能够运行的请通知我哈!
@SpringBootTest | |
public class EntityGeneratorTests { | |
// 数据源 url | |
static final String url = "jdbc:mysql://IP:3306/ 数据库名称?useUnicode=true&characterEncoding=utf8"; | |
// 数据库用户名 | |
static final String username = "用户名"; | |
// 数据库明码 | |
static final String password = "明码"; | |
@Test | |
public void generate() { | |
// 援用配置类,build 办法容许有多个配置类 | |
FileGenerator.build(Empty.class); | |
} | |
@Tables( | |
// 设置数据库连贯信息 | |
url = url, username = username, password = password, | |
// 设置 entity 类生成 src 目录, 绝对于 user.dir | |
srcDir = "src/main/java", | |
// 设置 entity 类的 package 值 | |
basePack = "xyz.zhouzhaodong.fluentmybatis", | |
// 设置 dao 接口和实现的 src 目录, 绝对于 user.dir | |
daoDir = "src/main/java", | |
// 设置哪些表要生成 Entity 文件 | |
tables = {@Table(value = {"user"})} | |
) | |
static class Empty {// 类名轻易取, 只是配置定义的一个载体} | |
} |
运行后生成如下三个文件:
1.4 生成代码后编译会在 target 目录生成代码
运行后生成的代码如下:
1.5 启动类配置 @MapperScan
@MapperScan 地址为下面 target 目录下的 mapper
可参考我的:
1.6 新建 UserController 测试
能够参考官网文档进行钻研,我这里只是简略的进行逻辑的实现。
文档地址为:
https://gitee.com/fluent-myba…
@RestController | |
@RequestMapping("/user") | |
public class UserController { | |
@Resource | |
UserDao userDao; | |
@Resource | |
UserMapper userMapper; | |
/** | |
* 依据 ID 查问数据 1 | |
* @param id | |
* @return | |
*/ | |
@GetMapping("/getByIdOne") | |
public UserEntity getByIdOne(Integer id){return userDao.selectById(id); | |
} | |
/** | |
* 依据 ID 查问数据 2 | |
* @param id | |
* @return | |
*/ | |
@GetMapping("/getByIdTwo") | |
public UserEntity getByIdTwo(Integer id){UserQuery query = new UserQuery().where.id().eq(id).end(); | |
return userMapper.findOne(query); | |
} | |
/** | |
* 依据 ID 删除 | |
* @param id | |
*/ | |
@GetMapping("/deleteById") | |
public void deleteById(Integer id){userDao.deleteById(id); | |
} | |
/** | |
* 依据 ID 进行更新 | |
* @param userEntity | |
* @return | |
*/ | |
@PostMapping("/updateById") | |
public UserEntity updateById(@RequestBody UserEntity userEntity){boolean b = userDao.updateById(userEntity); | |
if (b){return userDao.selectById(userEntity.getId()); | |
} | |
return null; | |
} | |
/** | |
* 新增 | |
* @param userEntity | |
* @return | |
*/ | |
@PostMapping("/insert") | |
public Integer insert(@RequestBody UserEntity userEntity){return userDao.save(userEntity); | |
} | |
} |
接下来进行测试:
1.6.1 getByIdOne
1.6.2 getByTwo
1.6.3 deleteById
1.6.4 insert
1.6.5 updateById
简略测试通过!
源代码地址:
https://gitee.com/zhouzhaodon…
集体博客地址:
http://www.zhouzhaodong.xyz/
正文完
发表至: springboot
2021-08-06