前言
大家好呀,今天这篇文章为大家介绍MyBatis如何实现批量更新~
一、案例
提示:以下是本篇文章正文内容,下面案例可供参考
假设对象是StudentVo
package com.school.vo;
import lombok.Data;
@Data
public class StudentVo {
/**
* 学生学号(必填)
*/
private String studentNo;
/**
* 学生姓名
*/
private String studentName;
/**
* 学生年龄
*/
private String studentAge;
/**
* 学生性别
*/
private String studentSex;
}
现在需要进行批量更新
List<StudentVo> list;
二、MyBatis实现批量更新的方式
1.Oracle方式
mapper层(简略版本):
<update id = "updateStudentInfoBatch" parameterType = "com.school.vo.StudentVo">
<foreach collection="list" item="item" index="index" open="BEGIN" close=";END;" separator=";">
UPDATE STUDENT
SET
STUDENT_NAME = #{item.studentName},
STUDENT_AGE = #{item.studentAge},
STUDENT_SEX = #{item.studentSex}
WHERE
STUDENT_NO = #{item.studentNo}
</foreach>
</update>
2.postgreSql方式
<update id = "updateStudentInfoBatch" parameterType = "com.school.vo.StudentVo">
<foreach collection="list" item="item" separator=";">
update sch.student
<trim prefix="set" suffixOverrides=",">
<if test="item.studentName != null">
student_name = #{item.studentName},
</if>
<if test="item.studentAge != null">
student_age = #{item.studnetAge}
</if>
<if test="item.studentSex != null">
student_sex = #{item.studentSex}
</if>
</trim>
<where>
student_no = #{item.studentNo}
</where>
</foreach>
</update>
总结
以上就是今天要讲的内容,本文仅仅简单介绍了MyBatis实现批量更新的两种方式,希望对大家有所帮助。
╭◜◝ ͡ ◜◝╮
( ˃̶͈◡˂ ̶͈ )感觉有用的话,欢迎点赞评论呀!
╰◟◞ ͜ ◟◞╯