所有分类
  • 所有分类
  • 未分类

Mybatis-Plus-条件构造器的用法

简介

说明

本文介绍Mybatis-Plus的条件构造器。

官网网址

条件构造器 | MyBatis-Plus

大全

函数名说明示例
allEq全部eq(或个别isNull)例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 = ‘老王’
eq等于 =例eq(“name”, “老王”)—>name = ‘老王’
ne不等于 <>例: ne(“name”, “老王”)—>name <> ‘老王’
ge大于 >例: gt(“age”, 18)—>age > 18
ge大于等于 >=例: ge(“age”, 18)—>age >= 18
lt小于 <例: lt(“age”, 18)—>age < 18
le小于等于 <=例: le(“age”, 18)—>age <= 18
betweenBETWEEN 值1 AND 值2例: between(“age”, 18, 30)—>age between 18 and 30
notBetweenNOT BETWEEN 值1 AND 值2例: notBetween(“age”, 18, 30)—>age not between 18 and 30
likeLIKE ‘%值%’例: like(“name”, “王”)—>name like ‘%王%’
notLikeNOT LIKE ‘%值%’例: notLike(“name”, “王”)—>name not like ‘%王%’
likeLeftLIKE ‘%值’例: likeLeft(“name”, “王”)—>name like ‘%王’
likeRightLIKE ‘值%’例: 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 (v0, v1, …)例: in(“age”,{1,2,3})—>age in (1,2,3) 例: in(“age”, 1, 2, 3)—>age in (1,2,3)
notIn字段 NOT IN (value.get(0), value.get(1), …) 字段 NOT IN (v0, v1, …)例:  notIn(“age”,{1,2,3})—>age not in (1,2,3) 例: notIn(“age”, 1, 2, 3)—>age not in (1,2,3)
inSql字段 IN ( sql语句 )例: inSql(“age”, “1,2,3,4,5,6”)         —>age in (1,2,3,4,5,6) 例: inSql(“id”, “select id from table where id < 3”)         —>id in (select id from table where id < 3)
notInSql字段 NOT IN ( sql语句 )例: notInSql(“age”, “1,2,3,4,5,6”)         —>age not in (1,2,3,4,5,6) 例: notInSql(“id”, “select id from table where id < 3”)         —>id 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
havingHAVING ( sql语句 )例: having(“sum(age) > 10”)—>having sum(age) > 10 例: having(“sum(age) > {0}”, 11)—>having sum(age) > 11
funcfunc 方法(主要方便在出现if…else下调用不同方法能不断链)例: func(i -> if(true) {i.eq(“id”, 1)} else {i.ne(“id”, 1)})
or拼接 OR。 主动调用or表示紧接着下一个方法不是用and连接!(不调用or则默认为使用and连接)例: eq(“id”,1).or().eq(“name”,”老王”)         —>id = 1 or name = ‘老王’ 例: or(i -> i.eq(“name”, “李白”).ne(“status”, “活着”))         —>or (name = ‘李白’ and status <> ‘活着’)
andAND 嵌套例: and(i -> i.eq(“name”, “李白”).ne(“status”, “活着”))         —>and (name = ‘李白’ and status <> ‘活着’)
nested正常嵌套 不带 AND 或者 OR例: nested(i -> i.eq(“name”, “张三”).gt(“age”, 25))
        —>(name = ‘张三’ and age > 25) 例: .eq(“name”, “李四”)
.nested(i -> i.gt(“age”, 25).or().lt(“age”, 12))
.eq(“email”, “abc@qq.com”)
        —>name = ‘李四” AND (age > 25 OR age < 12) AND email = “abc@qq.com”
apply拼接 sql。 该方法可用于数据库函数动态入参的params对应前面applySql内部的{index}部分。这样是不会有sql注入风险的,反之会有!例: apply(“id = 1”)         —>id = 1 例: apply(“date_format(dateColumn,’%Y-%m-%d’) = ‘2008-08-08′”)         —>date_format(dateColumn,’%Y-%m-%d’) = ‘2008-08-08′”) 例: apply(“date_format(dateColumn,’%Y-%m-%d’) = {0}”, “2008-08-08″)         —>date_format(dateColumn,’%Y-%m-%d’) = ‘2008-08-08′”)
last无视优化规则直接拼接到 sql 的最后。 只能调用一次,多次调用以最后一次为准。有sql注入的风险,请谨慎使用例: last(“limit 1”)
exists拼接 EXISTS ( sql语句 )例: exists(“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)

Compare功能

com/baomidou/mybatisplus/core/conditions/interfaces/Compare.java

Func功能

com/baomidou/mybatisplus/core/conditions/interfaces/Func.java

nested

com/baomidou/mybatisplus/core/conditions/interfaces/Nested.java

join功能

com/baomidou/mybatisplus/core/conditions/interfaces/Join.java

0

评论0

请先

显示验证码
没有账号?注册  忘记密码?

社交账号快速登录