Mybatis中进行添加(insert)、批量更新(update)##
一、添加insert:
方法1:
mapper.xml文件:
<insert id="savaApplyProg">
insert into tb_applyprogram(hid,name,version) values
<foreach collection="list" item="info" separator=",">
<choose>
<when test="info.name!=null and info.name!=''">
(#{info.hid},#{info.name},#{info.version})
</when>
<otherwise>
('','','')
</otherwise>
</choose>
</foreach>
</insert>
java控制层文件:
接收前端的批量参数,并将参数转为list集合
@RequestMapping(value="savaApplyProg")
@ResponseBody
public int savaApplyProg(String data){
List<ApplyProg> list = new ArrayList<ApplyProg>();
list = JSONArray.parseArray(data,ApplyProg.class);//这里的t是Class<T>
int i = tbHardwareService.savaApplyProg(list);
return i;
}
方法2:
同方法1中java控制层文件,转为list集合后,遍历list,循环插入。
二、批量更新update:
方法1:
mapper.xml文件:
<select id="updateShopCarNum" parameterType="java.util.List">
update yard_order_shopcar set menuNum =
<foreach collection="list" item="info" separator="" index="index" open="case id" close="end">
when #{info.id} then #{info.menuNum}
</foreach>
where id in
<foreach collection="list" index="index" item="info" separator="," open="(" close=")">
#{info.id}
</foreach>
</select>
java控制层文件:
接收前端的批量参数,并将参数转为list集合
方法2:
同方法1中java控制层文件,转为list集合后,遍历list,根据条件循环更新。