mybatis的批量update
⽅法有三种:1.通过java代码batch⽅式,xml⽂件只需⼀条update语句。java代码繁琐
<mapper namespace="com.dao.EmployeeDao">
<!--可以通过改变实体类的字段来验证映射-->
<update id="updateFirstWay">
update employee set employee_name = #{employeeName},
employee_gender = #{employeeGender},
employee_salary = #{employeeSalary},
dept_id = #{deptId}
where employee_id = #{employeeId}
</update>
<update id="updateSecondWay">
<foreach collection="list" item="emp" separator=";">
update employee set employee_name = #{ployeeName},
employee_gender = #{ployeeGender},
employee_salary = #{ployeeSalary},
dept_id = #{emp.deptId}
where employee_id = #{ployeeId}
</foreach>
</update>
<update id="updateThirdWay">
update employee set
employee_name= case employee_id
<foreach collection="list" item="emp">
when #{ployeeId} then #{ployeeName}
</foreach>
end, employee_gender= case employee_id
<foreach collection="list" item="emp">
when #{ployeeId} then #{ployeeGender}
</foreach>
end, employee_salary= case employee_id
bigdecimal转换为integer
<foreach collection="list" item="emp">
when #{ployeeId} then #{ployeeSalary}
</foreach>
end,dept_id= case employee_id
<foreach collection="list" item="emp">
when #{ployeeId} then #{emp.deptId}
</foreach>
end
where
<foreach collection="list" item="emp" separator="or">
employee_id = #{ployeeId}
</foreach>
</update>
</mapper>
  实体类:
ity;
import java.math.BigDecimal;
public class Employee {
private Integer employeeId;
private String employeeName;
private int employeeGender;
private BigDecimal employeeSalary;
private Integer deptId;
public Employee() {
}
public Employee(Integer employeeId, String employeeName, int employeeGender, BigDecimal employeeSalary, Integer deptId) {
this.deptId = deptId;
}
@Override
public String toString() {
return "Employee{" +
"employeeId=" + employeeId +
", employeeName='" + employeeName + '\'' +
", employeeGender=" + employeeGender +
", employeeSalary=" + employeeSalary +
", deptId='" + deptId + '\'' +
'}';
}
public Integer getEmployeeId() {
return employeeId;
}
public void setEmployeeId(Integer employeeId) {
}
public String getEmployeeName() {
return employeeName;
}
public void setEmployeeName(String employeeName) {
}
public int getEmployeeGender() {
return employeeGender;
}
public void setEmployeeGender(int employeeGender) {
}
public BigDecimal getEmployeeSalary() {
return employeeSalary;
}
public void setEmployeeSalary(BigDecimal employeeSalary) {
}
public Integer getDeptId() {
return deptId;
}
public void setDeptId(Integer deptId) {
this.deptId = deptId;
}
}
  EmployeeDao.class:
void updateFirstWay(Employee employee);
int updateSecondWay(List<Employee> employees);
int updateThirdWay(List<Employee> employees);
  test代码:
@Test
public void testUpdateFirstWay() {
List<Employee> employees = new ArrayList<>();
employees.add(new Employee(10,"傲慢", 1, new BigDecimal(9999), 1));
employees.add(new Employee(11,"⾊欲", 0, new BigDecimal(8888), 2));
employees.add(new Employee(12,"怠惰", 1, new BigDecimal(7777), 3));
employees.add(new Employee(13,"贪婪", 1, new BigDecimal(6666), 4));
employees.add(new Employee(14,"愤怒", 1, new BigDecimal(5555), 5));
SqlSessionFactory sqlSessionFactory = SqlSessionFactory();        SqlSession sqlSession = sqlSessionFactory.openSession(ExecutorType.BATCH);
EmployeeDao mapper = Mapper(EmployeeDao.class);
try {
for (int i = 0; i < employees.size(); i ++) {
mapper.(i));
if (i % 2 == 0 || i == employees.size() - 1) {
sqlSessionmit();
}
}
} catch (Exception e) {
} finally {
sqlSession.close();
}
}
@Test
public void testUpdateSecondWay() {
List<Employee> employees = new ArrayList<>();
employees.add(new Employee(15,"傲慢", 1, new BigDecimal(9999), 1));
employees.add(new Employee(16,"⾊欲", 1, new BigDecimal(8888), 2));
employees.add(new Employee(17,"怠惰", 1, new BigDecimal(7777), 3));
employees.add(new Employee(18,"贪婪", 0, new BigDecimal(6666), 4));
employees.add(new Employee(19,"愤怒", 1, new BigDecimal(5555), 5));
EmployeeDao employeeDao = ateMapper(EmployeeDao.class);
employeeDao.updateSecondWay(employees);
}
@Test
public void testUpdateThirdWay() {
List<Employee> employees = new ArrayList<>();
employees.add(new Employee(1,"傲慢", 1, new BigDecimal(9999), 1));
employees.add(new Employee(2,"⾊欲", 1, new BigDecimal(8888), 2));
EmployeeDao employeeDao = ateMapper(EmployeeDao.class);
employeeDao.updateThirdWay(employees);
}
  总结:#{}⾥⾯写的是实体类的字段,已经测试过了。
最后:根据实际要求,每个字段是要判断是否为空的,例
<update id="updateFirstWay">
update employee set
<if test="b != null and b != ''">
employee_name = #{b},
</if>
<if test="c != null and c != ''">
employee_gender = #{c},
</if>
<if test="d != null and d != ''">
employee_salary = #{d},
</if>
<if test="e != null and e !=''">
dept_id = #{e}
</if>
where employee_id = #{a}
</update>
  因为当只修改部分数据库的列的值,⽽不是全部列的时候,就需要将不要修改的字段跳过。上⾯的代码有漏洞,当所有列都不修改的时候会出错,修改代码的话以后再说,累了
有个bug:数据库的binary类型,实体类写的Integer,但是写在ongl⾥⾯判断x != null and x != '',如果是0的话,那么这句话会跳过,false

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。