11.2 基于注解的动态SQL3 update
Provider类中添加生成动态SQL的方法
/MyADynamicSQLTest/src/mapper/EmployeeDynamicSQLProvider.java1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42
| public String updateEmployeeByPO(Employee employee) { return new SQL(){ { UPDATE("tb_employee"); if(employee.getLoginname() != null) { SET("loginname=#{loginname}"); } if(employee.getPassword() != null) { SET("password=#{password}"); } if(employee.getName() != null) { SET("name=#{name}"); } if(employee.getSex() != null) { SET("sex=#{sex}"); } if(employee.getAge() != null) { SET("age=#{age}"); } if(employee.getPhone() != null) { SET("phone=#{phone}"); } if(employee.getSal() != null) { SET("sal=#{sal}"); } if(employee.getState() != null) { SET("state=#{state}"); } WHERE("id=#{id}"); } }.toString(); }
|
mapper接口中引用生成动态update的方法
/MyADynamicSQLTest/src/mapper/EmployeeMapper.java1 2 3 4 5
| @UpdateProvider( type = EmployeeDynamicSQLProvider.class, method = "updateEmployeeByPO" ) void updateEmployeeByPO(Employee employee);
|
测试类UpdateTest.java
/MyADynamicSQLTest/src/test/UpdateTest.java1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56
| package test;
import org.apache.ibatis.session.SqlSession; import domain.Employee; import fractory.SqlSessionFactoryTools; import mapper.EmployeeMapper;
public class UpdateTest { public static void main(String[] args) { SqlSession sqlSession = null; try { sqlSession = SqlSessionFactoryTools.getSqlSession(); EmployeeMapper employeeMapper = sqlSession.getMapper(EmployeeMapper.class); Employee employee = employeeMapper.selectEmployeeById(3); System.out.println("-------------------------------------------------"); if (employee != null) { System.out.println("修改之前的员工信息"); System.out.println(employee); System.out.println("-------------------------------------------------"); Employee employeeUpdate = new Employee(); employeeUpdate.setId(employee.getId()); employeeUpdate.setName("修改之后的姓名"); employeeUpdate.setSex("男"); employeeMapper.updateEmployeeByPO(employeeUpdate); System.out.println("-------------------------------------------------"); employee = employeeMapper.selectEmployeeById(3); System.out.println("-------------------------------------------------"); System.out.println(employee); } sqlSession.commit(); } catch (Exception e) { sqlSession.rollback(); e.printStackTrace(); } finally { if (sqlSession != null) sqlSession.close(); } } }
|
运行效果
DEBUG [main] ==> Preparing: select * from tb_employee where id=?
DEBUG [main] ==> Parameters: 3(Integer)
DEBUG [main] <== Total: 1
-------------------------------------------------
修改之前的员工信息
Employee [id=3, loginname=xiaoli, password=xiaoli, name=小丽, sex=女, age=23, phone=123456789123, sal=7800.0, state=active]
-------------------------------------------------
DEBUG [main] ==> Preparing: UPDATE tb_employee SET name=?, sex=? WHERE (id=?)
DEBUG [main] ==> Parameters: 修改之后的姓名(String), 男(String), 3(Integer)
DEBUG [main] <== Updates: 1
-------------------------------------------------
DEBUG [main] ==> Preparing: select * from tb_employee where id=?
DEBUG [main] ==> Parameters: 3(Integer)
DEBUG [main] <== Total: 1
-------------------------------------------------
Employee [id=3, loginname=xiaoli, password=xiaoli, name=修改之后的姓名, sex=男, age=23, phone=123456789123, sal=7800.0, state=active]