使用版本:mybatis-plus 3.4.0
此处只是为了记录大概用法,实际会可以点.出很多操作,一个个自己多试试清楚了
用法我部分分为两种,根据需求使用即可
<dependency>
<groupId>com.baomidougroupId>
<artifactId>mybatis-plus-boot-starterartifactId>
<version>3.4.0version>
dependency>
相关实体类参考
NewsEntity.java
参考:https://www.baomidou.com/pages/10c804/#gt
// 普通查询
QueryWrapper<NewsEntity> queryWrapper = new QueryWrapper<>();
// lambda 查询对象
LambdaQueryWrapper<NewsEntity> lambdaQuery = new LambdaQueryWrapper<>();
关键对象:com.baomidou.mybatisplus.core.toolkit.Wrappers
Wrappers已经将new对象封装,可返回多种
// 普通查询
QueryWrapper<NewsEntity> queryWrapper = Wrappers.query();
// lambda 查询对象
LambdaQueryWrapper<NewsEntity> lambdaQuery = Wrappers.lambdaQuery();
普通
QueryWrapper<NewsEntity> queryWrapper = new QueryWrapper<>();
queryWrapper.eq("type", entity.getType());
queryWrapper.like("title", entity.getTitle());
lambda 1
QueryWrapper<NewsEntity> queryWrapper = Wrappers.query();
// type是Integer类型,判断是否不为空,满足则执行
queryWrapper.lambda().eq(ObjectUtil.isNotNull(entity.getType()), NewsEntity::getType, entity.getType());
// title是string类型,判断是否不为空,满足则执行
queryWrapper.lambda().like(StrUtil.isNotEmpty(entity.getTitle()), NewsEntity::getTitle, entity.getTitle());
lambda 2
LambdaQueryWrapper<NewsEntity> lambdaQuery = Wrappers.lambdaQuery();
// type是Integer类型,判断是否不为空,满足则执行
lambdaQuery.eq(ObjectUtil.isNotNull(entity.getType()), NewsEntity::getType, entity.getType());
// title是string类型,判断是否不为空,满足则执行
lambdaQuery.like(StrUtil.isNotEmpty(entity.getTitle()), NewsEntity::getTitle, entity.getTitle());
这里用到的是eq(等于)、like(全模糊查询),官方还有ne(不等于)、gt(大于)、ge(大于等于)、lt(小于)、le(小于等于)…等等,该有的都会有(点击上面那个链接查看)
单单用or
lambdaQuery.eq(NewsEntity::getType, 1).or().eq(NewsEntity::getType, 2);
多条件
wrapper.lambda().like(NewsEntity::getTitle, "所");
wrapper.lambda().and(w -> w.eq(NewsEntity::getType, 1).or().eq(NewsEntity::getType, 2));
方式一
wrapper.lambda().between(NewsEntity::getReleaseTime, "2023-08-08 18:00:00", "2023-08-09 00:00:00");
方式二
wrapper.lambda().ge(NewsEntity::getReleaseTime, "2023-08-08 18:00:00");
wrapper.lambda().le(NewsEntity::getReleaseTime, "2023-08-09 00:00:00");
在条件最后面加
queryWrapper.last(" limit 1");
普通
queryWrapper.orderByAsc(" createDate, type");
lambda
queryWrapper.lambda().orderByDesc(NewsEntity::getCreateDate, NewsEntity::getType);
普通
queryWrapper.select(" id, title, content, release_time");
lambda
lambdaQuery.select(NewsEntity::getId, NewsEntity::getTitle, NewsEntity::getContent, NewsEntity::getReleaseTime);
我只会QueryWrapper的方式
// 不需要查询返回这些字段
queryWrapper.select(NewsEntity.class, vo -> !vo.getColumn().equals("release_time")
&& !vo.getColumn().equals("id")
&& !vo.getColumn().equals("content"));
我只会QueryWrapper的方式
// 不需要查询返回release_time这个字段
queryWrapper.select(NewsEntity.class, vo -> !vo.getColumn().equals("release_time"));
// 且重新拼接返回字段,修改release_time时间格式再拼接
queryWrapper.select(queryWrapper.getSqlSelect() + ", convert(varchar(19), release_time,120) as releaseTime");
// 分页查询
int page = 1;
int limit = 10;
IPage<NewsEntity> iPage = newsService.page(new Page<>(page, limit), queryWrapper);
// 常用返回值
List<NewsEntity> list = iPage.getRecords();
long count = iPage.getTotal();