gt、ge、lt、le、isNull、isNotNull
- 大于 >
- 例:
gt("age", 18)
→age > 18
- ge 大于等于 >=
- 例:
ge("age", 18)
→age >= 18
- lt 小于 <
- 例:
lt("age", 18)
→age < 18
- le 小于等于 <=
- 例:
le("age", 18)
→age <= 18
- isNUll 字段 IS NULL
- 例:
isNull("name")
→name is null
- isNotNull 字段 IS NOT NULL
- 例:
isNotNull("name")
→name is not null
@Test public void queryWrapperOne() { QueryWrapper<User> queryWrapper = new QueryWrapper<>(); queryWrapper. isNull("name") .ge("age", 23) .isNotNull("email"); // 逻辑删除 int result = userMapper.delete(queryWrapper); System.out.println(result); // 最终的语句为:UPDATE user SET deleted=1 WHERE deleted=0 AND name IS NULL AND age >= ? AND email IS NOT NULL }
eq、ne
- eq 等于 =
- 例:
eq("name", "老王")
→name = '老王'
- ne 不等于 <>
- 例:
ne("name", "老王")
→name <> '老王'
@Test public void queryWrapperTwo() { QueryWrapper<User> queryWrapper = new QueryWrapper<>(); queryWrapper.eq("name", "BNTang"); // selectOne:返回的是一条记录,当出现多条时会报错 User user = userMapper.selectOne(queryWrapper); System.out.println(user); }
between、notBetween
- BETWEEN 值1 AND 值2
- 例:
between("age", 18, 30)
→age between 18 and 30
- NOT BETWEEN 值1 AND 值2
- 例:
notBetween("age", 18, 30)
→age not between 18 and 30
@Test public void queryWrapperThree() { QueryWrapper<User> queryWrapper = new QueryWrapper<>(); queryWrapper.between("age", 20, 30); Integer count = userMapper.selectCount(queryWrapper); System.out.println(count); }
allEq
- allEq 全部 eq(或个别 isNull)
个别参数说明:
params
:key
为数据库字段名,value
为字段值如果想表示 null isNull,则在 value 取值的时候传入一个 null, 为
null
时调用 isNull 方法, 为false
时则忽略value
为null
的,也就是不拼接 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 = '老王'
@Test public void queryWrapperFour() { QueryWrapper<User> queryWrapper = new QueryWrapper<>(); Map<String, Object> queryParamsMap = new HashMap<>(); queryParamsMap.put("id", 1373266771933462530L); queryParamsMap.put("name", "BNTang"); queryParamsMap.put("age", 23); queryWrapper.allEq(queryParamsMap); List<User> users = userMapper.selectList(queryWrapper); users.forEach(System.out::println); }
个别参数说明
filter
: 过滤函数, 是否允许字段传入比对条件中params
与null
、IsNull
同上
- 例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 = '老王'
如上图可看到,只添加了一个 id 查询的条件因为我写的过滤条件为要包含 i
这个字符串,所以只有第一个 key, value 包含了所以就只添加了 id
like、notLike、likeLeft、likeRight
- LIKE '%值%'
- 例:
like("name", "王")
→name like '%王%'
- NOT LIKE '%值%'
- 例:
notLike("name", "王")
→name not like '%王%'
- LIKE '%值'
- 例:
likeLeft("name", "王")
→name like '%王'
- LIKE '值%'
- 例:
likeRight("name", "王")
→name like '王%'
@Test public void queryWrapperFive() { QueryWrapper<User> queryWrapper = new QueryWrapper<>(); queryWrapper .notLike("name", "BNTang") .likeRight("email", "zq"); // 返回值是Map列表 List<Map<String, Object>> users = userMapper.selectMaps(queryWrapper); users.forEach(System.out::println); }
in、notIn、inSql、notinSql、exists、notExists
- 字段 IN (value.get(0), value.get(1), ...)
- 例:
in("age",{1,2,3})
→age in (1,2,3)
- 字段 NOT IN (value.get(0), value.get(1), ...)
- 例:
notIn("age",{1,2,3})
→age not in (1,2,3)
- 字段 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)
- 字段 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)
- 拼接 EXISTS ( sql语句 )
- 例:
exists("select id from table where age = 1")
→exists (select id from table where age = 1)
- 拼接 NOT EXISTS ( sql语句 )
- 例:
notExists("select id from table where age = 1")
→not exists (select id from table where age = 1)
@Test public void queryWrapperSix() { QueryWrapper<User> queryWrapper = new QueryWrapper<>(); queryWrapper.in("id", 1, 2, 3); queryWrapper.inSql("id", "select id from user where id < 3"); // 返回值是Object列表 List<Object> users = userMapper.selectObjs(queryWrapper); users.forEach(System.out::println); }
or、and
- 拼接 OR
注意事项:
主动调用
or
表示紧接着下一个方法不是用and
连接!(不调用or
则默认为使用and
连接)
- 例:
eq("id",1).or().eq("name","老王")
→id = 1 or name = '老王'
- OR 嵌套
- 例:
or(i -> i.eq("name", "李白").ne("status", "活着"))
→or (name = '李白' and status <> '活着')
- AND 嵌套
- 例:
and(i -> i.eq("name", "李白").ne("status", "活着"))
→and (name = '李白' and status <> '活着')
@Test public void queryWrapperSeven() { // 修改值 User user = new User(); user.setAge(99); user.setName("BNTang6666"); // 修改条件 UpdateWrapper<User> userUpdateWrapper = new UpdateWrapper<>(); userUpdateWrapper .like("name", "BNTang") .or() .between("age", 20, 30); int result = userMapper.update(user, userUpdateWrapper); System.out.println(result); }
嵌套 or、嵌套 and
java@Test public void queryWrapperEight() { // 修改值 User user = new User(); user.setAge(99); user.setName("BNTang6666"); // 修改条件 UpdateWrapper<User> userUpdateWrapper = new UpdateWrapper<>(); userUpdateWrapper .like("name", "A") .or(i -> i.eq("name", "BNTang6666").ne("age", 20)); int result = userMapper.update(user, userUpdateWrapper); System.out.println(result); }
orderBy、orderByDesc、orderByAsc
- 排序:ORDER BY 字段, ...
- 例:
orderBy(true, true, "id", "name")
→order by id ASC,name ASC
- 排序:ORDER BY 字段, ... DESC
- 例:
orderByDesc("id", "name")
→order by id DESC, name DESC
- 排序:ORDER BY 字段, ... ASC
- 例:
orderByAsc("id", "name")
→order by id ASC,name ASC
@Test public void queryWrapperNine() { QueryWrapper<User> queryWrapper = new QueryWrapper<>(); queryWrapper.orderByDesc("id"); List<User> users = userMapper.selectList(queryWrapper); users.forEach(System.out::println); }
last
- 无视优化规则直接拼接到 sql 的最后
注意事项:
只能调用一次, 多次调用以最后一次为准有 sql 注入的风险, 请谨慎使用
- 例:
last("limit 1")
@Test public void queryWrapperTen() { QueryWrapper<User> queryWrapper = new QueryWrapper<>(); queryWrapper.last("limit 1"); List<User> users = userMapper.selectList(queryWrapper); users.forEach(System.out::println); }
指定要查询的列
java@Test public void queryWrapperEleven() { QueryWrapper<User> queryWrapper = new QueryWrapper<>(); queryWrapper.select("id", "name", "age"); List<User> users = userMapper.selectList(queryWrapper); users.forEach(System.out::println); }
set、setSql
- SQL SET 字段
- 例:
set("name", "老李头")
- 例:
set("name", "")
→数据库字段值变为空字符串 - 例:
set("name", null)
→据库字段值变为null
- 设置 SET 部分 SQL
- 例:
setSql("name = '老李头'")
@Test public void queryWrapperTwelve() { // 修改值 User user = new User(); user.setAge(10); // 修改条件 UpdateWrapper<User> userUpdateWrapper = new UpdateWrapper<>(); userUpdateWrapper .like("name", "A") // 除了可以查询还可以使用set设置修改的字段 .set("name", "BNTang") // 可以有子查询 .setSql(" email = '303158131@qq.com'"); int result = userMapper.update(user, userUpdateWrapper); System.out.println(result); }