MybatisPlus基础01
# MybatisPlus
# 环境搭建
使用
spring Initializr
初始化项目添加
MybatisPlus
的依赖<dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus-boot-starter</artifactId> <version>3.4.1</version> </dependency> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid</artifactId> <version>1.1.16</version> </dependency>
1
2
3
4
5
6
7
8
9
10制作表和实体类
create database if not exists mybatisplus_db character set utf8; use mybatisplus_db; CREATE TABLE user ( id bigint(20) primary key auto_increment, name varchar(32) not null, password varchar(32) not null, age int(3) not null , tel varchar(32) not null ); insert into user values(null,'tom','123456',12,'12345678910'); insert into user values(null,'jack','123456',8,'12345678910'); insert into user values(null,'jerry','123456',15,'12345678910'); insert into user values(null,'tom','123456',9,'12345678910'); insert into user values(null,'snake','123456',28,'12345678910'); insert into user values(null,'张益达','123456',22,'12345678910'); insert into user values(null,'张大炮','123456',16,'12345678910');
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16public class User { private Long id; private String name; private String password; private Integer age; private String tel; //自行添加getter、setter、toString()等方法 }
1
2
3
4
5
6
7
8设置 jdbc 参数
spring: datasource: type: com.alibaba.druid.pool.DruidDataSource driver-class-name: com.mysql.cj.jdbc.Driver url: jdbc:mysql://localhost:3306/mybatisplus_db?serverTimezone=UTC username: root password: root
1
2
3
4
5
6
7定义
dao
层,继承BaseMapper
接口package com.itheima.dao; import com.baomidou.mybatisplus.core.mapper.BaseMapper; import com.itheima.domain.User; import org.apache.ibatis.annotations.Mapper; @Mapper public interface UserDao extends BaseMapper<User> { }
1
2
3
4
5
6
7
8
9测试使用
@SpringBootTest public class Mybatisplus01QuickstartApplicationTests { @Autowired private UserDao userDao; @Test void testGetAll() { List<User> userList = userDao.selectList(null); System.out.println(userList); } }
1
2
3
4
5
6
7
8
9
10
11
12
# 基本增删改查
# 分页功能的使用
设置分页拦截器
@Configuration public class MybatisPlusConfig { @Bean public MybatisPlusInterceptor mybatisPlusInterceptor(){ //1 创建MybatisPlusInterceptor拦截器对象 MybatisPlusInterceptor mpInterceptor=new MybatisPlusInterceptor(); //2 添加分页拦截器 mpInterceptor.addInnerInterceptor(new PaginationInnerInterceptor()); return mpInterceptor; } }
1
2
3
4
5
6
7
8
9
10
11
12分页查询
@Test void testSelectPage(){ //1 创建IPage分页对象,设置分页参数 IPage<User> page=new Page<>(1,3); //2 执行分页查询 userDao.selectPage(page,null); //3 获取分页结果 System.out.println("当前页码值:"+page.getCurrent()); System.out.println("每页显示数:"+page.getSize()); System.out.println("总页数:"+page.getPages()); System.out.println("总条数:"+page.getTotal()); System.out.println("当前页数据:"+page.getRecords()); }
1
2
3
4
5
6
7
8
9
10
11
12
13
# 开启日志
# 开启mp的日志(输出到控制台)
mybatis-plus:
configuration:
log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
1
2
3
4
2
3
4
设置参考:https://www.jianshu.com/p/75f9d11ae011
# 表与字段映射
问题:
- 表字段和类属性不一致
- 类中有表字段未定义的属性
- 类中有不想被查询的属性
- 表名和类名不一致
@Data
@TableName("tbl_user")
public class User {
/*
id为Long类型,因为数据库中id为bigint类型,
并且mybatis有自己的一套id生成方案,生成出来的id必须是Long类型
*/
private Long id;
private String name;
@TableField(value = "pwd",select = false)
private String password;
private Integer age;
private String tel;
@TableField(exist = false) //表示online字段不参与CRUD操作
private Boolean online;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
TableField(value="pwd")
对应表中字段TableField(exist=false)
删除表中不存在的属性TableField(select=false)
设置属性不参与查询TableName(value="user")
设置对应表名
# DQL编程
# 条件查询
QueryWrapper
//方式一:按条件查询
QueryWrapper<User> qw=new QueryWrapper<>();
qw.lt("age", 18);
List<User> userList = userDao.selectList(qw);
System.out.println(userList);
1
2
3
4
5
2
3
4
5
lambda
格式
//方式二:lambda格式按条件查询
QueryWrapper<User> qw = new QueryWrapper<User>();
qw.lambda().lt(User::getAge, 10);
List<User> userList = userDao.selectList(qw);
System.out.println(userList);
1
2
3
4
5
2
3
4
5
LambdaQueryWrapper
格式
//方式三:lambda格式按条件查询
LambdaQueryWrapper<User> lqw = new LambdaQueryWrapper<User>();
lqw.lt(User::getAge, 10);
List<User> userList = userDao.selectList(lqw);
System.out.println(userList);
1
2
3
4
5
2
3
4
5
# 组合条件
and
//并且关系
LambdaQueryWrapper<User> lqw = new LambdaQueryWrapper<User>();
//并且关系:10到30岁之间
lqw.lt(User::getAge, 30).gt(User::getAge, 10);
List<User> userList = userDao.selectList(lqw);
System.out.println(userList);
1
2
3
4
5
6
2
3
4
5
6
or
//或者关系
LambdaQueryWrapper<User> lqw = new LambdaQueryWrapper<User>();
//或者关系:小于10岁或者大于30岁
lqw.lt(User::getAge, 10).or().gt(User::getAge, 30);
List<User> userList = userDao.selectList(lqw);
System.out.println(userList);
1
2
3
4
5
6
2
3
4
5
6
# NULL值处理
Integer minAge=10; //将来有用户传递进来,此处简化成直接定义变量了
Integer maxAge=null; //将来有用户传递进来,此处简化成直接定义变量了
LambdaQueryWrapper<User> lqw = new LambdaQueryWrapper<User>();
//参数1:如果表达式为true,那么查询才使用该条件
lqw.gt(minAge!=null,User::getAge, minAge)
.lt(maxAge!=null,User::getAge, maxAge);
List<User> userList = userDao.selectList(lqw);
userList.forEach(System.out::println);
1
2
3
4
5
6
7
8
2
3
4
5
6
7
8
# 查询投影
- 部分属性
/*LambdaQueryWrapper<User> lqw = new LambdaQueryWrapper<User>();
lqw.select(User::getId, User::getName, User::getAge);*/
//或者
QueryWrapper<User> lqw = new QueryWrapper<User>();
lqw.select("id", "name", "age", "tel");
List<User> userList = userDao.selectList(lqw);
System.out.println(userList);
1
2
3
4
5
6
7
2
3
4
5
6
7
- 未定义属性
QueryWrapper<User> lqw = new QueryWrapper<User>();
lqw.select("count(*) as count, tel");
lqw.groupBy("tel");
List<Map<String, Object>> userList = userDao.selectMaps(lqw);
System.out.println(userList);
1
2
3
4
5
2
3
4
5
# 多条件查询
- 范围匹配(> 、 = 、between)
- 模糊匹配(like)
- 空判定(null)
- 包含性匹配(in)
- 分组(group)
- 排序(order)
- ...
eq
匹配
LambdaQueryWrapper<User> lqw = new LambdaQueryWrapper<User>();
//等同于=
lqw.eq(User::getName, "Jerry").eq(User::getPassword, "jerry");
User loginUser = userDao.selectOne(lqw);
System.out.println(loginUser);
1
2
3
4
5
2
3
4
5
le、ge、between
区间
LambdaQueryWrapper<User> lqw = new LambdaQueryWrapper<User>();
//范围查询 lt le gt ge eq between
lqw.between(User::getAge, 10, 30);
List<User> userList = userDao.selectList(lqw);
System.out.println(userList);
1
2
3
4
5
2
3
4
5
like
检索
LambdaQueryWrapper<User> lqw = new LambdaQueryWrapper<User>();
//模糊匹配 like
lqw.likeLeft(User::getName, "J");
List<User> userList = userDao.selectList(lqw);
System.out.println(userList);
1
2
3
4
5
2
3
4
5
- 聚合函数
QueryWrapper<User> qw = new QueryWrapper<User>();
qw.select("gender","count(*) as nums");
qw.groupBy("gender");
List<Map<String, Object>> maps = userDao.selectMaps(qw);
System.out.println(maps);
1
2
3
4
5
2
3
4
5
更多参考:https://mybatis.plus/guide/wrapper.html#abstractwrapper
# DML编程
# ID生成策略
- 日志:自增(1,2,3,4,……)
- 购物订单:特殊规则(FQ23948AK3843)
- 外卖单:关联地区日期等信息(10 04 20200314 34 91)
- 关系表:可省略id
- ……
设置全局配置
mybatis-plus:
global-config:
db-config:
id-type: assign_id
table-prefix: tbl_
1
2
3
4
5
2
3
4
5
为前缀为 tbl_
的表设置主键的生成策略为自增
# 多记录删除
//删除指定多条数据
List<Long> list = new ArrayList<>();
list.add(1402551342481838081L);
list.add(1402553134049501186L);
list.add(1402553619611430913L);
userDao.deleteBatchIds(list);
1
2
3
4
5
6
7
2
3
4
5
6
7
//查询指定多条数据
List<Long> list = new ArrayList<>();
list.add(1L);
list.add(3L);
list.add(4L);
userDao.selectBatchIds(list);
1
2
3
4
5
6
2
3
4
5
6
# 逻辑删除
@Data
public class User {
private Long id;
//逻辑删除字段,标记当前记录是否被删除
@TableLogic
private Integer deleted;
}
1
2
3
4
5
6
7
8
9
10
2
3
4
5
6
7
8
9
10
配置逻辑删除
mybatis-plus:
global-config:
db-config:
table-prefix: tbl_
# 逻辑删除字段名
logic-delete-field: deleted
# 逻辑删除字面值:未删除为0
logic-not-delete-value: 0
# 逻辑删除字面值:删除为1
logic-delete-value: 1
1
2
3
4
5
6
7
8
9
10
2
3
4
5
6
7
8
9
10
编辑 (opens new window)
上次更新: 2023/02/07, 14:51:48