当前位置 博文首页 > ddd295569371的博客:Mybatis-plus使用QueryWrapper
按照惯例先奉上mybatis plus官方文档的链接https://mp.baomidou.com/guide/,有精力的可以进去学习一下。
queryWrapper是mybatis plus中实现查询的对象封装操作类
先看一下QueryWrapper的方法
函数名 | 说明 | 例子 |
---|---|---|
eq | 等于 = | 例:eq(“name”,“张三”) :name = ‘张三’ |
ne | 不等于<> | 例: eq(“name”,“老王”)—>name = ‘老王’ |
gt | 大于> | 例:gt(“age”,18)—>age > 18 |
ge | 大于等于>= | 例:ge(“age”,18)—>age >= 18 |
lt | 小于< | 例:lt(“age”,18)—>age < 18 |
le | 小于<= | 例:le(“age”,18)—>age <= 18 |
between | BETWEEN值1 AND值2 | 例:between(“age”,18,30)—>age between 18 and 30 |
notBetween | NOT BETWEEN值1 AND值2 | 例: notBetween(“age”,18,30)—>age not between 18 and 30 |
like | LIKE ‘%值%’ | 例: like(“name”,“王”)—–>name like '%王%’ |
notLike | NOT LIKE ‘%值%’ | 例: notLike (“name”,“王”)—>name not like '%王%’ |
likeLeft | LIKE '%值’ | 例:likeLeft (“name”,“王”)—–>name like '%王’ |
likeRight | LIKE’值%’ | 例: likeRight(“name”,“王”)—>name like ‘王%’ |
isNull | 字段IS NULL | 例: isNul1 (“name”)—>name is null |
isNotNull | 字段IS NOT NULL | 例: isNotNull(“name”)—>name is not null |
in | 字段IN (v0, v1,…) | 例: in(“age”,{1,2,3} )—–>age in (1,2,3) |
notIn | 字段NOT IN (v0, v1,…) | 例: notIn(“age”,1,2,3)—>age not in (1,2,3) |
inSql | 字段IN ( sql语句) | inSql(“id”, “select id from table where id < 3”) —–>id in (select id from table where id < 3) |
notInSql | 字段NOT IN ( sql语句) | notInSql(“id”, “select id from table where id < 3”) —–>age not in (select id from table where id < 3) |
groupBy | 分组:GROUP BY 字段,… | 例: groupBy(“id”, “name”)—>group by id, name |
orderByAsc | 排序:ORDER BY字段,… ASC | 例: orderByAsc(“id”, “name”)—>order by id ASC, name ASC |
orderByDesc | 排序:ORDER BY 字段,…DESC | 例: orderByDesc(“id”, “name”)—>order by id DESC, name DESC |
orderBy | 排序:ORDER BY字段,… | 例: orderBy(true,true,“id”,“name”)—–>order by id ASC, name ASC |
having | HAVING ( sql语句) | having(“sum(age) >{0}”,11)—>having sum(age) > 11 |
or | 拼接OR | 主动调用or表示紧接着下一个方法不是用and连接!(不调用or则默认为使用and连接)例:eq(“id”,1).or().eq(“name”,“老王”)—>id = 1 or name = '老王 |
and | AND嵌套 | 例: and(i -> i.eq(“name”,“李白”).ne(“status”,“活着”))—>and (name ='李白’ and status ’活着’) |
apply | 拼接sql | 该方法可用于数据库函数动态入参的params对应前面sqlHaving内部的{index}部分.这样是不会有sql注入风险的,反之会有! 例: apply(“date_format(dateColumn, ‘%Y一%m-%d’) ={0}”, “2008-08-08”)—>date_format(dateColumn,’%Y一%m-%d’) = ‘2008-08-08’") |
last | 无视优化规则直接拼接到sql 的最后 | 无视优化规则直接拼接到sql 的最后只能调用一次,多次调用以最后一次为准有sql注入的风险,请谨慎使用例: last(“limit 1”) |
exists | 拼接EXISTS ( sql语句) | —>exists (select id from table where age = 1)例: notExists(“select id from table where age = 1”) —>exists (select id from table where age = 1) |
notExists | 拼接NOT EXISTS ( sql语句) | 例: notExists(“select id from table where age = 1”)—–>not exists (select id from table where age = 1) |
nested | 正常嵌套不带AND或者 OR | 正常嵌套不带AND或者OR例: nested(i -> i.eq(“name”,“李白”).ne(“status”,“活着”))—>(name = '李白’and status 活着’) |
使用时先实例化QueryWrapper类,得到对象
QueryWrapper<Entity> queryWrapper = new QueryWrapper<>();
/**
*查询entity实体类对应的test_01字段为‘张三’
*如:select * from tableName where test_01 = '张三'
*/
queryWrapper.eq("test_01","张三");
// 查询 fillingcard_date 字段中时间小于等于 endDate 的时间
queryWrapper.le("endTime",endDate);
// 查询 fillingcard_date 字段中大于等于 startDate 的时间
queryWrapper.ge("startTime",startDate);
// 模糊查询 card_source 字段中 cardSource 的值
queryWrapper.like("card_source",cardSource);
----------------------------------------------
//以上queryWrapper的查询方法都是and条件关系