MyBatis-Plus简单上手教程(案例)(IDEA+SpringBoot+Maven+Mybatis-Plus)
一、简单开始
1、简介
MyBatis-Plus 是一个 Mybatis 增强版工具,在 MyBatis 上扩充了其他功能,并没有改变Mybatis的基本功能,为了简化开发提交效率而存在。
官网文档地址:
https://mp.baomidou.com/guide/
MyBatis-Plus 特性:
https://mp.baomidou.com/guide/#%E7%89%B9%E6%80%A7
2、使用
(1)准备工作
使用IDEA创建一个SpringBoot+Maven项目。
(3)添加 MyBatis-Plus 依赖(mybatis-plus-boot-starter)
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.3.1.tmp</version></dependency>
1
2
3
4
5
(4)添加MySQL、Lombok依赖
<!-- mysql依赖--><dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.18</version></dependency><!-- Lombok--><dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.10</version></dependency>
1
2
3
4
5
6
7
8
9
10
11
12
(5)完整依赖文件(pom.xml)
<?xml version="1.0" encoding="UTF-8"?><project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.11.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.cncc</groupId>
<artifactId>test_mybatis_plus2</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>test_mybatis_plus2</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<!-- mybatisplus依赖-->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.3.1.tmp</version>
</dependency>
<!--代码生成器-->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-generator</artifactId>
<version>3.3.1.tmp</version>
</dependency>
<!-- 模板引擎依赖-->
<dependency>
<groupId>org.apache.velocity</groupId>
<artifactId>velocity</artifactId>
<version>1.7</version>
</dependency>
<!-- mysql依赖-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.18</version>
</dependency>
<!-- Lombok-->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.10</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
<!--配置资源-->
<resources>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.xml</include>
</includes>
</resource>
<resource>
<directory>src/main/resources</directory>
</resource>
</resources>
</build></project>
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
(6)在数据库中创建一个表
我建了一个t_user表,字段为id,name,password。
(7)在 application.yml 文件中配置 mysql 数据源信息。
spring:
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver username: root password: 123456
url: jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf8&serverTimezone=UTC
1
2
3
4
5
6

(8)对于entity、mapper,service,controller的创建有两种方式。一种为自己手动创建,另一种为使用Mybatis-Plus代码生成器自动生成。
自己手动创建:
编写表对应的 实体类。
package entity;import lombok.Data;@Datapublic class User {
private Long id;
private String name;
private String password;}
1
2
3
4
5
6
7
8
9
10

编写操作实体类的 Mapper 类。需继承 BaseMapper,这是 mybatis-plus 封装好的类。
package com.cncc.mapper;import com.baomidou.mybatisplus.core.mapper.BaseMapper;import com.cncc.entity.User;public interface UserMapper extends BaseMapper<User> {}
1
2
3
4
5
6
7
8

手动创建就到这里。service和controller暂时省略。因为实体类、Mapper 类都写好后就可以使用了。
(使用参考(9))
代码生成器创建:
添加依赖
<!--代码生成器-->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-generator</artifactId>
<version>3.3.1.tmp</version>
</dependency>
<!-- 模板引擎依赖-->
<dependency>
<groupId>org.apache.velocity</groupId>
<artifactId>velocity</artifactId>
<version>1.7</version>
</dependency>
1
2
3
4
5
6
7
8
9
10
11
12
新建一个测试类

(注意import的类,别导错了)
package com.cncc.autogenerator;import com.baomidou.mybatisplus.annotation.IdType;import com.baomidou.mybatisplus.generator.AutoGenerator;import com.baomidou.mybatisplus.generator.config.DataSourceConfig;import com.baomidou.mybatisplus.generator.config.GlobalConfig;import com.baomidou.mybatisplus.generator.config.PackageConfig;import com.baomidou.mybatisplus.generator.config.StrategyConfig;import com.baomidou.mybatisplus.generator.config.rules.DateType;import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;import org.junit.Test;public class TestAutoGenerate {
@Test
public void autoGenerate(){
//步骤1:创建代码生成器,用于代码生成。
AutoGenerator autoGenerator = new AutoGenerator();
//步骤2: 全局配置。指定代码输出路径以及包名、作者等信息。
GlobalConfig globalConfig = new GlobalConfig();
// 填写代码生成的目录(自己项目所在目录)
String projectPath = "D:\\WorkSpace\\IDEA_CNCC\\test_mybatis_plus2";
// 拼接出代码最终输出的目录
globalConfig.setOutputDir(projectPath+"/src/main/java");
// 配置开发者信息(可选)(需要修改)
globalConfig.setAuthor("zyk");
// 配置是否打开目录,false 为不打开(可选)
globalConfig.setOpen(false);
// 实体属性 Swagger2 注解,添加 Swagger 依赖,开启 Swagger2 模式(可选)
//globalConfig.setSwagger2(true);
// 重新生成文件时是否覆盖,false 表示不覆盖(可选)
globalConfig.setFileOverride(false);
// 配置主键生成策略,此处为 ASSIGN_ID(可选)
globalConfig.setIdType(IdType.ASSIGN_ID);
// 配置日期类型,此处为 ONLY_DATE(可选)
globalConfig.setDateType(DateType.ONLY_DATE);
// 默认生成的 service 会有 I 前缀
globalConfig.setServiceName("%sService");
autoGenerator.setGlobalConfig(globalConfig);
// 步骤3:数据源配置。(需要修改) 用于指定需要生成代码的数据仓库,数据表。
DataSourceConfig dsc = new DataSourceConfig();
// 配置数据库 url 地址
dsc.setUrl("jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf8&serverTimezone=UTC");
// dsc.setSchemaName("testMyBatisPlus"); // 可以直接在 url 中指定数据库名
// 配置数据库驱动
dsc.setDriverName("com.mysql.cj.jdbc.Driver");
// 配置数据库连接用户名
dsc.setUsername("root");
// 配置数据库连接密码
dsc.setPassword("123456");
autoGenerator.setDataSource(dsc);
// 步骤:4:包配置 配置包信息
PackageConfig packageConfig = new PackageConfig();
// 配置父包名(需要修改)
packageConfig.setParent("com.cncc");
// 配置模块名(需要修改) (这里是设置com.cncc的下一级,如果为空,则创建的包会直接在com.cncc下;如果不为空,例如填为test,则创建的包会在com.cncc.test下.)
packageConfig.setModuleName("");
// 配置 entity 包名
packageConfig.setEntity("entity");
// 配置 mapper 包名
packageConfig.setMapper("mapper");
// 配置 service 包名
packageConfig.setService("service");
// 配置 controller 包名
packageConfig.setController("controller");
autoGenerator.setPackageInfo(packageConfig);
// 步骤5:策略配置(数据库表配置)
StrategyConfig strategy = new StrategyConfig();
// 指定表名(可以同时操作多个表,使用 , 隔开)(需要修改)
strategy.setInclude("t_user");
// 配置数据表与实体类名之间映射的策略
strategy.setNaming(NamingStrategy.underline_to_camel);
// 配置数据表的字段与实体类的属性名之间映射的策略
strategy.setColumnNaming(NamingStrategy.underline_to_camel);
// 配置 lombok 模式
strategy.setEntityLombokModel(true);
// 配置 rest 风格的控制器(@RestController)
strategy.setRestControllerStyle(true);
// 配置驼峰转连字符
strategy.setControllerMappingHyphenStyle(true);
// 配置表前缀,生成实体时去除表前缀
// 此处的表名为 test_mybatis_plus_user,模块名为 test_mybatis_plus,去除前缀后剩下为 user。
strategy.setTablePrefix(packageConfig.getModuleName() + "_");
autoGenerator.setStrategy(strategy);
// Step6:执行代码生成操作
autoGenerator.execute();
}}
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
运行测试方法后:

(9)实体类、Mapper 类都写好了,就可以使用了。
Step1:先得在启动类里扫描 Mapper 类,即添加 @MapperScan 注解
package com.cncc;import org.mybatis.spring.annotation.MapperScan;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;@SpringBootApplication@MapperScan("com.cncc.mapper")public class TestMybatisPlusApplication {
public static void main(String[] args) {
SpringApplication.run(TestMybatisPlusApplication.class, args);
}}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
Step2:在测试类里写一个测试方法测试一下。

@Autowired
private UserMapper userMapper;
@Test
public void testSelect() {
System.out.println(("----- selectAll method test ------"));
List<User> userList = userMapper.selectList(null);
for(User user:userList) {
System.out.println(user);
}
}
1
2
3
4
5
6
7
8
9
10
11
(10)总结:
通过以上简单操作,就能对 user 表进行 CRUD 操作,不需要去编写 xml 文件。
注:
若遇到报错mapper文件扫描不到,在pom.xml文件里添加如下代码:

<resources>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.xml</include>
</includes>
</resource>
<resource>
<directory>src/main/resources</directory>
</resource>
</resources>
1
2
3
4
5
6
7
8
9
10
11
二、Mybatis-Plus 常用操作
1、配置日志
想要查看执行的 sql 语句,可以在 yml 文件中添加配置信息,如下。
mybatis-plus:
configuration:
log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
1
2
3

2、自动填充数据功能
(1)添加、修改数据时,每次都会使用相同的方式进行填充。比如 数据的创建时间、修改时间等。
Mybatis-plus 支持自动填充这些字段的数据。
给之前的数据表新增两个字段:创建时间create_time、修改时间update_time。
并使用 代码生成器生成代码。
(3)使用自动填充功能。
Step1:
在实体类里使用 @TableField 注解标注需要进行填充的字段。
/**
* 创建时间
*/
@TableField(fill = FieldFill.INSERT)
private Date createTime;
/**
* 最后修改时间
*/
@TableField(fill = FieldFill.INSERT_UPDATE)
private Date updateTime;
1
2
3
4
5
6
7
8
9
10
11
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-gpYh5O2W-1606980063900)(C:\Users\KK\AppData\Roaming\Typora\typora-user-images\1606908908847.png)]

Step2:
自定义一个类,实现 MetaObjectHandler 接口,并重写方法。
添加 @Component 注解,交给 Spring 去管理。
package com.cncc.handler;import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;import org.apache.ibatis.reflection.MetaObject;import org.springframework.stereotype.Component;import java.util.Date;@Componentpublic class MyMetaObjectHandler implements MetaObjectHandler {
@Override
public void insertFill(MetaObject metaObject) {
//createTime、updateTime为属性名
this.strictInsertFill(metaObject,"createTime", Date.class,new Date());
this.strictInsertFill(metaObject,"updateTime", Date.class,new Date());
}
@Override
public void updateFill(MetaObject metaObject) {
this.strictUpdateFill(metaObject,"updateTime",Date.class,new Date());
}}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23

Step3:
简单测试一下。
@Test
void testInsert2(){//自动填充数据测试 这里自动填充了创建时间和最后修改时间
TUser uer = new TUser();
uer.setName("ZiDong3").setPassword("2222222");
if (userService.save(uer)){
for (TUser user : userService.list()) {
System.out.println(user);
}
}else {
System.out.println("添加数据失败!");
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
3、逻辑删除
(1)简介
删除数据,可以通过物理删除,也可以通过逻辑删除。
物理删除指的是直接将数据从数据库中删除,不保留。
逻辑删除指的是修改数据的某个字段,使其表示为已删除状态,而非删除数据,保留该数据在数据库中,但是查询时不显示该数据(查询时过滤掉该数据)。
给数据表增加一个字段:delete_flag,用于表示该数据是否被逻辑删除。
(2)使用逻辑删除。
可以定义一个自动填充规则,初始值为 0。0 表示未删除, 1 表示删除。
//在实体类里添加属性/**
* 逻辑删除(0 未删除、1 删除)
*/@TableLogic(value = "0", delval = "1")@TableField(fill = FieldFill.INSERT)private Integer deleteFlag;//在自定义的实现MetaObjectHandler接口的类里 @Overridepublic void insertFill(MetaObject metaObject) {
this.strictInsertFill(metaObject, "deleteFlag", Integer.class, 0);}
1
2
3
4
5
6
7
8
9
10
11
12
13
14


(3)简单测试
使用 mybatis-plus 封装好的方法时,会自动添加逻辑删除的功能。
若是自定义的 sql 语句,需要手动添加逻辑。
//逻辑删除
/*
物理删除指的是直接将数据从数据库中删除,不保留。
逻辑删除指的是修改数据的某个字段,使其表示为已删除状态,
而非删除数据,保留该数据在数据库中,但是查询时不显示该
数据(查询时过滤掉该数据)。
*/
//可以定义一个自动填充规则,初始值为 0。0 表示未删除, 1 表示删除。
//注意:当使用逻辑删除这个功能后,若提供判断的属性值不为0(假设设置0表示未删除),则查不到。
@Test
public void testDelete() {
if (userService.removeById(12)) {
System.out.println("删除数据成功");
userService.list().forEach(System.out::println);
} else {
System.out.println("删除数据失败");
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
现有数据 为:

执行 testDelete 进行逻辑删除后:

PS:若去除属性上的 TableLogic 注解,则再执行 testDelete 时会进行物理删除,直接删除这条数据。
4、分页插件的使用
Step1:
配置分页插件。
编写一个 配置类,内部使用 @Bean 注解将 PaginationInterceptor 交给 Spring 容器管理。
package com.cncc.config;
import com.baomidou.mybatisplus.extension.plugins.PaginationInterceptor;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
//@MapperScan("com.cncc.mapper")
public class Myconfig {
/**
* 分页插件
* @return 分页插件的实例
*/
@Bean
public PaginationInterceptor paginationInterceptor() {
return new PaginationInterceptor();
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20

Step2:
编写分页代码。
直接 new 一个 Page 对象,对象需要传递两个参数(当前页,每页显示的条数)。
调用 mybatis-plus 提供的分页查询方法,其会将 分页查询的数据封装到 Page 对象中。
//分页查询
/*
直接 new 一个 Page 对象,对象需要传递两个参数(当前页,每页显示的条数)。
调用 mybatis-plus 提供的分页查询方法,其会将 分页查询的数据封装到 Page 对象中。
*/
@Test
public void testPage() {
// Step1:创建一个 Page 对象
// Page<TUser> page = new Page<>();//默认当前页为第一页,每页大小为10
Page<TUser> page = new Page<>(2, 5);//设置当前页为第二页,每页5条数据
// Step2:调用 mybatis-plus 提供的分页查询方法
userService.page(page, null);
// Step3:获取分页数据
System.out.println(page.getCurrent()); // 获取当前页
System.out.println(page.getTotal()); // 获取总记录数
System.out.println(page.getSize()); // 获取每页的条数
System.out.println(page.getRecords()); // 获取每页数据的集合
System.out.println(page.getPages()); // 获取总页数
System.out.println(page.hasNext()); // 是否存在下一页
System.out.println(page.hasPrevious()); // 是否存在上一页
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-MwFazeXf-1606980063914)(C:\Users\KK\AppData\Roaming\Typora\typora-user-images\1606976358353.png)]

5、乐观锁(待补充)
三、Mybatis-Plus CRUD 操作了解
1、Mapper 接口方法(CRUD)
使用代码生成器生成的 mapper 接口中,其继承了 BaseMapper 接口。而 BaseMapper 接口中封装了一系列 CRUD 常用操作(当然,自定义代码执行也可以),可以直接使用。
此处简单介绍一下 BaseMapper 接口中的常用方法:
添加数据:
方法介绍int insert(T entity);// 插入一条记录
注:
T 表示任意实体类型
entity 表示实体对象
1
2
3
删除数据:
方法介绍int deleteById(Serializable id);// 根据主键 ID 删除int deleteByMap(@Param(Constants.COLUMN_MAP) Map<String, Object> columnMap);// 根据 map 定义字段的条件删除int delete(@Param(Constants.WRAPPER) Wrapper wrapper);// 根据实体类定义的 条件删除对象int deleteBatchIds(@Param(Constants.COLLECTION) Collection<? extends Serializable> idList);// 进行批量删除
注:
id 表示 主键 ID
columnMap 表示表字段的 map 对象
wrapper 表示实体对象封装操作类,可以为 null。
idList 表示 主键 ID 集合(列表、数组),不能为 null 或 empty
1
2
3
4
5
修改数据:
方法介绍int updateById(@Param(Constants.ENTITY) T entity);// 根据 ID 修改实体对象。int update(@Param(Constants.ENTITY) T entity, @Param(Constants.WRAPPER) Wrapper updateWrapper);// 根据 updateWrapper 条件修改实体对象
注:
update 中的 entity 为 set 条件,可以为 null。
updateWrapper 表示实体对象封装操作类(可以为 null,里面的 entity 用于生成 where 语句)
1
2
3
查询数据:
方法介绍T selectById(Serializable id);// 根据 主键 ID 查询数据List selectBatchIds(@Param(Constants.COLLECTION) Collection<? extends Serializable> idList);// 进行批量查询List selectByMap(@Param(Constants.COLUMN_MAP) Map<String, Object> columnMap);// 根据表字段条件查询T selectOne(@Param(Constants.WRAPPER) Wrapper queryWrapper);// 根据实体类封装对象 查询一条记录Integer selectCount(@Param(Constants.WRAPPER) Wrapper queryWrapper);// 查询记录的总条数List selectList(@Param(Constants.WRAPPER) Wrapper queryWrapper);// 查询所有记录(返回 entity 集合)List<Map<String, Object>> selectMaps(@Param(Constants.WRAPPER) Wrapper queryWrapper);// 查询所有记录(返回 map 集合)List selectObjs(@Param(Constants.WRAPPER) Wrapper queryWrapper);// 查询所有记录(但只保存第一个字段的值)<E extends IPage> E selectPage(E page, @Param(Constants.WRAPPER) Wrapper queryWrapper);// 查询所有记录(返回 entity 集合),分页<E extends IPage<Map<String, Object>>> E selectMapsPage(E page, @Param(Constants.WRAPPER) Wrapper queryWrapper);// 查询所有记录(返回 map 集合),分页
注:
queryWrapper 表示实体对象封装操作类(可以为 null)
page 表示分页查询条件
1
2
3
2、Service 接口方法(CRUD)
使用 代码生成器 生成的 service 接口中,其继承了 IService 接口。IService 内部进一步封装了 BaseMapper 接口的方法(当然也提供了更详细的方法)。使用时,可以通过 生成的 mapper 类进行 CRUD 操作,也可以通过 生成的 service 的实现类进行 CRUD 操作。(当然,自定义代码执行也可以)
此处简单介绍一下 IService 中封装的常用方法:
添加数据:
方法介绍default boolean save(T entity);// 调用 BaseMapper 的 insert 方法,用于添加一条数据。boolean saveBatch(Collection entityList, int batchSize);// 批量插入数据
注:
entityList 表示实体对象集合
batchSize 表示一次批量插入的数据量,默认为 1000
1
2
3
添加或修改数据:
方法介绍boolean saveOrUpdate(T entity);// id 若存在,则修改, id 不存在则新增数据default boolean saveOrUpdate(T entity, Wrapper updateWrapper);// 先根据条件尝试更新,然后再执行 saveOrUpdate 操作boolean saveOrUpdateBatch(Collection entityList, int batchSize);// 批量插入并修改数据
删除数据:
方法介绍default boolean removeById(Serializable id);// 调用 BaseMapper 的 deleteById 方法,根据 id 删除数据。default boolean removeByMap(Map<String, Object> columnMap);// 调用 BaseMapper 的 deleteByMap 方法,根据 map 定义字段的条件删除default boolean remove(Wrapper queryWrapper);// 调用 BaseMapper 的 delete 方法,根据实体类定义的 条件删除对象。default boolean removeByIds(Collection<? extends Serializable> idList);// 用 BaseMapper 的 deleteBatchIds 方法, 进行批量删除。
修改数据:
方法介绍default boolean updateById(T entity);// 调用 BaseMapper 的 updateById 方法,根据 ID 选择修改。default boolean update(T entity, Wrapper updateWrapper);// 调用 BaseMapper 的 update 方法,根据 updateWrapper 条件修改实体对象。boolean updateBatchById(Collection entityList, int batchSize);// 批量更新数据
查找数据:
方法介绍default T getById(Serializable id);// 调用 BaseMapper 的 selectById 方法,根据 主键 ID 返回数据。default List listByIds(Collection<? extends Serializable> idList);// 调用 BaseMapper 的 selectBatchIds 方法,批量查询数据。default List listByMap(Map<String, Object> columnMap);// 调用 BaseMapper 的 selectByMap 方法,根据表字段条件查询default T getOne(Wrapper queryWrapper);// 返回一条记录(实体类保存)。Map<String, Object> getMap(Wrapper queryWrapper);// 返回一条记录(map 保存)。default int count(Wrapper queryWrapper);// 根据条件返回 记录数。default List list();// 返回所有数据。default List list(Wrapper queryWrapper);// 调用 BaseMapper 的 selectList 方法,查询所有记录(返回 entity 集合)。default List<Map<String, Object>> listMaps(Wrapper queryWrapper);// 调用 BaseMapper 的 selectMaps 方法,查询所有记录(返回 map 集合)。default List listObjs();// 返回全部记录,但只返回第一个字段的值。default <E extends IPage> E page(E page, Wrapper queryWrapper);// 调用 BaseMapper 的 selectPage 方法,分页查询default <E extends IPage<Map<String, Object>>> E pageMaps(E page, Wrapper queryWrapper);// 调用 BaseMapper 的 selectMapsPage 方法,分页查询
注:
get 用于返回一条记录。
list 用于返回多条记录。
count 用于返回记录总数。
page 用于分页查询。
1
2
3
4
5
链式调用:
方法介绍default QueryChainWrapper query();// 普通链式查询default LambdaQueryChainWrapper lambdaQuery();// 支持 Lambda 表达式的修改default UpdateChainWrapper update();// 普通链式修改default LambdaUpdateChainWrapper lambdaUpdate();// 支持 Lambda 表达式的修改
注:
query 表示查询
update 表示修改
Lambda 表示内部支持 Lambda 写法。
形如:
query().eq("column", value).one();
lambdaQuery().eq(Entity::getId, value).list();
update().eq("column", value).remove();
lambdaUpdate().eq(Entity::getId, value).update(entity);
1
2
3
4
5
6
7
8
9