@Test publicvoidtest01(){ //查询用户名包含a,年龄在20到30之间,并且邮箱不为null的用户信息 //SELECT id,username AS name,age,email,is_deleted FROM t_user WHERE is_deleted=0 AND (username LIKE ? AND age BETWEEN ? AND ? AND email IS NOT NULL) QueryWrapper<User> queryWrapper = newQueryWrapper<>(); queryWrapper.like("username", "a") .between("age", 20, 30) .isNotNull("email"); List<User> list = userMapper.selectList(queryWrapper); list.forEach(System.out::pintln); }
2、例2:组装排序条件
1 2 3 4 5 6 7 8 9 10 11
@Test publicvoidtest02(){ //按年龄降序查询用户,如果年龄相同则按id升序排列 //SELECT id,username AS name,age,email,is_deleted FROM t_user WHERE is_deleted=0 ORDER BY age DESC,id ASC QueryWrapper<User> queryWrapper = newQueryWrapper<>(); queryWrapper .orderByDesc("age") .orderByAsc("id"); List<User> users = userMapper.selectList(queryWrapper); users.forEach(System.out::println); }
3、例3:组装删除条件
1 2 3 4 5 6 7 8
@Test publicvoidtest03(){ //删除email为空的用户 //DELETE FROM t_user WHERE (email IS NULL) QueryWrapper<User> queryWrapper = newQueryWrapper<>(); queryWrapper.isNull("email"); //条件构造器也可以构建删除语句的条件 intresult= userMapper.delete(queryWrapper); System.out.println("受影响的行数:" + result); }
4、例4:条件的优先级
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
@Test publicvoidtest04() { QueryWrapper<User> queryWrapper = newQueryWrapper<>(); //将(年龄大于20并且用户名中包含有a)或邮箱为null的用户信息修改 //UPDATE t_user SET age=?, email=? WHERE (username LIKE ? AND age > ? OR email IS NULL) queryWrapper .like("username", "a") .gt("age", 20) .or() .isNull("email"); Useruser=newUser(); user.setAge(18); user.setEmail("user@test.com"); intresult= userMapper.update(user, queryWrapper); System.out.println("受影响的行数:" + result); }
1 2 3 4 5 6 7 8 9 10 11 12 13 14
@Test publicvoidtest04() { QueryWrapper<User> queryWrapper = newQueryWrapper<>(); //将用户名中包含有a并且(年龄大于20或邮箱为null)的用户信息修改 //UPDATE t_user SET age=?, email=? WHERE (username LIKE ? AND (age > ? ORemail IS NULL)) //lambda表达式内的逻辑优先运算 queryWrapper .like("username", "a") .and(i -> i.gt("age", 20).or().isNull("email")); Useruser=newUser(); user.setAge(18); user.setEmail("user@test.com"); intresult= userMapper.update(user, queryWrapper); System.out.println("受影响的行数:" + result); }
@Test publicvoidtest06() { //查询id小于等于3的用户信息 //SELECT id,username AS name,age,email,is_deleted FROM t_user WHERE (id IN(select id from t_user where id <= 3)) QueryWrapper<User> queryWrapper = newQueryWrapper<>(); queryWrapper.inSql("id", "select id from t_user where id <= 3"); List<User> list = userMapper.selectList(queryWrapper); list.forEach(System.out::println); }
3、UpdateWrapper
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
@Test publicvoidtest07() { //将(年龄大于20或邮箱为null)并且用户名中包含有a的用户信息修改 //组装set子句以及修改条件 UpdateWrapper<User> updateWrapper = newUpdateWrapper<>(); //lambda表达式内的逻辑优先运算 updateWrapper .set("age", 18) .set("email", "user@test.com") .like("username", "a") .and(i -> i.gt("age", 20).or().isNull("email")); //这里必须要创建User对象,否则无法应用自动填充。如果没有自动填充,可以设置为null //UPDATE t_user SET username=?, age=?,email=? WHERE (username LIKE ? AND (age > ? OR email IS NULL)) //User user = new User(); //user.setName("张三"); //int result = userMapper.update(user, updateWrapper); //UPDATE t_user SET age=?,email=? WHERE (username LIKE ? AND (age > ? OR email IS NULL)) intresult= userMapper.update(null, updateWrapper); System.out.println(result); }