mybatis因为没有级联保存,级联删除,批量处理方法,所以批量更新需要自己手写,综合网友的知识,(mysql)写法有三种
bean类:
@Entity
public class EvaScore extends BaseEntity {
//考核项编码
private String code;
//考核项
private String name;
//得分
private double score;
//得分占比
private double baifenbi;
//单位类型
private long unit_type;
//任务编号
private long taskid;
//验收时间
private Date checktime;
//实际得分
private double actscore;
/**
* 考核项分配人员
*/
private long memberid;
/**
* 考核项分类
*/
private String mgType;
public String getCode() {
return code;
}
public void setCode(String code) {
this.code = code;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public double getScore() {
return score;
}
public void setScore(double score) {
this.score = score;
}
public double getBaifenbi() {
return baifenbi;
}
public void setBaifenbi(double baifenbi) {
this.baifenbi = baifenbi;
}
public long getUnit_type() {
return unit_type;
}
public void setUnit_type(long unit_type) {
this.unit_type = unit_type;
}
public long getTaskid() {
return taskid;
}
public void setTaskid(long taskid) {
this.taskid = taskid;
}
public Date getChecktime() {
return checktime;
}
public void setChecktime(Date checktime) {
this.checktime = checktime;
}
public double getActscore() {
return actscore;
}
public void setActscore(double actscore) {
this.actscore = actscore;
}
public long getMemberid() {
return memberid;
}
public void setMemberid(long memberid) {
this.memberid = memberid;
}
public String getMgType() {
return mgType;
}
public void setMgType(String mgType) {
this.mgType = mgType;
}
}
接口:
public void batchUpdateEvaScores(@Param("list") List<EvaScore> list);
mapper:
<update id="batchUpdateEvaScores" parameterType="java.util.List">
<!--方法一-->
<foreach collection="list" item="item" index="index" separator=";" open="" close="">
update eva_score
<set>baifenbi=#{item.baifenbi},
taskid=#{item.taskid},
unit_type=#{item.unit_type}
</set>
where name=#{item.name} and mg_type=#{item.mgType}
</foreach>
<!--
<!--方法二-->
<foreach collection="list" separator=";" item="cus">
update eva_score set
baifenbi = #{cus.baifenbi},
taskid = #{cus.taskid},
unit_type = #{cus.unit_type}
where name=#{cus.name} and mg_type=#{cus.mgType}
</foreach>
<!-- 方法三 -->
update eva_score
<trim prefix="set" suffixOverrides=",">
<trim prefix="baifenbi =case" suffix="end,">
<foreach collection="list" item="item" index="index">
<if test="item.baifenbi !=null and item.baifenbi != -1">
when name=#{item.name} then #{item.baifenbi}
</if>
<if test="item.baifenbi == null or item.baifenbi == -1">
when name=#{item.name} then eva_score.baifenbi//原数据
</if>
</foreach>
</trim>
<trim prefix="baifenbi =case" suffix="end,">
<foreach collection="list" item="item" index="index">
<if test="item.taskid !=null and item.taskid != -1">
when name=#{item.name} then #{item.taskid}
</if>
<if test="item.taskid == null or item.taskid == -1">
when name=#{item.name} then eva_score.taskid//原数据
</if>
</foreach>
</trim>
<trim prefix="baifenbi =case" suffix="end,">
<foreach collection="list" item="item" index="index">
<if test="item.unit_type !=null and item.unit_type != -1">
when name=#{item.name} then #{item.unit_type}
</if>
<if test="item.unit_type == null or item.unit_type == -1">
when name=#{item.name} then eva_score.unit_type//原数据
</if>
</foreach>
</trim>
</trim>
where name in
<foreach collection="list" index="index" item="item"
separator="," open="(" close=")">
#{item.name}
</foreach>
-->
</update>
注意:当使用第一第二种写法时,需要给mysql配置批量执行,在spring.datasource.url后加上allowMultiQueries=true如 spring.datasource.url=jdbc:mysql://47.93.23.66:3306/db_smarte?allowMultiQueries=true
否则,将报错不能执行。