博客
关于我
MyBatis学习总结(27)——Mybatis-Plus使用小技巧
阅读量:797 次
发布时间:2023-02-09

本文共 3408 字,大约阅读时间需要 11 分钟。

MyBatis-Plus 条件查询与分页优化

在MyBatis-Plus中,条件查询与分页操作是常用的功能,以下是对这些操作的详细说明和实践示例。

条件查询(QueryWrapper)

1. 基本条件查询

要在用户信息表中筛选出年龄等于20的用户,可以使用QueryWrapper构建条件。

QueryWrapper queryWrapper = new QueryWrapper<>();
queryWrapper.eq("age", 20);
List
userInfoList = userInfoMapper.selectList(queryWrapper);

2. Lambda表达式

当字段名较多或易于混淆时,可以使用Lambda表达式来构建条件。

QueryWrapper queryWrapper = new QueryWrapper<>();
queryWrapper.lambda()
.eq(UserInfo::getAge, 20);
List
userInfoList = userInfoMapper.selectList(queryWrapper);

3. LambdaQueryWrapper

LambdaQueryWrapperQueryWrapper的Lambda写法一致,适用于动态条件。

LambdaQueryWrapper queryWrapper = new LambdaQueryWrapper<>();
queryWrapper.eq(UserInfo::getAge, 20);
List
userInfoList = userInfoMapper.selectList(queryWrapper);

分页查询

1. 基本分页

使用Page对象进行分页查询。

Page
page = new Page<>(currentPage, pageSize);
IPage
iPage = userInfoMapper.selectPage(page, queryWrapper);

2. 分页查询(联表)

当需要联表分页时,创建UserInfoVO类并配置XML映射。

public class UserInfoVO extends UserInfo {
private String sexText;
}

UserInfoMapper中定义分页方法。

IPage
iPage = userInfoMapper.list(page, queryWrapper);

UserInfoServiceImpl中调用分页方法。

Page
page = new Page<>(page, limit);
IPage
iPage = userInfoMapper.list(page, queryWrapper);
List
userInfoList = iPage.getRecords();

3. 分页查询(默认条件)

如果不需要额外条件,queryWrapper可设为null

Page
page = new Page<>(page, limit);
IPage
iPage = userInfoMapper.selectPage(page, null);
List
userInfoList = iPage.getRecords();

AND与OR的使用

1. 基本组合

构建包含AND和OR条件的查询。

LambdaQueryWrapper queryWrapper = new LambdaQueryWrapper<>();
queryWrapper.eq(UserInfo::getId, "1")
.and(i -> i.eq(UserInfo::getName, "jack"))
.or(i -> i.eq(UserInfo::getPhone, "13888888888"));
List
userInfoList = userInfoMapper.selectList(queryWrapper);

2. 复杂组合

构建更复杂的条件组合。

LambdaQueryWrapper queryWrapper = new LambdaQueryWrapper<>();
queryWrapper.eq(UserInfo::getId, "1")
.and(i -> i.and(j -> j.eq(UserInfo::getName, "jack").eq(UserInfo::getCategory, 1)))
.or(j -> j.eq(UserInfo::getPhone, "13888888888").eq(UserInfo::getCategory, 2));
List
userInfoList = userInfoMapper.selectList(queryWrapper);

逻辑删除(@TableLogic)

在用户信息表中添加逻辑删除字段del_flag,通过注解配置。

@ApiModelProperty(value = "删除状态(0--未删除1--已删除)")
@TableField("del_flag")
@TableLogic
private Integer delFlag;

配置MyBatis-Plus全局配置,启用逻辑删除。

在查询时,逻辑删除条件会自动添加。

LambdaQueryWrapper queryWrapper = new LambdaQueryWrapper<>();
queryWrapper.eq(UserInfo::getAge, 20);
List
userInfoList = userInfoMapper.selectList(queryWrapper);

指定查询字段

为了提高查询效率,可以指定只需查询的字段。

LambdaQueryWrapper queryWrapper = new LambdaQueryWrapper<>();
queryWrapper.select(UserInfo::getId, UserInfo::getName, UserInfo::getPhone)
.eq(UserInfo::getAge, 20);
List
userInfoList = userInfoMapper.selectList(queryWrapper);

查询一条数据(getOne)

使用getOne方法查询单条数据,需注意唯一性。

LambdaQueryWrapper queryWrapper = new LambdaQueryWrapper<>();
queryWrapper.eq(UserInfo::getOpenId, openId);
UserInfo userInfo = userInfoService.getOne(queryWrapper);

当多个记录存在时,可传false避免异常。

LambdaQueryWrapper queryWrapper = new LambdaQueryWrapper<>();
queryWrapper.eq(UserInfo::getOpenId, openId);
UserInfo userInfo = userInfoService.getOne(queryWrapper, false);

总结

以上方法可以帮助开发者高效地进行条件查询和分页操作。在实际项目中,根据具体需求选择合适的方法,确保代码的可读性和可维护性。通过合理使用QueryWrapper、分页组件以及注解配置,可以显著提升代码效率和用户体验。

转载地址:http://cyffk.baihongyu.com/

你可能感兴趣的文章
MSSQL将多行单列变一行一列并用指定分隔符分隔,模拟Mysql中的group_concat
查看>>
Movie播放Gif,完美实现屏幕适配
查看>>
MPLS 实验其实不难,把这篇文章中的实验做会,后面轻轻松松!
查看>>
mpls 标签操作
查看>>
MPLS和SD-WAN有什么区别?为什么很多企业要从 MPLS 迁移到 SD-WAN?
查看>>
MPLS基础知识
查看>>
MPM模块
查看>>
mppt算法详解-ChatGPT4o作答
查看>>
mpvue 小程序切换页面时数据没清空的坑
查看>>
mpvue+vant app搭建微信小程序
查看>>
Mpvue小程序的最新规范
查看>>
mpvue的使用(一)必要的开发环境
查看>>
mpvue的使用(三)封装axios
查看>>
mpvue的使用(二)使用vant-weapp
查看>>
mq 消费慢处理方式,rocketmq消费慢如何处理,mq如何处理消费端消费速率慢。rocketmq优化
查看>>
MQ 重复消费如何解决?
查看>>
MQC功能测试大揭秘(4)- MQC 功能测试 DEMO
查看>>
mqtt broker服务端
查看>>
mqtt haproxy 代理及负载搭建
查看>>
MQTT v5共享订阅是怎么回事?如何使用共享订阅提高消息订阅的灵活性和可伸缩性?
查看>>