mybatis-plus条件构造器UpdateWrapper实例(Mybatis plus condition constructor updatewrapper instance)

mybatis-plus条件构造器UpdateWrapper实例

我们再搞更新或者删除的时候,有时候where条件复杂的话,可以使用UpdateWrapper来构造条件;

我们搞几个实例来深入理解下条件构造器UpdateWrapper的用法:

案例一:

更新指定员工的邮箱和联系电话

sql实现:

UPDATE t_employee SET email="123456@qq.com", phoneNumber="12345678" WHERE id=6

mp实现:

@Testpublic void updateByUpdateWrapper(){  UpdateWrapper<Employee> updateWrapper=new UpdateWrapper<>();    //UpdateWrapper<Employee> updateWrapper2 = Wrappers.<Employee>update();   Employee employee=new Employee();  employee.setEmail("1234@qq.com");     employee.setPhoneNumber("1234567");  updateWrapper.eq("id",6);     int affectRows=employeeMapper.update(employee,updateWrapper);     if(affectRows>0){      System.out.println("更新成功");    }else{        System.out.println("更新失败");   }}

案例二:

删除市场部老员工

sql实现:

DELETE FROM t_employee WHERE DATE_FORMAT(birthday,'%Y-%m-%d')<="1990-01-01" AND departmentId=2

mp实现:

@Testpublic void deleteByUpdateWrapper2(){  UpdateWrapper<Employee> updateWrapper=new UpdateWrapper<>();  //UpdateWrapper<Employee> updateWrapper2 = Wrappers.<Employee>update();  updateWrapper.apply("DATE_FORMAT(birthday,'%Y-%m-%d')<={0}","1990-01-01").eq("departmentId",2);  int affectRows=employeeMapper.delete(updateWrapper);  if(affectRows>0){    System.out.println("删除成功");  }else{    System.out.println("删除失败");  }}

——————————————————————————————————————————

————————

mybatis-plus条件构造器UpdateWrapper实例

When we update or delete, sometimes if the where condition is complex, we can use updatewrapper to construct the condition;

Let’s take a few examples to deeply understand the usage of the conditional constructor updatewrapper:

Case 1:

Update the email and contact number of the specified employee

SQL implementation:

UPDATE t_employee SET email="123456@qq.com", phoneNumber="12345678" WHERE id=6

MP implementation:

@Testpublic void updateByUpdateWrapper(){  UpdateWrapper<Employee> updateWrapper=new UpdateWrapper<>();    //UpdateWrapper<Employee> updateWrapper2 = Wrappers.<Employee>update();   Employee employee=new Employee();  employee.setEmail("1234@qq.com");     employee.setPhoneNumber("1234567");  updateWrapper.eq("id",6);     int affectRows=employeeMapper.update(employee,updateWrapper);     if(affectRows>0){      System.out.println("更新成功");    }else{        System.out.println("更新失败");   }}

Case 2:

Delete old employees of marketing department

SQL implementation:

DELETE FROM t_employee WHERE DATE_FORMAT(birthday,'%Y-%m-%d')<="1990-01-01" AND departmentId=2

MP implementation:

@Testpublic void deleteByUpdateWrapper2(){  UpdateWrapper<Employee> updateWrapper=new UpdateWrapper<>();  //UpdateWrapper<Employee> updateWrapper2 = Wrappers.<Employee>update();  updateWrapper.apply("DATE_FORMAT(birthday,'%Y-%m-%d')<={0}","1990-01-01").eq("departmentId",2);  int affectRows=employeeMapper.delete(updateWrapper);  if(affectRows>0){    System.out.println("删除成功");  }else{    System.out.println("删除失败");  }}

——————————————————————————————————————————