mybatis批量修改


只執行一條語句批量更新

更新語句 update table set dept_id=?,emp_code=?  where id in (1,2,3);

1.接口

    int batchUpdateEmpParams(@Param("list") List<Emp> list, @Param("deptId") String deptId,
            @Param("empCode") String empCode);

 

2.xml

 

<update id="batchUpdateEmpParams" parameterType="Map">
        update table 
    <set>
        <if test="deptId!= null and deptId!= '' ">
                dept_id = #{deptId,jdbcType=VARCHAR},
            </if>
        <if test="empCode!= null and empCode!= '' "> 
          emp_code = #{empCode,jdbcType=VARCHAR} 
       </if>
    </set> 
where id in <foreach collection="list" index="index" item="item" open="(" separator="," close=")"> #{item.id,jdbcType=VARCHAR} </foreach> </update>

  

3.Emp.java

public class Emp implements Serializable {
    private String id;

    private String empCode;

    private String deptId;
   //getter setter

}

  

4.控制器

 /**
     * 批量修改部門、code
     * 員工id用英文逗號隔開 如1,2,3
     * @author kpzc
     * @time 20181128
     */
@RequestMapping(value = "/xxx.json") public void batchUpdateEmpParams(Emp emp,HttpServletRequest request) {     List<Emp> empList = new ArrayList<Emp>(); if(null!=emp){ String [] ids=null; if(null!=emp.getId()){ ids=emp.getId().split(","); } for (int i = 0; i < ids.length; i++) { Emp e=new Emp(); e.setId(ids[i]); empList.add(e); } } int updateRows=0; try{ updateRows = empMapper.batchUpdateEmpParams(empList, emp.getDeptId(), emp.getEmpCode()); }catch(Exception e){ e.printStackTrace(); //批量修改員工記錄異常! } if (updateRows > 0) {     //批量修改員工記錄成功    } }

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM