SpringBoot之路:整合MyBatisPlus

“MyBatis-Plus(简称 MP)是一个 MyBatis 的增强工具,在 MyBatis 的基础上只做增强不做改变,为简化开发、提高效率而生”

集成MyBatisPlus后,可以通过继承类的方式,在Service、Dao、Mapper中实现简单逻辑以及SQL,省去了大部分简单代码的工作,的提高开发效率。
本文记录了Springboot项目快速整合MyBatisPlus的过程。

使用的环境如下

环境 版本
系统 macOS 10.15.4
JDK 8
Maven 3

搭建Springboot

Springboot的搭建过程 懒的写 不在本文记录…
使用的是2.3.2.RELEASE的版本

Maven依赖

引入MyBatisPlus依赖

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
28
29
30
31
32
33
34
35
36
<dependencies>
<!--Springboot-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
<version>2.3.2.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<version>2.3.2.RELEASE</version>
</dependency>
<!--Lombok-->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.8</version>
</dependency>
<!--MySQL-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.49</version>
</dependency>
<!--MybatisPlus-->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.4.3</version>
</dependency>
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus</artifactId>
<version>3.4.3</version>
</dependency>
</dependencies>

application.yml

配置文件中要给MyBatisPlus加上相关的参数

1
2
mybatis-plus:
mapper-locations: classpath*:mappers/**/*Mapper.xml

Springboot启动类

在启动类上添加
@MapperScan(value = "com.blackteachan.demo.dao")
来扫描Dao

1
2
3
4
5
6
7
8
9
@SpringBootApplication
@MapperScan(value = "com.blackteachan.demo.dao")
public class DemoApplication {

public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}

}

Controller

控制器调用Service没啥好说的
这里的ServerResponse是封装返回给前端用的

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
@RestController
@RequestMapping("/demo")
public class DemoController {

@Autowired
private DemoService demoService;

@GetMapping("/{id}")
public ServerResponse<TableEntity> get(@PathVariable Long id){
TableEntity tableEntity = demoService.getById(id);
return ServerResponseUtil.success(tableEntity);
}

@PostMapping
public ServerResponse save(TableEntity tableEntity){
tableEntity.setCreateUser(1L);
tableEntity.setCreateTime(new Date());
demoService.save(tableEntity);
return ServerResponseUtil.success();
}

}

Service

DemoService接口继承MyBatisPlus的IService<T>
IService<T>的T是表的实体类

1
2
3
public interface DemoService extends IService<TableEntity> {

}

ServiceImpl

DemoServiceImpl实现类除了实现DemoService接口外,还要继承MyBatisPlus的ServiceImpl<M, T>
ServiceImpl<M, T>的M是一个继承BaseMapper<T>的Dao,T是表的实体类

1
2
3
4
@Service
public class DemoServiceImpl extends ServiceImpl<DemoMapper, TableEntity> implements DemoService {

}

Dao

DemoMapper接口需要继承MyBatisPlus的BaseMapper<T>
T是表的实体类

1
2
3
public interface DemoMapper extends BaseMapper<TableEntity> {

}

Mapper

Mapper.xml里很空,不需要加东西

1
2
3
4
5
<?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.blackteachan.demo.dao.DemoMapper">

</mapper>

实体类

TableEntity的属性和数据库表字段一一对应

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
@Data
@TableName(value = "t_table")
public class TableEntity implements Serializable {

private static final long serialVersionUID = -3906522870557549136L;

@TableId(value = "id", type = IdType.AUTO)
private Long id;
private String code;
private String name;
private Long createUser;
private Date createDate;
private Long updateUser;
private Date updateTime;

}

MySQL

非常简单的一张表

1
2
3
4
5
6
7
8
9
10
CREATE TABLE `t_table` (
`id` bigint(20) NOT NULL AUTO_INCREMENT,
`code` varchar(20) NOT NULL,
`name` varchar(50) DEFAULT NULL,
`create_user` bigint(255) NOT NULL,
`create_time` datetime NOT NULL,
`update_user` bigint(20) DEFAULT NULL,
`update_time` datetime DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

Postman测试

通过Postman工具请求接口,在console里面可以看到数据已经被插入了
调用Post接口:

1
2
3
4
5
6
2021-08-04 15:43:44,360 [http-nio-8888-exec-6] DEBUG com.blackteachan.demo.dao.DemoMapper.insert -[BaseJdbcLogger.java:137]- ==>  Preparing: INSERT INTO t_table ( code, name, create_user, create_time ) VALUES ( ?, ?, ?, ? )
2021-08-04 15:43:44,361 [http-nio-8888-exec-6] DEBUG com.blackteachan.demo.dao.DemoMapper.insert -[BaseJdbcLogger.java:137]- ==> Parameters: CODE_0001(String), 名称(String), 1(Long), 2021-08-04 15:43:44.359(Timestamp)
2021-08-04 15:43:44,363 [http-nio-8888-exec-6] DEBUG com.blackteachan.demo.dao.DemoMapper.insert -[BaseJdbcLogger.java:137]- <== Updates: 1
2021-08-04 15:44:09,188 [http-nio-8888-exec-7] DEBUG com.blackteachan.demo.dao.DemoMapper.insert -[BaseJdbcLogger.java:137]- ==> Preparing: INSERT INTO t_table ( code, name, create_user, create_time ) VALUES ( ?, ?, ?, ? )
2021-08-04 15:44:09,189 [http-nio-8888-exec-7] DEBUG com.blackteachan.demo.dao.DemoMapper.insert -[BaseJdbcLogger.java:137]- ==> Parameters: CODE_0002(String), 名称(String), 1(Long), 2021-08-04 15:44:09.187(Timestamp)
2021-08-04 15:44:09,190 [http-nio-8888-exec-7] DEBUG com.blackteachan.demo.dao.DemoMapper.insert -[BaseJdbcLogger.java:137]- <== Updates: 1

调用Get接口:

1
2
3
2021-08-04 15:47:58,831 [http-nio-8888-exec-2] DEBUG com.blackteachan.demo.dao.DemoMapper.selectById -[BaseJdbcLogger.java:137]- ==>  Preparing: SELECT id,code,name,create_user,create_time,update_user,update_time FROM t_table WHERE id=?
2021-08-04 15:47:58,831 [http-nio-8888-exec-2] DEBUG com.blackteachan.demo.dao.DemoMapper.selectById -[BaseJdbcLogger.java:137]- ==> Parameters: 1(Long)
2021-08-04 15:47:58,839 [http-nio-8888-exec-2] DEBUG com.blackteachan.demo.dao.DemoMapper.selectById -[BaseJdbcLogger.java:137]- <== Total: 1
如果文章对您有帮助,欢迎评论或打赏,感谢支持!