时间:2020-11-02 17:01:21 | 栏目:JAVA代码 | 点击:次
在Mybatis-Plus中,Wrapper接口的实现类关系如下:
可以看到,AbstractWrapper和AbstractChainWrapper是重点实现,接下来我们重点看下AbstractWrapper以及其子类。
说明:QueryWrapper(LambdaQueryWrapper) 和 UpdateWrapper(LambdaUpdateWrapper) 的父类 用于生成 sql的 where 条件, entity 属性也用于生成 sql 的 where 条件
注意:entity 生成的 where 条件与使用各个 api 生成的 where 条件 没有任何关联行为
文档地址:https://mybatis.plus/guide/wrapper.html
allEq
说明
全部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 = ‘老王'
个别参数说明: 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 = ‘老王'
测试用例
@Test public void testAllEq() { // 设置条件 Map<String, Object> params = new HashMap<>(); params.put("name", "李四"); params.put("age", "20"); params.put("password", null); QueryWrapper<User> wrapper = new QueryWrapper<>(); // SELECT id,user_name,name,age,email AS mail FROM tb_user WHERE password IS NULL AND name = ? AND age = ? // wrapper.allEq(params); // SELECT id,user_name,name,age,email AS mail FROM tb_user WHERE name = ? AND age = ? // wrapper.allEq(params, false); // SELECT id,user_name,name,age,email AS mail FROM tb_user WHERE age = ? // wrapper.allEq((k, v) -> (k.equals("age") || k.equals("id")), params); // SELECT id,user_name,name,age,email AS mail FROM tb_user WHERE name = ? AND age = ? wrapper.allEq((k, v) -> (k.equals("age") || k.equals("id") || k.equals("name")), params); List<User> users = this.userMapper.selectList(wrapper); for (User user : users) { System.out.println(user); } }
基本比较操作
测试用例
@Test public void testEq() { QueryWrapper<User> wrapper = new QueryWrapper<>(); // SELECT id,user_name,name,age,email AS mail 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 testLike() { QueryWrapper<User> wrapper = new QueryWrapper<>(); // SELECT id,user_name,name,age,email AS mail FROM tb_user WHERE name LIKE ? // 参数:%五(String) wrapper.likeLeft("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 testOrderByAgeDesc() { QueryWrapper<User> wrapper = new QueryWrapper<>(); // 按照年龄倒序排序 // SELECT id,user_name,name,age,email AS mail 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
拼接 OR
主动调用 or 表示紧接着下一个方法不是用 and 连接!(不调用 or 则默认为使用 and 连接)
and
AND 嵌套
例: and(i -> i.eq(“name”, “李白”).ne(“status”, “活着”)) ―> and (name = ‘李白' and status<> ‘活着')
测试用例
@Test public void testOr() { QueryWrapper<User> wrapper = new QueryWrapper<>(); // SELECT id,user_name,name,age,email AS mail FROM tb_user WHERE name = ? OR age = ? wrapper.eq("name", "王五").or().eq("age", 21); List<User> users = this.userMapper.selectList(wrapper); for (User user : users) { System.out.println(user); } }
select
在Mybatis-Plus查询中,默认查询所有的字段,如果有需要也可以通过select方法进行指定字段。
@Test public void testSelect() { QueryWrapper<User> wrapper = new QueryWrapper<>(); // SELECT id,name,age FROM tb_user WHERE name = ? OR age = ? wrapper.eq("name", "王五").or().eq("age", 21).select("id", "name", "age"); // 指定查询的字段 List<User> users = this.userMapper.selectList(wrapper); for (User user : users) { System.out.println(user); } }
查询方式 | 说明 |
---|---|
setSqlSelect | 设置 SELECT 查询字段 |
where | WHERE 语句,拼接 + WHERE 条件 |
and | AND 语句,拼接 + AND 字段=值 |
andNew | AND 语句,拼接 + AND (字段=值) |
or | OR 语句,拼接 + OR 字段=值 |
orNew | OR 语句,拼接 + OR (字段=值) |
eq | 等于= |
allEq | 基于 map 内容等于= |
ne | 不等于<> |
gt | 大于> |
ge | 大于等于>= |
lt | 小于< |
le | 小于等于<= |
like | 模糊查询 LIKE |
notLike | 模糊查询 NOT LIKE |
in | IN 查询 |
notIn | NOT IN 查询 |
isNull | NULL 值查询 |
isNotNull | IS NOT NULL |
groupBy | 分组 GROUP BY |
having | HAVING 关键词 |
orderBy | 排序 ORDER BY |
orderAsc | ASC 排序 ORDER BY |
orderDesc | DESC 排序 ORDER BY |
exists | EXISTS 条件语句 |
notExists | NOT EXISTS 条件语句 |
between | BETWEEN 条件语句 |
notBetween | NOT BETWEEN 条件语句 |
addFilter | 自由拼接 SQL |
last | 拼接在最后,例如:last(“LIMIT 1”) |