mybatis批量更新多个字段方法

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 list);

mapper:


			
	
		update eva_score 
		baifenbi=#{item.baifenbi},
			taskid=#{item.taskid},
			unit_type=#{item.unit_type}
		
		where name=#{item.name} and mg_type=#{item.mgType}
	
		 
	     
	        update eva_score set  
	        baifenbi = #{cus.baifenbi},  
	        taskid = #{cus.taskid},  
	        unit_type = #{cus.unit_type}
	        where name=#{cus.name} and mg_type=#{cus.mgType}
	    
	    
	
	update eva_score
        
            
                 
                     
                         when name=#{item.name} then #{item.baifenbi}
                     
                     
                         when name=#{item.name} then eva_score.baifenbi//原数据
                     
                     
                     
                 
            
            
                 
                     
                         when name=#{item.name} then #{item.taskid}
                     
                     
                         when name=#{item.name} then eva_score.taskid//原数据
                     
                 
            
            
                 
                     
                         when name=#{item.name} then #{item.unit_type}
                     
                     
                         when name=#{item.name} then eva_score.unit_type//原数据
                     
                 
            
        
        where name in
    	
        	#{item.name}
    	
	     --> 
	
注意:当使用第一第二种写法时,需要给mysql配置批量执行,在spring.datasource.url后 加上allowMultiQueries=true
如 spring.datasource.url=jdbc:mysql://47.93.23.66:3306/db_smarte?allowMultiQueries=true

否则,将报错不能执行。





你可能感兴趣的:(MyBatis)