Mybatis-Plus条件构造器
在MP中,Wrapper接⼝的实现类关系如下:
在这里面,AbstractWrapper和AbstractChainWrapper是重点实现,重点看一下AbstractWrapper以及其⼦类。
说明:
QueryWrapper(LambdaQueryWrapper) 和 UpdateWrapper(LambdaUpdateWrapper) 的⽗类 ⽤于⽣成 sql 的 where 条件, entity 属性也⽤于⽣成 sql 的 where 条件
注意: entity ⽣成的 where 条件与 使⽤各个 api ⽣成的 where 条件没有任何关联⾏为
1、allEq
1.1、说明
allEq(Map<R, V> params)
allEq(Map<R, V> params, boolean null2IsNull)
allEq(boolean condition, Map<R, V> params, boolean null2IsNull)
全部eq(或个别isNull)
个别参数说明:
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 = ‘小七’
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, boolean null2IsNull)
个别参数说明:
filter : 过滤函数,是否允许字段传⼊⽐对条件中 params 与
null2IsNull : 同上
例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 = ‘小七’
1.2、测试⽤例
/*
测试条件构建器allEq
*/
@Test
public void testAllEq(){
QueryWrapper<User> queryWrapper = new QueryWrapper<>();
//构建Map
HashMap<String, Object> map = new HashMap<>();
map.put("name","jack");
map.put("age",null);
//WHERE name = ? AND age IS NULL
//queryWrapper.allEq(map);
//WHERE name = ?
//queryWrapper.allEq(map,false);
//SELECT * FROM user WHERE name = ? AND age = ?
wrapper.allEq((k, v) -> (k.equals("name") || k.equals("age")),params);
List<User> users = userMapper.selectList(queryWrapper);
for (User user : users) {
System.out.println(user);
}
}
2、基本⽐较操作
- 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 testWrapper(){
QueryWrapper<User> queryWrapper = new QueryWrapper<>();
//WHERE email = ? AND age >= ? AND name IN (?)
queryWrapper.eq("email","xiaoqi@163.com")
.ge("age",10)
.in("name","小七1");
List<User> users = userMapper.selectList(queryWrapper);
for (User user : users) {
System.out.println(user);
}
}
3、模糊查询
- 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 testLike(){
QueryWrapper<User> queryWrapper = new QueryWrapper<>();
queryWrapper.like("name","小");
List<User> users = userMapper.selectList(queryWrapper);
for (User user : users) {
System.out.println(user);
}
}
4、排序
- 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 testOrderBy(){
QueryWrapper<User> queryWrapper = new QueryWrapper<>();
queryWrapper.orderByDesc("age");
List<User> users = userMapper.selectList(queryWrapper);
for (User user : users) {
System.out.println(user);
}
}
5、逻辑查询
- or
- 拼接 OR
- 主动调⽤ or 表示紧接着下⼀个⽅法不是⽤ and 连接!(不调⽤ or 则默认为使⽤ and 连接)
- and
- AND 嵌套
- 例: and(i -> i.eq(“name”, “李⽩”).ne(“status”, “活着”)) —> and (name = ‘李⽩’ and status <> ‘活着’)
测试⽤例:
/*
逻辑查询
*/
@Test
public void testOr(){
QueryWrapper<User> queryWrapper = new QueryWrapper<>();
// WHERE name = ? OR name = ?
queryWrapper.eq("name","jack").or().eq("name","小七");
List<User> users = userMapper.selectList(queryWrapper);
for (User user : users) {
System.out.println(user);
}
}
6、select
在MP查询中,默认查询所有的字段,如果有需要也可以通过select⽅法进⾏指定字段。
/*
select查询
*/
@Test
public void testSlt(){
QueryWrapper<User> queryWrapper = new QueryWrapper<>();
queryWrapper.eq("name","jack").select("age");
List<User> users = userMapper.selectList(queryWrapper);
for (User user : users) {
System.out.println(user);
}
}