Mybatis-Plus条件参数查询手册

x33g5p2x  于2022-05-25 转载在 其他  
字(2.8k)|赞(0)|评价(0)|浏览(433)

【引言】

使用mybatis-plus框架的基础上,直接使用其中的条件参数进行查询还是很方便的。但每次使用到像大于、不等于这样一些不常用条件时,都需要现查,所以记录在这篇博客里,当作一个自己的查询手册。

【手册】

查询方式说明
select设置查询字段
andAND 语句,拼接 + AND (字段=值)
orOR 语句,拼接 + OR (字段=值)
eq等于=
allEq基于 map 内容等于=
ne不等于<>
gt大于>
ge大于等于>=
lt小于<
le小于等于<=
like模糊查询
notLike模糊查询 NOT LIKE
inIN 查询
notInNOT IN 查询
isNullNULL 值查询
isNotNullIS NOT NULL
groupBy分组 GROUP BY
havingHAVING 关键词
orderBy排序 ORDER BY
orderAscASC 排序 ORDER BY
orderDescDESC 排序 ORDER BY
existsEXISTS 条件语句
notExistsNOT EXISTS 条件语句
betweenBETWEEN 条件语句
notBetweenNOT BETWEEN 条件语句
last拼接在最后,例如:last(“LIMIT 1”)

【示例】

  • select查询指定字段

代码使用:

//查询作者和编码字段,返回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打印:

  • and和or:并且或者条件

代码使用:

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打印:

  • ge:大于等于条件

代码使用:

//查询条件:访问量大于等于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打印:

  • in:批量条件

代码使用:

//栏目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打印:

  • between:范围条件

代码使用:

//查询发布时间在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打印:

  • order:排序条件

代码使用:

//查询指定栏目下所有,并按访问量和创建时间排序
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打印:

相关文章