Mybatis-Plus的条件构造器(Wrapper)

一.介绍

在mybatis-plus中提了构造条件的类Wrapper,它可以根据自己的意图定义我们需要的条件。Wrapper是一个抽象类,一般情况下我们用它的子类QueryWrapper来实现自定义条件查询。

二.常用方法

1.selectOne方法//查询姓名为刘辉军并且性别为男的员工

@Test
public void testSelectOne(){
QueryWrapper<Employee> queryWrapper=new QueryWrapper<>();
queryWrapper.eq("emp_name","刘辉军");
queryWrapper.eq("emp_gender","男");
Employee employee = employeeMapper.selectOne(queryWrapper);
System.out.println(employee);
}

2.selectList方法

//查询姓名中带有"磊"的并且年龄小于30的员工
@Test
public void testSelectList(){
QueryWrapper<Employee> queryWrapper=new QueryWrapper<>();
queryWrapper.like("emp_name","磊").lt("age",30);// less than 
List<Employee> employeeList = employeeMapper.selectList(queryWrapper);
employeeList.forEach(System.out::println);
}
//查询姓王的或者性别为男,按年龄的降序排序
@Test
public void testSelectList2(){
QueryWrapper<Employee> queryWrapper=new QueryWrapper<>();
queryWrapper.like("emp_name","王")
  .or().eq("emp_gender","男")
  .orderByDesc("age");
List<Employee> employeeList = employeeMapper.selectList(queryWrapper);
employeeList.forEach(System.out::println);
}
//查询姓刘的并且(年龄小于35或者邮箱不为空)
@Test
public void testSelectList3(){
QueryWrapper<Employee> queryWrapper=new QueryWrapper<>();
queryWrapper.likeRight("emp_name","刘")
  .and(wq->wq.lt("age",35).or().isNotNull("email"));
List<Employee> employeeList = employeeMapper.selectList(queryWrapper);
employeeList.forEach(System.out::println);
}

3.selectPage方法

selectPage用于分页,在mybatis-plus中分页有两种,一种是逻辑分页或叫内存分页,另一种是物理分页,内存分页就是把数据全部查询出来放到内存中,返回你想要的一部分数据,当数据量非常庞大时这种方法就行不通了,因为太耗内存,所以一般采用物理分页,需要springboot中加入物理分页配置:(也可以设置到主启动类中)

@Configuration
public class MybatisPlusConfig {
@Bean
  public MybatisPlusInterceptor mybatisPlusInterceptor() {
        MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
        interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL));
        return interceptor;
  }
}

测试:

@Test
public void testSelectPage(){
QueryWrapper<Employee> queryWrapper=new QueryWrapper<>();
queryWrapper.lt("age",50);
//Page<>(当前页,每页记录数);
Page<Employee> page=new Page<>(1,2);
Page<Employee> employeePage = employeeMapper.selectPage(page, queryWrapper);
System.out.println("当前页:"+ employeePage.getCurrent());
System.out.println("每页记录数:"+employeePage.getSize());
System.out.println("总记录数:"+employeePage.getTotal());
System.out.println("总页数:"+employeePage.getPages());
List<Employee> employeeList = employeePage.getRecords();
employeeList.forEach(System.out::println);
}

4.update方法

//根据姓名和年龄修改记录
@Test
public void testUpdate(){
QueryWrapper<Employee> updateWrapper=new QueryWrapper<>();
updateWrapper.eq("emp_name","刘龙")
     .eq("age",25);
Employee employee=new Employee();
employee.setEmpId(1367720249630318593L);
employee.setName("刘龙");
employee.setEmail("lilong111@.qq.com");
employee.setAge(26);
employee.setEmpGender("女");
int rows=employeeMapper.update(employee,updateWrapper);
System.out.println("受影响的行数:"+rows);
}

可以使用UpdateWrapper 进行更新操作: 
@Test
 public void updateTest3(){

     Employee employee =new Employee();
     //设置更新的数据:
     employee.setEmpName("李华");
     employee.setEmail("lihua@qq.com");
     UpdateWrapper<Employee> updateWrapper = new UpdateWrapper<>();
     updateWrapper.eq("age",24);
     updateWrapper.eq("emp_name","何雨柱");
     //参数一: employee 要更新的数据:

     //参数二: 设置查询条件:
     employeeMapper.update(employee,updateWrapper);
 }

5.delete方法

//根据姓名和年龄删除记录
@Test
public void testDelete(){
QueryWrapper<Employee> queryWrapper=new QueryWrapper<>();
queryWrapper.eq("emp_name","刘龙")
  .eq("age",26);
int rows=employeeMapper.delete(queryWrapper);
System.out.println("受影响的行数:"+rows);
}

上一篇:III-MybatisPlus、ORM思想


下一篇:mybatis-plus的QueryWrapper及LambdaQueryWrapper