一、wapper介绍

image.png
Wrapper : 条件构造抽象类,最顶端父类
AbstractWrapper : 用于查询条件封装,生成 sql 的 where 条件
QueryWrapper : Entity 对象封装操作类,不是用lambda语法
UpdateWrapper : Update 条件封装,用于Entity对象更新操作
AbstractLambdaWrapper : Lambda 语法使用 Wrapper统一处理解析 lambda 获取 column。
LambdaQueryWrapper :看名称也能明白就是用于Lambda语法使用的查询Wrapper
LambdaUpdateWrapper : Lambda 更新封装Wrapper

  1. @RunWith(SpringRunner.class)
  2. @SpringBootTest
  3. public class QueryWrapperTests {
  4. @Autowired
  5. private UserMapper userMapper;
  6. }

二、AbstractWrapper

注意:以下条件构造器的方法入参中的 column 均表示数据库字段

1、ge、gt、le、lt、isNull、isNotNull

  1. @Test
  2. public void testDelete() {
  3. QueryWrapper<User> queryWrapper = new QueryWrapper<>();
  4. queryWrapper
  5. .isNull("name")
  6. .ge("age", 12)
  7. .isNotNull("email");
  8. int result = userMapper.delete(queryWrapper);
  9. System.out.println("delete return count = " + result);
  10. }

SQL:UPDATE user SET deleted=1 WHERE deleted=0 AND name IS NULL AND age >= ? AND email IS NOT NULL

2、eq、ne

注意:seletOne返回的是一条实体记录,当出现多条时会报错

  1. @Test
  2. public void testSelectOne() {
  3. QueryWrapper<User> queryWrapper = new QueryWrapper<>();
  4. queryWrapper.eq("name", "Tom");
  5. User user = userMapper.selectOne(queryWrapper);
  6. System.out.println(user);
  7. }

SELECT id,name,age,email,create_time,update_time,deleted,version FROM user WHERE deleted=0 AND name = ?

3、between、notBetween

包含大小边界

  1. @Test
  2. public void testSelectCount() {
  3. QueryWrapper<User> queryWrapper = new QueryWrapper<>();
  4. queryWrapper.between("age", 20, 30);
  5. Integer count = userMapper.selectCount(queryWrapper);
  6. System.out.println(count);
  7. }

SELECT COUNT(1) FROM user WHERE deleted=0 AND age BETWEEN ? AND ?

4、allEq

  1. @Test
  2. public void testSelectList() {
  3. QueryWrapper<User> queryWrapper = new QueryWrapper<>();
  4. Map<String, Object> map = new HashMap<>();
  5. map.put("id", 2);
  6. map.put("name", "Jack");
  7. map.put("age", 20);
  8. queryWrapper.allEq(map);
  9. List<User> users = userMapper.selectList(queryWrapper);
  10. users.forEach(System.out::println);
  11. }

SELECT id,name,age,email,create_time,update_time,deleted,version FROM user WHERE deleted=0 AND name = ? AND id = ? AND age = ?

5、like、notLike、likeLeft、likeRight

selectMaps返回Map集合列表

  1. @Test
  2. public void testSelectMaps() {
  3. QueryWrapper<User> queryWrapper = new QueryWrapper<>();
  4. queryWrapper
  5. .notLike("name", "e")
  6. .likeRight("email", "t");
  7. List<Map<String, Object>> maps = userMapper.selectMaps(queryWrapper);//返回值是Map列表
  8. maps.forEach(System.out::println);
  9. }

SELECT id,name,age,email,create_time,update_time,deleted,version FROM user WHERE deleted=0 AND name NOT LIKE ? AND email LIKE ?

6、in、notIn、inSql、notinSql、exists、notExists

in、notIn:

  • notIn(“age”,{1,2,3})—->age not in (1,2,3)
  • notIn(“age”, 1, 2, 3)—->age not in (1,2,3)

inSql、notinSql:可以实现子查询

  • 例: 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)
  1. @Test
  2. public void testSelectObjs() {
  3. QueryWrapper<User> queryWrapper = new QueryWrapper<>();
  4. //queryWrapper.in("id", 1, 2, 3);
  5. queryWrapper.inSql("id", "select id from user where id < 3");
  6. List<Object> objects = userMapper.selectObjs(queryWrapper);//返回值是Object列表
  7. objects.forEach(System.out::println);
  8. }

SELECT id,name,age,email,create_time,update_time,deleted,version FROM user WHERE deleted=0 AND id IN (select id from user where id < 3)

7、or、and

注意:这里使用的是 UpdateWrapper
不调用or则默认为使用 and 连

  1. @Test
  2. public void testUpdate1() {
  3. //修改值
  4. User user = new User();
  5. user.setAge(99);
  6. user.setName("Andy");
  7. //修改条件
  8. UpdateWrapper<User> userUpdateWrapper = new UpdateWrapper<>();
  9. userUpdateWrapper
  10. .like("name", "h")
  11. .or()
  12. .between("age", 20, 30);
  13. int result = userMapper.update(user, userUpdateWrapper);
  14. System.out.println(result);
  15. }

UPDATE user SET name=?, age=?, update_time=? WHERE deleted=0 AND name LIKE ? OR age BETWEEN ? AND ?

8、嵌套or、嵌套and

这里使用了lambda表达式,or中的表达式最后翻译成sql时会被加上圆括号

  1. @Test
  2. public void testUpdate2() {
  3. //修改值
  4. User user = new User();
  5. user.setAge(99);
  6. user.setName("Andy");
  7. //修改条件
  8. UpdateWrapper<User> userUpdateWrapper = new UpdateWrapper<>();
  9. userUpdateWrapper
  10. .like("name", "h")
  11. .or(i -> i.eq("name", "李白").ne("age", 20));
  12. int result = userMapper.update(user, userUpdateWrapper);
  13. System.out.println(result);
  14. }

UPDATE user SET name=?, age=?, update_time=? WHERE deleted=0 AND name LIKE ? OR ( name = ? AND age <> ? )

9、orderBy、orderByDesc、orderByAsc

  1. @Test
  2. public void testSelectListOrderBy() {
  3. QueryWrapper<User> queryWrapper = new QueryWrapper<>();
  4. queryWrapper.orderByDesc("id");
  5. List<User> users = userMapper.selectList(queryWrapper);
  6. users.forEach(System.out::println);
  7. }

SELECT id,name,age,email,create_time,update_time,deleted,version FROM user WHERE deleted=0 ORDER BY id DESC

10、last

直接拼接到 sql 的最后

注意:只能调用一次,多次调用以最后一次为准 有sql注入的风险,请谨慎使用

  1. @Test
  2. public void testSelectListLast() {
  3. QueryWrapper<User> queryWrapper = new QueryWrapper<>();
  4. queryWrapper.last("limit 1");
  5. List<User> users = userMapper.selectList(queryWrapper);
  6. users.forEach(System.out::println);
  7. }

SELECT id,name,age,email,create_time,update_time,deleted,version FROM user WHERE deleted=0 limit 1

11、指定要查询的列

  1. @Test
  2. public void testSelectListColumn() {
  3. QueryWrapper<User> queryWrapper = new QueryWrapper<>();
  4. queryWrapper.select("id", "name", "age");
  5. List<User> users = userMapper.selectList(queryWrapper);
  6. users.forEach(System.out::println);
  7. }

SELECT id,name,age FROM user WHERE deleted=0

12、set、setSql

最终的sql会合并 user.setAge(),以及 userUpdateWrapper.set() 和 setSql() 中 的字段

  1. @Test
  2. public void testUpdateSet() {
  3. //修改值
  4. User user = new User();
  5. user.setAge(99);
  6. //修改条件
  7. UpdateWrapper<User> userUpdateWrapper = new UpdateWrapper<>();
  8. userUpdateWrapper
  9. .like("name", "h")
  10. .set("name", "老李头")//除了可以查询还可以使用set设置修改的字段
  11. .setSql(" email = '123@qq.com'");//可以有子查询
  12. int result = userMapper.update(user, userUpdateWrapper);
  13. }

UPDATE user SET age=?, update_time=?, name=?, email = ‘123@qq.com’ WHERE deleted=0 AND name LIKE ?