只执行一条语句批量更新
更新语句 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) { //批量修改员工记录成功 } }