ibatis的批量插入和Mybatis的批量插入大体功能类似,但是具体的实现标签不一致,这主要是体现在了dtd的约束不一致:
ibatis的批量方式:
UpperCaseMap map1 = new UpperCaseMap();
List l = new ArrayList();
for(int i=0;i<3;i++){
OfficeDetailVO v = new OfficeDetailVO();
v.setBuildArea(1.1);
v.setBuildAreaQK(2.1);
l.add(v);
}
map1.put("LL", l);
map1.put("c1", "c1");
map1.put("c2", "c2");
map1.put("SAVEID", "batchtest");//给定后端xml配置sql对应的id
commonService.commonSaveService(map1);
对应的查询sql:
<insert id="batchtest" parameterClass="UpperCaseMap">
insert into office_realty_pre_base(pre_batch_no,belong_branch,path_type)
select coalesce(max(pre_batch_no),0)+1,#C1#,#C2# from office_realty_pre_base;
<iterate property="LL" conjunction=";">
insert into office_realty_pre_result (pre_batch_no,config_stand, item_num)
select coalesce(max(pre_batch_no),0)+1,#LL[].buildArea#,#LL[].buildAreaQK# from office_realty_pre_result
</iterate>
</insert>
对于Mybatis,批量插入功能实现基本一致:
List l = new ArrayList();
for(int i=0;i<3;i++){
Course c = new Course();
c.setCname("c"+i);
c.setCid(i);
l.add(c);
}
Map m1 = new HashMap();
m1.put("tetst", l);
<insert id="courseInsertBatch" parameterType="java.util.HashMap" >
<foreach collection="tetst" item="item" index="index" separator=";" >
insert into course(cid,cname) values (#{item.cid},#{item.cname})
</foreach>
</insert>