/**
* 查询名字中包含'牛'并且年龄小于28
* where name like '%牛%' and age < 28
*/
@Test
public void selectByWrapper(){
QueryWrapper queryWrapper = new QueryWrapper<>();
queryWrapper.like("name","牛").lt("age",28);
List userList = userMapper.selectList(queryWrapper);
userList.forEach(System.out::println);
}
/**
* 创建日期为2020年1月14日并且姓名为王姓
* date_format(create_time,'%Y-%m-%d') and manager_id in (select id from user where name like '王%')
*/
@Test
public void selectByWrapper2(){
QueryWrapper queryWrapper = new QueryWrapper<>();
queryWrapper.apply("date_format(create_time,'%Y-%m-%d')={0}","2020-01-14")
.inSql("manager_id","select id from user where name like '王%'");
List userList = userMapper.selectList(queryWrapper);
userList.forEach(System.out::println);
}
/**
* 名字为王姓,(年龄小于40或者邮箱不为空)
*/
@Test
public void selectByWrapper3(){
QueryWrapper queryWrapper = new QueryWrapper<>();
queryWrapper.likeRight("name","王").and(wq->
wq.lt("age",40).or().isNotNull("email"));
List userList = userMapper.selectList(queryWrapper);
userList.forEach(System.out::println);
}
/**
* 名字为王姓,(年龄小于40,并且年龄大于20,并且邮箱不为空)
*/
@Test
public void selectWrapper4(){
QueryWrapper queryWrapper = new QueryWrapper<>();
queryWrapper.likeRight("name", "王").and(wq -> wq.between("age", 20, 40).and(wqq -> wqq.isNotNull("email")));
List userList = userMapper.selectList(queryWrapper);
userList.forEach(System.out::println);
}
/**
* (年龄小于40或者邮箱不为空)并且名字为王姓
* (age<40 or email is not null)and name like '王%'
*/
@Test
public void selectWrapper5(){
QueryWrapper queryWrapper = new QueryWrapper<>();
queryWrapper.nested(wq->wq.lt("age",40).or().isNotNull("email")).likeRight("name","王");
List userList = userMapper.selectList(queryWrapper);
userList.forEach(System.out::println);
}
/**
* 年龄为30,31,35,34的员工
*/
@Test
public void selectWrapper6(){
QueryWrapper queryWrapper = new QueryWrapper<>();
queryWrapper.in("age", Arrays.asList(30,31,34,35));
List userList = userMapper.selectList(queryWrapper);
userList.forEach(System.out::println);
}
/**
* 查找为王姓的员工的姓名和年龄
*/
@Test
public void selectWrapper8(){
QueryWrapper queryWrapper = new QueryWrapper<>();
queryWrapper.select("name","age").likeRight("name","王");
List userList = userMapper.selectList(queryWrapper);
userList.forEach(System.out::println);
}
/**
* 查询所有员工信息除了创建时间和员工ID列
*/
@Test
public void selectWrapper9(){
QueryWrapper queryWrapper = new QueryWrapper<>();
queryWrapper.select(User.class,info->!info.getColumn().equals("create_time")
&&!info.getColumn().equals("manager_id"));
List userList = userMapper.selectList(queryWrapper);
userList.forEach(System.out::println);
}
apply
方法为例)可以看出,每个查询方法的第一个参数都是boolean类型的参数,重载方法中默认给我们传入的都是true。
default Children apply(String applySql, Object... value) {
return apply(true, applySql, value);
}
Children apply(boolean condition, String applySql, Object... value);
这个condition的作用是为true时,执行其中的SQL条件,为false时,忽略设置的SQL条件。
在web开发中,controller层常常会传递给我们一个用户的对象,比如通过用户姓名和用户年龄查询用户列表。
我们可以将传递过来的对象直接以构造参数的形式传递给QueryWrapper
,MyBatisPlus会自动根据实体对象中的属性自动构建相应查询的SQL语句。
@Test
public void selectWrapper10(){
User user = new User();
user.setName("刘红雨");
user.setAge(32);
QueryWrapper queryWrapper = new QueryWrapper<>(user);
List userList = userMapper.selectList(queryWrapper);
userList.forEach(System.out::println);
}
@Test
public void selectWrapper10(){
User user = new User();
user.setName("红");
user.setAge(32);
QueryWrapper queryWrapper = new QueryWrapper<>(user);
List userList = userMapper.selectList(queryWrapper);
userList.forEach(System.out::println);
}