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
<dependencies>
2
    <!--Springboot-->
3
    <dependency>
4
        <groupId>org.springframework.boot</groupId>
5
        <artifactId>spring-boot-starter</artifactId>
6
        <version>2.3.2.RELEASE</version>
7
    </dependency>
8
    <dependency>
9
        <groupId>org.springframework.boot</groupId>
10
        <artifactId>spring-boot-starter-web</artifactId>
11
        <version>2.3.2.RELEASE</version>
12
    </dependency>
13
    <!--Lombok-->
14
    <dependency>
15
        <groupId>org.projectlombok</groupId>
16
        <artifactId>lombok</artifactId>
17
        <version>1.18.8</version>
18
    </dependency>
19
    <!--MySQL-->
20
    <dependency>
21
        <groupId>mysql</groupId>
22
        <artifactId>mysql-connector-java</artifactId>
23
        <version>5.1.49</version>
24
    </dependency>
25
    <!--MybatisPlus-->
26
    <dependency>
27
        <groupId>com.baomidou</groupId>
28
        <artifactId>mybatis-plus-boot-starter</artifactId>
29
        <version>3.4.3</version>
30
    </dependency>
31
    <dependency>
32
        <groupId>com.baomidou</groupId>
33
        <artifactId>mybatis-plus</artifactId>
34
        <version>3.4.3</version>
35
    </dependency>
36
</dependencies>

application.yml

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

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

Springboot启动类

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

1
@SpringBootApplication
2
@MapperScan(value = "com.blackteachan.demo.dao")
3
public class DemoApplication {
4
    
5
    public static void main(String[] args) {
6
        SpringApplication.run(DemoApplication.class, args);
7
    }
8
    
9
}

Controller

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

1
@RestController
2
@RequestMapping("/demo")
3
public class DemoController {
4
    
5
    @Autowired
6
    private DemoService demoService;
7
    
8
    @GetMapping("/{id}")
9
    public ServerResponse<TableEntity> get(@PathVariable Long id){
10
        TableEntity tableEntity = demoService.getById(id);
11
        return ServerResponseUtil.success(tableEntity);
12
    }
13
    
14
    @PostMapping
15
    public ServerResponse save(TableEntity tableEntity){
16
        tableEntity.setCreateUser(1L);
17
        tableEntity.setCreateTime(new Date());
18
        demoService.save(tableEntity);
19
        return ServerResponseUtil.success();
20
    }
21
    
22
}

Service

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

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

ServiceImpl

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

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

Dao

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

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

Mapper

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

1
<?xml version="1.0" encoding="UTF-8" ?>
2
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
3
<mapper namespace="com.blackteachan.demo.dao.DemoMapper">
4
    
5
</mapper>

实体类

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

1
@Data
2
@TableName(value = "t_table")
3
public class TableEntity implements Serializable {
4
    
5
    private static final long serialVersionUID = -3906522870557549136L;
6
    
7
    @TableId(value = "id", type = IdType.AUTO)
8
    private Long id;
9
    private String code;
10
    private String name;
11
    private Long createUser;
12
    private Date createDate;
13
    private Long updateUser;
14
    private Date updateTime;
15
    
16
}

MySQL

非常简单的一张表

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

Postman测试

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

1
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 ( ?, ?, ?, ? )
2
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)
3
2021-08-04 15:43:44,363 [http-nio-8888-exec-6] DEBUG com.blackteachan.demo.dao.DemoMapper.insert -[BaseJdbcLogger.java:137]- <==    Updates: 1
4
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 ( ?, ?, ?, ? )
5
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)
6
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
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=?
2
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)
3
2021-08-04 15:47:58,839 [http-nio-8888-exec-2] DEBUG com.blackteachan.demo.dao.DemoMapper.selectById -[BaseJdbcLogger.java:137]- <==      Total: 1
坚持原创技术分享,您的支持将鼓励我继续创作!