mybatis_plus
Mybatis-Plus条件构造器
(一)条件构造器介绍
在mybatis-plus中提了构造条件的类Wrapper,它可以根据自己的意图定义我们需要的条件。Wrapper是一个抽象类,一般情况下我们用它的子类QueryWrapper来实现自定义条件查询.
(二)selectOne方法
//查询姓名为mojie并且性别为男的员工 @Test public void testSelectOne(){ QueryWrapper<Employee> queryWrapper=new QueryWrapper<>(); queryWrapper.eq("emp_name","mojie"); queryWrapper.eq("emp_gender","男"); Employee employee = employeeMapper.selectOne(queryWrapper); System.out.println(employee); }
(三).selectList方法
//查询姓名中带有"磊"的并且年龄小于30的员工 @Test public void testSelectList(){ QueryWrapper<Employee> queryWrapper=new QueryWrapper<>(); queryWrapper.like("emp_name","磊").lt("age",30); 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().lt("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); }
(四).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<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); }
(五)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); }
(六).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); }