一、insert

  1. User user = new User();
  2. user.setId(1);
  3. user.setAge(18);
  4. int rows = userMapper.insert(user);

二、select

1、普通查询

  1. User user = userMapper.selectById(11);

2、批量查询

  1. List<Long> ids = Arrays.asList(123L,124L,125L);
  2. List<User> userList = userMapper.selectBatchIds(ids);

3、名字包含小雨并且年龄小于30

  1. QueryWrapper<User> queryWrapper = new QueryWrapper<User>();
  2. queryWrapper.like("username","小雨").lt("age",30);
  3. List<User> userList = userMapper.selectList(queryWrapper);

4、名字包含小雨并且年龄大于等于20且小于等于40并且email不为空

  1. QueryWrapper<User> queryWrapper = new QueryWrapper<User>();
  2. queryWrapper.like("username","小雨").between("age",20,30).isNotNull("email");
  3. List<User> userList = userMapper.selectList(queryWrapper);

5、名字姓肖或者年龄大雨等于20,按照年龄降序排列,年龄相同按照id生序排列

  1. QueryWrapper<User> queryWrapper = new QueryWrapper<User>();
  2. queryWrapper.likeRight("username","肖")
  3. .or().ge("age",20).orderByDesc("age").orderByAsc("id");
  4. List<User> userInfoList = userMapper.selectList(queryWrapper);

6、创建日期为2019年10月2日并且直属上级名字为王姓

  1. QueryWrapper<User> queryWrapper = new QueryWrapper<User>();
  2. queryWrapper.apply("date_format(create_time,'%Y-%m-%d')={0}","2019-10-07")
  3. .inSql("parent_id","select id from user where username like '王%'");
  4. List<User> userList = userMapper.selectList(queryWrapper);

7、只取指定的字段

  1. QueryWrapper<User> queryWrapper = new QueryWrapper<User>();
  2. queryWrapper.select("id","username");
  3. List<User> userList = userMapper.selectList(queryWrapper);

8、只获取一条、返回数量

  1. QueryWrapper<User> queryWrapper = new QueryWrapper<User>();
  2. queryWrapper.like("name","小雨").lt("age",30);
  3. User user = userMapper.selectOne(queryWrapper);
  4. int count = userMapper.selectCount(queryWrapper);

三、update

1、根据id进行更新

  1. //把id为1的用户年龄更新为18,其他属性不变
  2. User user = new User();
  3. user.setId(1);
  4. user.setAge(18);
  5. int rows = userMapper.updateById(user);

2、条件构造器作为参数进行更新

  1. //把名字为rhb的用户年龄更新为18,其他属性不变
  2. UpdateWrapper<User> updateWrapper = new UpdateWrapper<>();
  3. updateWrapper.eq("name","rhb");
  4. User user = new User();
  5. user.setAge(18);
  6. int rows = userMapper.update(user, updateWrapper);

3、lambda构造器

  1. //把名字为rhb的用户年龄更新为18,其他属性不变
  2. LambdaUpdateWrapper<User> lambdaUpdateWrapper = new LambdaUpdateWrapper<>();
  3. lambdaUpdateWrapper.eq(User::getName, "rhb").set(User::getAge, 18);
  4. int rows = userMapper.update(null, lambdaUpdateWrapper);

四、delete

1、根据id进行删除

  1. int rows = userMapper.deleteById(1351456313578713090L);

2、根据map删除

  1. Map<String, Object> map = new HashMap<>();
  2. map.put("name", "张三");
  3. map.put("age", 25);
  4. int rows = userMapper.deleteByMap(map);

3、以条件构造器为参数删除

  1. QueryWrapper wrapper = new QueryWrapper();
  2. wrapper.eq("name","张三");
  3. wrapper.eq("age",25);
  4. int rows = userMapper.delete(wrapper);
  1. LambdaQueryWrapper<User> wrapper = new LambdaQueryWrapper();
  2. wrapper.eq(User::getName,"张三").eq(User::getAge,25);
  3. int rows = userMapper.delete(wrapper);

五、LambdaQueryWrapper条件构造器

  1. setSqlSelect SELECT 查询字段 举例
  2. where WHERE 语句,拼接 + WHERE 条件
  3. and AND 语句,拼接 + AND 字段=值
  4. andNew AND 语句,拼接 + AND (字段=值)
  5. or OR 语句,拼接 + OR 字段=值 .eq(" name “,” 木 子 “). or ( ). eq (” id ", 1 ) => name=“木子” or id=1 注意事项:主动调用or表示下一个方法不是and连接,不调用默认为使用and连接
  6. orNew OR 语句,拼接 + OR (字段=值)
  7. eq 等于= .eq(“name”,“木子”) => name=“木子”
  8. allEq 基于 map 内容等于=
  9. ne 不等于<>
  10. gt 大于>
  11. ge 大于等于>=
  12. lt 小于<
  13. le 小于等于<=
  14. like 模糊查询 LIKE .like(“name”,“木子”) => name like ‘%木子%’
  15. notLike NOT LIKE模糊查询
  16. in IN 查询 .in(“age”,{1,2,3}) => age in (1,2,3)
  17. notIn NOT IN 查询
  18. isNull NULL 值查询
  19. isNotNull IS NOT NULL
  20. groupBy 分组 GROUP BY
  21. having HAVING 关键词
  22. orderBy 排序 ORDER BY
  23. orderAsc Asc 排序 ORDER BY
  24. orderDesc DESC 排序 ORDER BY
  25. exists EXISTS 条件语句
  26. notExists NOT EXISTS 条件语句
  27. between BETWEEN 条件语句 .between (“age”,1,2) => age between 1 and 2
  28. notBetween NOT BETWEEN 条件语句
  29. addFilter 自由拼接 SQL
  30. last 拼接在最后,例如:last(“LIMIT 1”)