mybatis 批量更新

<!-- 批量修改 -->
    <update id="batachUpdateFieldChild" parameterType="java.util.List">
    update yryz_field_child c set
    c.designation=
    <foreach collection="list" item="fieldChild" index="index"
        separator=" " open="case c.id" close="end">
        when #{fieldChild.id,jdbcType=INTEGER} then
        #{fieldChild.designation,jdbcType=VARCHAR}
    </foreach>
    ,c.attach_code=
    <foreach collection="list" item="fieldChild" index="index"
        separator=" " open="case c.id" close="end">
        when #{fieldChild.id,jdbcType=INTEGER} then
        #{fieldChild.attachCode,jdbcType=INTEGER}
    </foreach>
    where c.id in
    <foreach collection="list" index="index" item="fieldChild"
        separator="," open="(" close=")">
        #{fieldChild.id,jdbcType=INTEGER}
    </foreach>
</update>

原理等同于:

 update yryz_field_child c set
    c.designation= case c.id

     when id then attachCode

end

where c.id in (id)

eg:

 update yryz_field_child c set
    c.designation= case c.id

    when 1 then 'hello'

    when 2 then 'world'

end

where c.id in (1,2)

eg:

update yryz_field_child c
set c.designation= case c.id
    when ? then ?
    when ? then ?
    when ? then ?
    when ? then ?
end
where c.id in ( ? , ? , ? , ? )

public int batachUpdateFieldChild(List<FieldChild> fieldChildList);

    @ResponseBody
    @POST
    @Path("/batachUpdateFieldChild")
    public ResultEntity batachUpdateFieldChild(MergeDto mergeDto){
        String designationStrs=mergeDto.getDesignation();
        String[] designationsBase=designationStrs.split(" ");
        String[] newdesignations=arrayResult(designationsBase);
        int id=0;String designation="";
        List<FieldChild> fieldChildlist=new ArrayList<FieldChild>();
        FieldChild fieldChild=null;
        for(int i=0;i<newdesignations.length;i++){
            String newdesignationsChild=newdesignations[i];
            System.out.println(newdesignationsChild);
            String[] lastdesignationsChild=newdesignationsChild.split(",");
            for(int j=0;j<lastdesignationsChild.length;j++){
                id=Integer.valueOf(lastdesignationsChild[0]);
                fieldChild=new FieldChild();
                designation=lastdesignationsChild[1];
                fieldChild.setId(Long.valueOf(id));
                fieldChild.setAttachCode(mergeDto.getAttachCode());
                fieldChild.setDesignation(designation);
            }
            fieldChildlist.add(fieldChild);
            
        }
        int countChild=fieldChildService.batachUpdateFieldChild(fieldChildlist);
        String msg="";
        if(countChild>0){
            msg="success";
        }else{
            msg="fail";
        }
        return new ResultEntity(msg,null);
    }

 空格进行分割,完了放入array数组中,便利数组存入对象,将对象放入List中

/**
     * 对数组进行去重
     * @param str
     * @return
     */
    public static String[] arrayResult(String[] str){
        String[] array = str;  
        Set<String> set = new HashSet<>();  
        for(int i=0;i<array.length;i++){  
            set.add(array[i]);  
        }  
        String[] arrayResult = (String[]) set.toArray(new String[set.size()]);
        return arrayResult;
    }

原文地址:https://www.cnblogs.com/austinspark-jessylu/p/6652499.html