zoukankan      html  css  js  c++  java
  • 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) {     //批量修改员工记录成功    } }
  • 相关阅读:
    690. 员工的重要性
    【递推算法】
    【数据排序】快速排序
    【数据排序】车厢重组
    【基本算法--高精度计算】大整数相加
    【基本算法--高精度计算】回文数
    高精度计算 除法 高精除以低精
    PReLU
    重学C++(1)
    概率论基础知识回顾(1)
  • 原文地址:https://www.cnblogs.com/zjk1/p/10032753.html
Copyright © 2011-2022 走看看