mybatis中同一个id多条数据批量插入

  项目中使用的mybatis数据库是用的Oracle,在将数据插入从表的时候因为一个id对应了多条记录,为了不影响数据库性能,所以需要将数据一次性插入表中。

  Java中使用map进行传值,分别存放id和list

            List<AddressSubtable> list2 = mailDetailService.queryOrgPsgType(address);
                    List<AddressSubtable> list3 = addressLibraryService.querySubtable(id);
                    //去掉地址库从表已有的数据
                    list2.removeAll(list3);
                    if (list2.size()>0) {
                        Map<String, Object> map2 = new HashMap<String, Object>();
                        map2.put("id", id);
                        map2.put("addressSubtableList", list2);
                        addressLibraryService.insertSubtable(map2);
                    } 

  xml文件里代码:

<insert id="insertSubtable" parameterType="java.util.HashMap">
        insert into tb_org_psg_addr 
               (v_id,v_mail_type,v_deal_type,v_org,v_road) 
               ( 
               <foreach collection ="addressSubtableList" item="addressSubtable" index= "index" separator ="UNION ALL">
                 select
                 #{id,jdbcType=VARCHAR}, 
                 #{addressSubtable.mailType},
                 #{addressSubtable.dealType},
                 #{addressSubtable.org},
                 #{addressSubtable.road}
                 from dual
            </foreach >
            ) 
    </insert>

猜你喜欢

转载自www.cnblogs.com/suiyuebulaorenxin/p/12674675.html