0%

11.2 基于注解的动态SQL3 update

11.2 基于注解的动态SQL3 update

生成动态SQL的方法

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接口方法

1
2
3
4
5
@UpdateProvider(
type = EmployeeDynamicSQLProvider.class,
method = "updateEmployeeByPO"
)
void updateEmployeeByPO(Employee employee);

测试类

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
package test;
import org.apache.ibatis.session.SqlSession;
import domain.Employee;
import fractory.SqlSessionFratoryTools;
import mapper.EmployeeMapper;
public class UpdateTest{
public static void main(String[] args)
{
SqlSession sqlSession = null;
try
{
// 加载mybatis-config.xml,获取SqlSession实例
sqlSession = SqlSessionFratoryTools.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.setLoginname();
// employeeUpdate.setPassword();
employeeUpdate.setName("修改之后的姓名");
employeeUpdate.setSex("男");
// employeeUpdate.setAge(15);
// employeeUpdate.setPhone();
// employeeUpdate.setSal(123.1);
// employeeUpdate.setState("员工");
//
employeeMapper.updateEmployeeByPO(employeeUpdate);
System.out.println("-------------------------------------------------");
// sqlSession.commit();
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();
}
}
}

运行效果

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
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=15, phone=123456789123, sal=123.1, state=员工]
-------------------------------------------------
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=15, phone=123456789123, sal=123.1, state=员工]

原文链接: 11.2 基于注解的动态SQL3 update