I always like to pay attention to more new frameworks, and found a framework that basically doesn't need to write mapper and xml. Let's study this framework.
1. Create a new Spring Boot project
1.1 pom.xml configuration
<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>
We have introduced fluent-mybatis-processor here to automatically generate code and minimize our own code writing.
1.2 application.yml configuration
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 automatic code generation
I have tried to run this in main in non-Test, but the result always fails. Please let me know if you can run it!
@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 { //类名随便取, 只是配置定义的一个载体
}
}
After running, the following three files are generated:
1.4 After generating the code, compiling will generate code in the target directory
The generated code after running is as follows:
1.5 Start class configuration @MapperScan
@MapperScan address is the mapper in the target directory above
Can refer to mine:
1.6 New UserController test
You can refer to the official documents for research, I am just simply implementing the logic here.
The document address is:
https://gitee.com/fluent-mybatis/fluent-mybatis/wikis/fluent%20mybatis%E7%89%B9%E6%80%A7%E6%80%BB%E8%A7%88?sort_id=4216053
@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);
}
}
Next, test:
1.6.1 getByIdOne
1.6.2 getByTwo
1.6.3 deleteById
1.6.4 insert
1.6.5 updateById
Simple test passed!
Source code address:
https://gitee.com/zhouzhaodong/springboot/tree/master/fluent-mybatis
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。