• 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) {     //批量修改员工记录成功    } }
  • 相关阅读:
    Ajax学习感悟
    C#自定义控件designmodel的判断
    Asp.net+Flash多文件上传
    .net下帮助文件(sandcastle)
    水晶报表小例用于学习
    WinAPI转C#利器
    利用HttpHandler和Cache统计点击量
    关于C#调用API的理解(汇多考勤机HD4K)
    ubuntu 12.04 配置PHP開發環境遇到的問題
    windows 7 系統在VMWear workstation 9上安裝Mac OS X 10.7
  • 原文地址:https://www.cnblogs.com/zjk1/p/10032753.html
Copyright © 2020-2023  润新知