Java – MybatisPlus – 条件构造器Wrapper
简介
MybatisPlus 提供了一种相对比较方便安全的条件组合包装类Wrapper。
allEq
allEq 提供了几种方法来拼接多个条件相等的查询条件。
allEq(Map<R, V> params)
allEq(Map<R, V> params, boolean null2IsNull)
allEq(boolean condition, Map<R, V> params, boolean null2IsNull)
allEq(BiPredicate<R, V> filter, Map<R, V> params)
allEq(BiPredicate<R, V> filter, Map<R, V> params, boolean null2IsNull)
allEq(boolean condition, BiPredicate<R, V> filter, Map<R, V> params, booleannull2IsNull)
params
: 使用 Map 集合对条件做相等判断,其key则是字段名,value则是字段值,多个集合成员用【AND】
拼接
null2IsNull
: 当集合成员中的value是【NULL】
时,MybatisPlus 默认会拼接【key IS NULL】
的SQL语句,如果设置为false
,则表示 NULL
值不参与SQL拼接查询。
个别参数说明: params : key
为数据库字段名, value
为字段值 null2IsNull
: 为true
则在 map 的value
为null
时调用 isNull
方法,为false
时则忽略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 = '老王'
BiPredicate
: BiPredicate 是JDK8 提供的一种过滤接口,可以自定义过滤Map集合,allEq 中可以通过配置 BiPredicate 来过滤 params 中那些成员允许拼接SQL语句。
例1: allEq((k,v) -> k.equals("name") || k.equals("age") > 0, {id:1,name:"老王",age:null}) ---> name = '老王' and age is null
例2: allEq((k,v) -> k.equals("id"), {id:1,name:"老王",age:null}, false) ---> id ='1'
@Test
public void testWrapper() {
QueryWrapper<User> wrapper = new QueryWrapper<>();
//设置条件
Map<String, Object> params = new HashMap<>();
params.put("name", "曹操");
params.put("age", "20");
params.put("password", null);
// wrapper.allEq(params);//SELECT * FROM tb_user WHERE password IS NULL AND name = ?AND age = ?
// wrapper.allEq(params,false); //SELECT * FROM tb_user WHERE name = ? AND age = ?
// wrapper.allEq((k, v) -> (k.equals("name") || k.equals("age")) ,params);//SELECT * FROM tb_user WHERE name = ? AND age = ?
List<User> users = this.userMapper.selectList(wrapper);
for (User user : users) {
System.out.println(user);
}
}
基本比较操作
eq
等于 =
ne
不等于 <>
gt
大于 >
ge
大于等于 >=
lt
小于 <
le
小于等于 <=
between
BETWEEN 值1 AND 值2
notBetween
NOT BETWEEN 值1 AND 值2
in
字段 IN (value.get(0), value.get(1), ...)
notIn
字段 NOT IN (v0, v1, ...)
举例:
@Test
public void testEq() {
QueryWrapper<User> wrapper = new QueryWrapper<>();
//SELECT id,user_name,password,name,age,email FROM tb_user WHERE password = ? AND age >= ? AND name IN (?,?,?)
wrapper.eq("password", "123456")
.ge("age", 20)
.in("name", "李四", "王五", "赵六");
List<User> users = this.userMapper.selectList(wrapper);
for (User user : users) {
System.out.println(user);
}
}
模糊查询
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 '王%'
举例
@Test
public void testWrapper() {
QueryWrapper<User> wrapper = new QueryWrapper<>();
//SELECT id,user_name,password,name,age,email FROM tb_user WHERE name LIKE ?
//Parameters: %曹%(String)
wrapper.like("name", "曹");
List<User> users = this.userMapper.selectList(wrapper);
for (User user : users) {
System.out.println(user);
}
}
排序
orderBy
排序:ORDER BY 字段, ...
例: orderBy(true, true, "id", "name") ---> order by id ASC,name ASC
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
举例:
@Test
public void testWrapper() {
QueryWrapper<User> wrapper = new QueryWrapper<>();
//SELECT id,user_name,password,name,age,email FROM tb_user ORDER BY age DESC
wrapper.orderByDesc("age");
List<User> users = this.userMapper.selectList(wrapper);
for (User user : users) {
System.out.println(user);
}
}
逻辑查询(or和and)
or
拼接 OR
主动调用or 表示紧接着下一个方法不是用and 连接!(不调用or 则默认为使用and 连接)
and
AND 嵌套
例: and(i -> i.eq("name", "李白").ne("status", "活着")) ---> and (name = '李白' and status<> '活着')
举例:
@Test
public void testWrapper() {
QueryWrapper<User> wrapper = new QueryWrapper<>();
//SELECT id,user_name,password,name,age,email FROM tb_user WHERE name = ? OR age = ?
wrapper.eq("name","李四").or().eq("age", 24);
List<User> users = this.userMapper.selectList(wrapper);
for (User user : users) {
System.out.println(user);
}
}
指定字段 select
在MybatisPlus查询中,默认查询所有的字段,如果有需要也可以通过select方法进行指定字段。
@Test
public void testWrapper() {
QueryWrapper<User> wrapper = new QueryWrapper<>();
//SELECT id,name,age FROM tb_user WHERE name = ? OR age = ?
wrapper.eq("name", "李四")
.or()
.eq("age", 24)
.select("id", "name", "age");
List<User> users = this.userMapper.selectList(wrapper);
for (User user : users) {
System.out.println(user);
}
}
共有 0 条评论