MybatisPlus高级查询QueryWrapper常用方法

QueryWrapper常用方法列表

函数 意义 举例
eq 等于(=) queryWrapper.eq(“user_name”, “张三”); //user_name=“张三”
ne 不等于(<>) queryWrapper.ne(“user_name”, “张三”); //user_name<>“张三”
gt 大于(>) queryWrapper.gt(“level”, 1); //level>1
ge 大于等于(>=) queryWrapper.ge(“level”, 1); //level>=1
lt 小于(<) queryWrapper.lt(“level”, 1); //level<1
le 小于等于(<=) queryWrapper.le(“level”, 1); //level<=1
between 值1和值2之间 queryWrapper.between(“level”, 1,10); //level>=1 and level <=10
notBetween 不在值1和值2之间 queryWrapper.notBetween(“level”, 1,10);//level<1 or level >10
like 模糊匹配 like %值% queryWrapper.like(“user_name”, “张三”); //user_name like “%张三%”
not like 不模糊匹配 not like %值% queryWrapper.notLike(“user_name”, “张三”); //user_name not like “%张三%”
likeLeft 左模糊匹配 like %值 queryWrapper.likeLeft(“user_name”, “张三”); //user_name like “%张三”
likeRight 右模糊匹配 like 值% queryWrapper.likeRight(“user_name”, “张三”); //user_name like “张三%”
isNull 字段 为空 queryWrapper.isNull(“user_name”);//user_name is null
isNotNull 字段 不为空 queryWrapper.isNotNull(“user_name”);//user_name is not null
in 字段 in (v0,v1,…) queryWrapper.in(“user_name”, {“张三”,“李四”,“王五”}); // user_name in (“张三”,“李四”,“王五”)
notIn 字段 not in (v0,vl,…) queryWrapper.notIn(“user_name”, {“张三”,“李四”,“王五”}); // user_name not in (“张三”,“李四”,“王五”)
inSql 字段 in ( sql语句) queryWrapper.in(“user_name”, (select name from student where age< 23)); // user_name in (select name from student where age< 23)
notInSql 字段 not in ( sql语句) queryWrapper.notIn(“user_name”, (select name from student where age< 23)); // user_name not in (select name from student where age< 23)
groupBy 分组:GROUP BY 字段 queryWrapper.groupBy(“user_name”);//group by user_name
orderByAsc 排序:ORDER BY 字段 ASC queryWrapper.orderByAsc(“createTime”);//order by createTime asc
orderByDesc 排序:ORDER BY 字段 DESC queryWrapper.orderByDesc(“createTime”);//order by createTime desc
orderBy 排序:ORDER BY 字段 queryWrapper.orderBy(true, true, “createTime”);//order by createTime asc
or 拼接 OR 说明:主动调用or表示紧接着下一个方法不是用and连接!(不调用or则默认为使用and连接);queryWrapper.eq(“id”, “1”).or().eq(“user_name”, “张三”); //id = 1 or name =‘张三’
and 拼接 AND queryWrapper.eq(“id”, “1”).and(i->i.eq(“user_name”, “张三”)); //id = 1 and name =‘张三’
apply 拼接 sql 说明: 该方法可用于数据库函数动态入参的params;queryWrapper.eq(“id”, user.getId()).apply(“date_format(‘createTime’,‘%Y-%m-%d’) = {0}”,“2023-07-25”);//date_format(‘createTime’,'%Y-%m-%d) =‘2023-07-25’")
last 无视优化规则直接拼接到 sql 的最后 说明:只能调用一次,多次调用以最后一次为准 有sq1注入的风险,请谨慎使用queryWrapper.last(" limit 1");// limit 1
exists 拼接 EXISTS(sql语句) queryWrapper.exists(“SELECT id FROM Student WHERE User.id = Student.userId”);// exists (“SELECT id FROM Student WHERE User.id = Student.userId”)
notExists 拼接 NOT EXISTS (sg1语句) queryWrapper.notExists(“SELECT id FROM Student WHERE User.id = Student.userId”);// not exists (“SELECT id FROM Student WHERE User.id = Student.userId”)
nested 正常嵌套不带 AND或者 OR queryWrapper.nested(i->i.eq(“id”,“1”).eq(“user_name”,“张三”));// id = 1 and user_name=“张三”
having having ( sql语句) queryWrapper.having(“sum(age) > {0}”,10);//having sum(age) > 10;

常用的函数有eq等于like模糊查询between两值之间

如果是单创建QueryWrapper对象,最后查询想放在集合内的,可以调用mapper层继承的baseMapper内的17个操作方法之一selectList返回一个集合对象

public 类型 方法名(){
创建一个wrapper对象
给这个对象加上查询条件,在条件里写对字段的限制,就是上面的常用方法
返回basemapper里的查询方法selectList用这个查询方法查询wrapper对象
}

MybatisPlus高级查询QueryWrapper常用方法_第1张图片

你可能感兴趣的:(详细补充,java,算法,mybatis,sql)