MyBatisPlus之AbstractWrapper
说明:
QueryWrapper(LambdaQueryWrapper) 和 UpdateWrapper(LambdaUpdateWrapper) 的父类
用于生成 sql 的 where 条件, entity 属性也用于生成 sql 的 where 条件
注意: entity 生成的 where 条件与 使用各个 api 生成的 where 条件没有任何关联行为
allEq(Map<R, V> params) allEq(Map<R, V> params, boolean null2IsNull) allEq(boolean condition, Map<R, V> params, boolean null2IsNull)
个别参数说明:
params
: key
为数据库字段名,value
为字段值null2IsNull
: 为true
则在map
的value
为null
时调用 isNull 方法,为false
时则忽略value
为null
的
例1: allEq({id:1,name:"老王",age:null})--->id = 1 and name = '老王' and age is null 例2: allEq({id:1,name:"老王",age:null}, false)--->id = 1 and name = '老王'
allEq(BiPredicate<R, V> filter, Map<R, V> params) allEq(BiPredicate<R, V> filter, Map<R, V> params, boolean null2IsNull) allEq(boolean condition, BiPredicate<R, V> filter, Map<R, V> params, boolean null2IsNull)
个别参数说明:
filter
: 过滤函数,是否允许字段传入比对条件中params
与 null2IsNull
: 同上
例1: allEq((k,v) -> k.indexOf("a") >= 0, {id:1,name:"老王",age:null})--->name = '老王' and age is null 例2: allEq((k,v) -> k.indexOf("a") >= 0, {id:1,name:"老王",age:null}, false)--->name = '老王'
eq(R column, Object val) eq(boolean condition, R column, Object val)
eq("name", "老王")--->name = '老王'
ne(R column, Object val) ne(boolean condition, R column, Object val)
例: ne("name", "老王")--->name <> '老王'
gt(R column, Object val) gt(boolean condition, R column, Object val)
例: gt("age", 18)--->age > 18
ge(R column, Object val) ge(boolean condition, R column, Object val)
例: ge("age", 18)--->age >= 18
lt(R column, Object val) lt(boolean condition, R column, Object val)
例: lt("age", 18)--->age < 18
le(R column, Object val) le(boolean condition, R column, Object val)
例: le("age", 18)--->age <= 18
/** * 带条件的分页查询 * @param current * @param limit * @param teacherQuery * @return */ @Override public Page<EduTeacher> pageListTeacherCondition(long current, Long limit, TeacherQuery teacherQuery){ //创建Page对象 Page<EduTeacher> eduTeacherPage = new Page<>(current,limit); //构建条件 QueryWrapper<EduTeacher> wrapper = new QueryWrapper<>(); //获取传入讲师的条件是否为空 //讲师名 String name = teacherQuery.getName(); //讲师级别 Integer level = teacherQuery.getLevel(); //开始时间 String gmtCreate = teacherQuery.getGmtCreate(); //结束时间 String gmtModified = teacherQuery.getGmtModified(); //多条件组合查询 //判断条件值是否为空,如果不为空拼接条件 if (!StringUtils.isEmpty(name)){ //构建条件 模糊查询 wrapper.like("name",name); } if (!StringUtils.isEmpty(level)){ //等于 wrapper.eq("level",level); } if (!StringUtils.isEmpty(gmtCreate)){ //大于等于 wrapper.ge("gmt_create",gmtCreate); } if (!StringUtils.isEmpty(gmtModified)){ //小于等于 wrapper.le("gmt_modified",gmtModified); } //调用mybatis plus分页方法进行查询 eduTeacherMapper.selectPage(eduTeacherPage,wrapper); //通过Page对象获取分页信息 //long current = page.getCurrent();//当前页 List<EduTeacher> records = eduTeacherPage.getRecords(); //每页的数据 list集合 //long size = eduTeacherPage.getSize(); //每页显示的条数 long total = eduTeacherPage.getTotal(); //总记录数 long pages = eduTeacherPage.getPages(); //总页数 boolean pagehasNext = eduTeacherPage.hasNext(); //下一页 boolean pagehasPrevious = eduTeacherPage.hasPrevious(); //上一页 return eduTeacherPage; }
select * from user where id in [ 1,2,3] and (user_name like '%'+userName+'%' or address like '%'+address+'%';
EntityWrapper<userEntity> entityEntityWrapper = new EntityWrapper<>(); //在这里我们使用MP的selectPage方法去查,关于MP搭建可以去看之前的博文(关于mybatis-plus的) Page<userEntity> userList= qdDeviceService.selectPage(new Page<userEntity>(), //先来构建in的条件 entityEntityWrapper.in("device_id",ids) //之后以and连接并列的条件 后面更1=1 来保持成立的确定性 .and("1=1") //之后再以like来拼接你所需要的模糊查询 .like("user_name", (String) params.get("userName")) .like("address", (String) params.get("address")) ); //以上就是EntityWrapper的方式去查询
QueryWrapper<userEntity> userWrapper = new QueryWrapper<>(); String userName= (String) map.get("userName"); String address= (String) map.get("address"); userWrapper.and(wrapper -> wrapper.like("user_name", userName).or().like("address", address);
LambdaQueryWrapper<Article> queryWrapper = new LambdaQueryWrapper<>(); queryWrapper.eq(Article::getCatId,10); queryWrapper.and(x->x.like(Article::getKeywords,keywords).or().like(Article::getTitle,keywords)); queryWrapper.select(Article::getAuthor,Article::getCode,Article::getChildTitle,Article::getTitle); List<Article> articles = articleMapper.selectList(queryWrapper); articles.forEach(x-> System.out.println("文章关键字:" + x.getKeywords() + ",文章标题:" + x.getTitle()));