SpringBoot入门:SpringBoot集成mybatis-plus

1、mybatis-plus简介

MyBatis-Plus (简称 MP)是一个 MyBatis 的增强工具,在 MyBatis 的基础上只做增强不做改变,为简化开发、提高效率而生。
官网:https://mp.baomidou.com/
github: https://github.com/baomidou/mybatis-plus

2、新建maven项目,引入maven依赖

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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.3.RELEASE</version>
<relativePath/>
</parent>

<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>

<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.4.2</version>
</dependency>

<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.10</version>
</dependency>

<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.0.29</version>
</dependency>

<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.8</version>
</dependency>

<dependency>
<groupId>commons-collections</groupId>
<artifactId>commons-collections</artifactId>
<version>3.2.1</version>
</dependency>

</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<fork>true</fork>
</configuration>
</plugin>
</plugins>
</build>

3、resources下新增配置文件application.yml

内容如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
spring:
datasource:
type: com.alibaba.druid.pool.DruidDataSource
driver-class-name: com.mysql.jdbc.Driver
url: jdbc:mysql://127.0.0.1:3306/mybatis?useUnicode=true&characterEncoding=utf-8&zeroDateTimeBehavior=convertToNull&generateSimpleParameterMetadata=true
username: root
password: root

mybatis:
#开启驼峰命名法
configuration:
map-underscore-to-camel-case: true
mybatis-plus:
mapper-locations: classpath:mappers/*.xml
# 实体扫描,多个package用逗号或者分号分隔
type-aliases-package: cn.river.springboot.model #自己的实体类地址
configuration:
# 这个配置会将执行的sql打印出来,在开发或测试的时候可以用
log-impl: org.apache.ibatis.logging.stdout.StdOutImpl

数据库脚本:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
CREATE TABLE `tb_user` (
`id` bigint(20) NOT NULL AUTO_INCREMENT COMMENT '主键ID',
`name` varchar(30) DEFAULT NULL COMMENT '姓名',
`age` int(11) DEFAULT NULL COMMENT '年龄',
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

INSERT INTO `mybatis`.`tb_user` (`id`, `name`, `age`) VALUES ('1', 'Jone', '18');
INSERT INTO `mybatis`.`tb_user` (`id`, `name`, `age`) VALUES ('2', 'Jack', '20');
INSERT INTO `mybatis`.`tb_user` (`id`, `name`, `age`) VALUES ('3', 'Tom', '28');
INSERT INTO `mybatis`.`tb_user` (`id`, `name`, `age`) VALUES ('4', 'Sandy', '21');
INSERT INTO `mybatis`.`tb_user` (`id`, `name`, `age`) VALUES ('5', 'Billie', '24');
INSERT INTO `mybatis`.`tb_user` (`id`, `name`, `age`) VALUES ('6', 'jason', '30');

4、新增接口UserMapper.java

继承BaseMapper,内容如下:

1
2
3
public interface UserMapper extends BaseMapper<User> {

}

User.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
@Data
@TableName("tb_user")
@ToString
@NoArgsConstructor
@AllArgsConstructor
public class User implements Serializable {
@TableId(value = "id", type = IdType.AUTO)
private Integer id;

@TableField("name")
private String name;

@TableField("age")
private Integer age;

}

BaseMapper中默认已经实现了基本的数据库表CRUD功能,可以直接使用,比较方便。

如果不满足自己的需求,可以在UserMapper中自定义方法,可以使用注解@Select,@Insert,@Update,@Delete或者xml配置实现数据操作。

5、新增springboot启动类

加上@MapperScan注解,扫描Mapper文件

1
2
3
4
5
6
7
8
9
@SpringBootApplication
@MapperScan("cn.river.springboot.mapper")
public class MyBatisPlusApplication {

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

}

6、新增JUnit单元测试类进行测试

1
2
3
4
5
6
7
8
9
10
11
12
13
@RunWith(SpringRunner.class)
@SpringBootTest
public class SampleTest {

@Resource
private UserMapper userMapper;

@Test
public void testSelect() {
List<User> userList = userMapper.selectList(null);
userList.forEach(System.out::println);
}
}

SpringBoot入门:SpringBoot集成mybatis-plus
https://river106.cn/posts/2e04c026.html
作者
river106
发布于
2021年5月11日
许可协议