mybatis 插入多条记录
1.传List
2.传map,包括主表参数和list
void saveRepairListPartDetail(List< RepairListPartDetail> serviceListPartDetails);
< insert id=”saveRepairListPartDetail” parameterType=”java.util.List”>
declare @repairkind int
select @repairkind = repairkind from repairlistmaster where repairno =’${list[0].repairno}’
< foreach collection=”list” item=”item” index=”index” separator=”;”>
if(EXISTS(select 1 from RepairListPartDetail where repairno=’${item.repairno}’ and partno =’${item.partno}’ and rid =’${item.rid}’) )
begin
update RepairListPartDetail
< set>
< if test=”item.partname != null and item.partname != ‘’ “>
partname = #{item.partname},
< /if>
< if test=”item.remark != null “>
remark = #{item.remark},
< /if>
< /set>
where repairno =’${item.repairno}’ and partno =’${item.partno}’ and rid =’${item.rid}’
end else begin
insert into
RepairListPartDetail(repairno,itemno)
values(#{item.repairno},#{item.itemno},#{item.partno},#{item.partname},#{item.amount},#{item.taxprice},#{item.taxmoney},#{item.price},#{item.money},#{item.tax},#{item.costprice},
#{item.unitgroup},#{item.baseunit},#{item.unitdesc})
end
< /foreach>
< /insert>
< /update>
这里如果要访问主表,只能 ${list[0].repairno}
if (repairListPartDetails != null && repairListPartDetails.size() != 0) {
//第1种
int size = repairListPartDetails.size();
int num = 30;
int div = size / num;
int mod = size % num;
for(int i = 0;i < div; i++){
List newList = repairListPartDetails.subList(i num,i num + num);
repairMapper.saveRepairListPartDetail(newList);
}
if (mod > 0) {
List newList = repairListPartDetails.subList(size - mod, size);
repairMapper.saveRepairListPartDetail(newList);
}
}
2.void saveRepairListPackPayDetail(Map< String, Object> map);
< update id=”saveRepairListPackPayDetail” parameterType=”hashmap”>
< foreach collection=”list” item=”item” index=”index” separator=”;”>
if(not EXISTS(select 1 from RepairListPackPayDetail where repairno=’${item.repairno}’ and memberpackno=’${item.memberpackno}’ ) )
begin
insert into RepairListPackPayDetail (RepairNo, MemberPackNo, MemberPackName,CurrBalance,Currminlimit, UsePackMoney, CreateDate, ServiceDate)
values(#{item.repairno, jdbcType=VARCHAR},#{item.memberpackno, jdbcType=VARCHAR},#{item.memberpackname, jdbcType=VARCHAR},
#{item.currbalance, jdbcType=NUMERIC},#{item.currminlimit, jdbcType=NUMERIC},0,getdate(),null)
end
< /foreach>
< if test=”repairno != null and repairno != ‘’ “>
update repairlistmaster set IsMultiPackPay = 1 where repairno =’${repairno}’ //
< /if>
< /update>
int size = listDispatch.size();
int num = 40;
int div = size / num;
int mod = size % num;
for(int i = 0;i < div; i++){
List newList = listDispatch.subList(i num,i num + num);
Map< String,Object> map = new HashMap< String,Object>();
map.put(“repairno”,repairListMaster.getRepairno());
map.put(“list”,newList);
repairMapper.saveRepairListPackPayDetail(map);
}
if (mod > 0) {
List newList = listDispatch.subList(size - mod, size);
Map< String,Object> map = new HashMap< String,Object>();
map.put(“repairno”,repairListMaster.getRepairno());
map.put(“list”,newList);
repairMapper.saveRepairListPackPayDetail(map);
}