SpringBoot整合MybatisPlus有关条件构造器的相关查询方法

eq: 等于 =

例子:eq("name", "老王")--->name = '老王'

ne: 不等于 <>

例子:ne("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: 值 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

例子:isNull("name")--->name is null

isNotNull:字段 IS NOT NULL

例子:isNotNull("name")--->name is not null

in: 字段IN(value.get(0),value.get(1),...)

例子:in("age",{1,2,3})--->age in (1,2,3)

     字段 IN (v0, v1, ...)

例子:in("age", 1, 2, 3)--->age in (1,2,3)

notIn: 字段 IN (value.get(0), value.get(1), ...)

例子:notIn("age",{1,2,3})--->age not in (1,2,3)

           字段 NOT IN (v0, v1, ...)

例子:notIn("age", 1, 2, 3)--->age not in (1,2,3)

inSql: 字段 IN(SQL语句)

例子1:inSql("age", "1,2,3,4,5,6")--->age in (1,2,3,4,5,6)

例子2:inSql("id", "select id from table where id < 3")--->id in (select id from  table where id < 3)

notInSql: 字段NOT IN (sql语句)

例子1:notInSql("age", "1,2,3,4,5,6")--->age not in (1,2,3,4,5,6)

例子2:notInSql("id", "select id from table where id < 3")--->age not in (select id from table where id < 3)

分组:GROUP BY 字段,...

例子:groupBy("id", "name")--->group by id,name

升序: ORDER BY 字段,...ASC

例子:orderByAsc("id", "name")--->order by id ASC,name ASC

降序:ORDER BY 字段,...DESC

例子:orderByDesc("id", "name")--->order by id DESC,name DESC

排序:OREDR BY 字段,...

例子:orderBy(true, true, "id", "name")--->order by id ASC,name ASC

聚合函数作为条件HAVING (sql语句)

例子1:having("sum(age) > 10")--->having sum(age) > 10(有sql注入风险)

例子2:having("sum(age) > {0}", 11)--->having sum(age) > 11(推荐使用)

拼接 OR: or()

例子:eq("id",1).or().eq("name","老王")--->id = 1 or name = '老王'

注意事项:主动调用or表示紧接着下一个方法不是用and连接!(不调用or则默认为使用and连接)

OR 嵌套

例子:or(i -> i.eq("name", "李白").ne("status", "活着"))--->or (name = '李白' and status <> '活着')

AND 嵌套

例子:and(i -> i.eq("name", "李白").ne("status", "活着"))--->and (name = '李白' and status <> '活着')

nested:正常嵌套 不带AND 或者 OR

例子:nested(i -> i.eq("name", "李白").ne("status", "活着"))--->(name = '李白' and status <> '活着')

拼接 sql:apply

例子1:apply("id = 1")--->id = 1

例子2:apply("date_format(dateColumn,'%Y-%m-%d') = '2008-08-08'")

              --->date_format(dateColumn,'%Y-%m-%d') = '2008-08-08'")

例子3:apply("date_format(dateColumn,'%Y-%m-%d') = {0}", "2008-08-08")

             --->date_format(dateColumn,'%Y-%m-%d') = '2008-08-08'")

注意事项:该方法可用于数据库函数 动态入参的params对应前面applySql内部的{index}部分.这样是不会有sql注入风险的,反之会有!

无视优化规则直接拼接到 sql 的最后:last

例子:last("limit 1")

注意事项:只能调用一次,多次调用以最后一次为准 有sql注入的风险,请谨慎使用

exists:拼接EXISTS(sql语句)

例子:exists("select id from table where age = 1")--->exists (select id from table where age = 1)

参考文章:https://www.cnblogs.com/zhukf/p/12144072.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值