11.2.1 基于注解的动态SQL3 update

11.2 基于注解的动态SQL3 update

Provider类中添加生成动态SQL的方法

/MyADynamicSQLTest/src/mapper/EmployeeDynamicSQLProvider.java
1
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(){
{
// 在这里写上动态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.java
1
2
3
4
5
@UpdateProvider(
type = EmployeeDynamicSQLProvider.class,
method = "updateEmployeeByPO"
)
void updateEmployeeByPO(Employee employee);

测试类UpdateTest.java

/MyADynamicSQLTest/src/test/UpdateTest.java
1
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 {
// 加载mybatis-config.xml,获取SqlSession实例
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();
// 主键id不更新
employeeUpdate.setId(employee.getId());
// 更新其他信息
// employeeUpdate.setLoginname();
// employeeUpdate.setPassword();
employeeUpdate.setName("修改之后的姓名");
employeeUpdate.setSex("男");
// employeeUpdate.setAge(15);
// employeeUpdate.setPhone();
// employeeUpdate.setSal(123.1);
// employeeUpdate.setState("员工");
// 执行更新语句
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]