【引言】
使用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”) |
【示例】
之前《Mybatis-Plus 3.X 條件查詢》博客中,也寫過一些查詢的使用,下面總結幾個其中沒有提到的,使用的mybatis-plus版本是3.1.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打印: