mybatis-plus条件构造器简介
mybatis-plus提供了AbstractWrapper抽象类,提供了很多sql语法支持的方法,比如模糊查询,比较,区间,分组查询,排序,判断空,子查询等等,方便我们用面向对象的方式去实现sql语句;
AbstractWrapper有两个常用的实现类,分别是QueryWrapper和UpdateWrapper,我们以后就用这两个实现类;
下面搞两个实例来体验下QueryWrapper条件构造器:
/**
* 查找薪水大于3500 名字里有“小”的 员工
* sql: select * from t_employee where salary>3500 and name like '%小%'
*/
@Test
public void selectByQueryWrapper(){
QueryWrapper<Employee> queryWrapper=new QueryWrapper();
// QueryWrapper<Employee> queryWrapper2=Wrappers.<Employee>query();
queryWrapper.gt("salary",3500).like("name","小");
List<Employee> employeeList = employeeMapper.selectList(queryWrapper);
System.out.println(employeeList);
}
/**
* 查找薪水范围【3500,5000】,名字里有“小”以及email不为空 的员工
* sql: select * from t_employee where between 3500 and 5000 and name like '%小%' and email is not null
*/
@Test
public void selectByQueryWrapper2(){
QueryWrapper<Employee> queryWrapper=new QueryWrapper();
// QueryWrapper<Employee> queryWrapper2=Wrappers.<Employee>query();
queryWrapper.like("name","小").between("salary",3500,5000).isNotNull("email");
List<Employee> employeeList = employeeMapper.selectList(queryWrapper);
System.out.println(employeeList);
}