@Testpublicvoidtest(){// 查询用户名包含a,年龄在20-30之间,邮箱信息不为null的用户信息// SELECT id,name,age,email FROM user WHERE (name LIKE ? AND age BETWEEN ? AND ? AND email IS NOT NULL)//==> Parameters: %a%(String), 20(Integer), 30(Integer)QueryWrapper<User> wrapper =newQueryWrapper<>();
wrapper.like("name","a").between("age",20,30).isNotNull("email");List<User> users = userMapper.selectList(wrapper);
users.forEach(System.out::println);}
排序
@Testpublicvoidtest01(){// 查询用户信息,按照年龄的降序排序,若年龄相同,则按照id升序排序//SELECT id,name,age,email FROM user ORDER BY age DESC,id ASCQueryWrapper<User> wrapper =newQueryWrapper<>();QueryWrapper<User> wrapper1 = wrapper.orderByDesc("age").orderByAsc("id");List<User> list = userMapper.selectList(wrapper1);
list.forEach(System.out::println);}
删除
@Testpublicvoidtest02(){QueryWrapper<User> wrapper =newQueryWrapper<>();
wrapper.isNull("email");int list = userMapper.delete(wrapper);System.out.println("result"+list);}
优先级
@Testpublicvoidtest03(){// (年龄大于20并且用户名包含a) 或者邮箱为null 的用户信息修改// UPDATE user SET name=?, age=? WHERE is_deleted=0 AND (age > ? AND name LIKE ? OR email IS NULL)UpdateWrapper<User> wrapper =newUpdateWrapper<>();
wrapper.gt("age",20).like("name","a").or().isNull("email");User user =newUser();
user.setName("白毛");
user.setAge(66);int update = userMapper.update(user, wrapper);System.out.println("result"+update);}