https://www.bilibili.com/video/BV1rE41197jR
p33~39
Wrapper接口实现类重点有AbstractWrapper和AbstractChainWrapper
区别在于AbstractChainWrapper采用lambda表达式编写
使用
以allEq为例
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| 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);
List<User> users = this.userMapper.selectList(wrapper); for (User user : users) { System.out.println(user); }
|
这样生成的sql语句为
1
| SELECT * FROM tb_user WHERE password IS NULL AND name = ? AND age = ?
|
第二个参数为false时忽略值为null的
1
| wrapper.allEq(params,false);
|
生成的sql语句为
1
| SELECT * FROM tb_user WHERE name = ? AND age = ?
|
还可以设置过滤器
基本比较操作
官方文档
模糊查询
like、notLike、likeLeft、likeRight
例如
1 2 3 4 5 6 7 8
| QueryWrapper<User> wrapper = new QueryWrapper<>();
wrapper.like("name", "曹");
List<User> users = this.userMapper.selectList(wrapper); for (User user : users) { System.out.println(user); }
|
排序
orderBy(默认Asc)、orderByAsc、orderByDesc
例如
1 2 3 4 5 6 7 8 9
| QueryWrapper<User> wrapper = new QueryWrapper<>();
wrapper.orderByDesc("age");
List<User> users = this.userMapper.selectList(wrapper); for (User user : users) { System.out.println(user); }
|
逻辑查询
or、and
例如
1 2 3 4 5 6 7 8 9
| QueryWrapper<User> wrapper = new QueryWrapper<>();
wrapper.eq("name","李四").or().eq("age", 24);
List<User> users = this.userMapper.selectList(wrapper); for (User user : users) { System.out.println(user); }
|
指定查询字段
select
例如
1 2 3 4 5 6 7 8 9 10 11 12
| QueryWrapper<User> wrapper = new QueryWrapper<>();
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); }
|