【引言】
使用mybatis-plus框架的基础上,直接使用其中的条件参数进行查询还是很方便的。但每次使用到像大于、不等于这样一些不常用条件时,都需要现查,所以记录在这篇博客里,当作一个自己的查询手册。
【手册】
查询方式 | 说明 |
---|---|
select | 设置查询字段 |
and | AND 语句,拼接 + AND (字段=值) |
or | OR 语句,拼接 + OR (字段=值) |
eq | 等于= |
allEq | 基于 map 内容等于= |
ne | 不等于<> |
gt | 大于> |
ge | 大于等于>= |
lt | 小于< |
le | 小于等于<= |
like | 模糊查询 |
notLike | 模糊查询 NOT LIKE |
in | IN 查询 |
notIn | NOT IN 查询 |
isNull | NULL 值查询 |
isNotNull | IS NOT NULL |
groupBy | 分组 GROUP BY |
having | HAVING 关键词 |
orderBy | 排序 ORDER BY |
orderAsc | ASC 排序 ORDER BY |
orderDesc | DESC 排序 ORDER BY |
exists | EXISTS 条件语句 |
notExists | NOT EXISTS 条件语句 |
between | BETWEEN 条件语句 |
notBetween | NOT BETWEEN 条件语句 |
last | 拼接在最后,例如:last(“LIMIT 1”) |
【示例】
代码使用:
//查询作者和编码字段,返回Article中其他字段的值均为null
public Article searchOne(Integer id) {
LambdaQueryWrapper<Article> queryWrapper = new LambdaQueryWrapper<>();
queryWrapper.select(Article::getAuthor,Article::getCode).eq(Article::getId,id);
return articleMapper.selectOne(queryWrapper);
}
sql打印:
代码使用:
public List<Article> searchMore(String keywords) {
LambdaQueryWrapper<Article> queryWrapper = new LambdaQueryWrapper<>();
queryWrapper.eq(Article::getCatId,10);
queryWrapper.and(x->x.like(Article::getKeywords,keywords).or().like(Article::getTitle,keywords));
return articleMapper.selectList(queryWrapper);
}
sql打印:
代码使用:
//查询条件:访问量大于等于100
public List<Article> searchByCondition() {
LambdaQueryWrapper<Article> queryWrapper = new LambdaQueryWrapper<>();
//大于等于
queryWrapper.ge(Article::getVisits,100);
//查询指定字段
queryWrapper.select(Article::getAuthor,Article::getCode,Article::getTitle,Article::getVisits);
return articleMapper.selectList(queryWrapper);
}
sql打印:
代码使用:
//栏目Id属于10和20的
public List<Article> searchByCondition() {
LambdaQueryWrapper<Article> queryWrapper = new LambdaQueryWrapper<>();
//in
Long[] catId = {10L,20L};
List<Long> catList = Arrays.asList(catId);
queryWrapper.in(Article::getCatId,catList);
//查询指定字段
queryWrapper.select(Article::getAuthor,Article::getCode,Article::getTitle,Article::getVisits);
return articleMapper.selectList(queryWrapper);
}
sql打印:
代码使用:
//查询发布时间在2020-05-01至2020-06-25
public List<Article> searchByCondition() {
LambdaQueryWrapper<Article> queryWrapper = new LambdaQueryWrapper<>();
//between
queryWrapper.between(Article::getPublishTime, LocalDate.of(2020,5,1),LocalDate.now().plusMonths(1));
//查询指定字段
queryWrapper.select(Article::getAuthor,Article::getCode,Article::getTitle,Article::getVisits);
return articleMapper.selectList(queryWrapper);
}
sql打印:
代码使用:
//查询指定栏目下所有,并按访问量和创建时间排序
public List<Article> searchByCondition() {
LambdaQueryWrapper<Article> queryWrapper = new LambdaQueryWrapper<>();
queryWrapper.eq(Article::getCatId,20);
//查询指定字段
queryWrapper.select(Article::getAuthor,Article::getCode,Article::getTitle,Article::getVisits);
//按访问量和创建时间排序
queryWrapper.orderByDesc(Article::getVisits).orderByAsc(Article::getCreateTime);
return articleMapper.selectList(queryWrapper);
}
sql打印:
版权说明 : 本文为转载文章, 版权归原作者所有 版权申明
原文链接 : https://lebron.blog.csdn.net/article/details/124956373
内容来源于网络,如有侵权,请联系作者删除!