Mybatis PLUS查询对List使用OR模糊查询
- 1、版本
- 2、代码
- 3、效果
1、版本
Mybatis PLUS版本:3.5.7
注意:版本3.1.2及以下是需要return的
- 因当前为高版本,代码中已将 return 注释。
2、代码
java">QueryWrapper<Object> queryWrapper = new QueryWrapper<>();
List<String> values = Arrays.asList("1","2");
queryWrapper.and(values != null && values.size() > 0, qw -> {values.forEach(var -> {if(var != null && !"".equals(var)){qw.or(sub -> sub.like("id", var));}});//return qw;
});
如需要封装,代码如下:
java">package com;import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.support.SFunction;
import java.util.List;public class MyLambdaQueryWrapper<T> extends LambdaQueryWrapper<T> {public MyLambdaQueryWrapper<T> andOrLikePresent(SFunction<T, ?> column, List<?> values) {if(null == values){return this;}super.and(values != null && values.size() > 0, qw -> {values.forEach(var -> {if(var != null && !"".equals(var)){qw.or(sub -> sub.like(column, var));}});//return qw;});return this;}}
使用如下:
java">MyLambdaQueryWrapper<Object> wrapper = new MyLambdaQueryWrapper<>();
wrapper.andOrLikePresent(Object::getID, Arrays.asList("1","2"));
- 当前版本Maven配置
java"><!-- mybatisPlus -->
<dependency><groupId>com.baomidou</groupId><artifactId>mybatis-plus-spring-boot3-starter</artifactId><version>3.5.7</version>
</dependency>
<!-- 代码生成器 -->
<dependency><groupId>com.baomidou</groupId><artifactId>mybatis-plus-generator</artifactId> <version>3.5.7</version>
</dependency>
<!-- MyBatis 联表查询 -->
<dependency><groupId>com.github.yulichang</groupId><artifactId>mybatis-plus-join-boot-starter</artifactId><version>1.4.10</version>
</dependency>
3、效果
SELECTidaccount,name,status
FROMt_user
WHEREstatus = '1'and ((( name like '%张三%')or ( name like '%李四%')or ( name like '%王五%')));
多个List模糊查询
SELECTidaccount,name,status
FROMt_user
WHEREstatus = '1'and ((( account like '%zhang%')or ( account like '%li%')or ( account like '%wang%'))and (( name like '%张三%')or ( name like '%李四%')or ( name like '%王五%')));
OK,就这些吧。
有什么不对的还望指正,书写不易,觉得有帮助就点个赞吧!☺☺☺
参考链接:
https://www.cnblogs.com/lboke/p/17146689.html