Mybatis批量插入数据
批量插入数据两种方式
1. 查询后插入
<insert id="saveInfo" parameterType="java.util.List" useGeneratedKeys="true" keyProperty="id"> insert into v_product_info ( `demand_number`, `product_name`, `product_type`, `sap_item_number`, `sgc_number`, `need_count`, `create_time`, `update_time` ) <foreach collection="list" item="item" index="index" separator="union"> select #{item.demandNumber}, #{item.productname}, #{item.productcode}, #{item.materialid}, #{item.sgccode}, #{item.number}, now(), now() from dual </foreach> </insert>
2. 使用拼接的数据
<insert id="saveBatch" parameterType="java.util.List"> <selectKey resultType ="java.lang.Integer" keyProperty= "id" order= "AFTER"> SELECT LAST_INSERT_ID() </selectKey> insert into p_product_info_rec ( `proid`, `assetid`, `sap_no`, `porduct_model`, `number`, `print_no_num`, `device_name`, `device_type`, `card_type`, `card_no_type`, `print_info`, `mark_info`, `write_info`, `remark`, `subpackage_no`, `number_code` ) values <foreach collection ="list" item="rec" index= "index" separator =","> ( #{rec.proid}, #{rec.assetid}, #{rec.sapNo}, #{rec.porductModel}, #{rec.number}, #{rec.printNoNum}, #{rec.deviceName}, #{rec.deviceType}, #{rec.cardType}, #{rec.cardNoType}, #{rec.printInfo}, #{rec.markInfo}, #{rec.writeInfo}, #{rec.remark}, #{rec.subpackageNo}, #{rec.numberCode} ) </foreach > </insert>