首先看看批处理的mapper.xml文件
-
<insert id="insertbatch" parameterType="java.util.List">
-
<selectKey keyProperty="fetchTime" order="BEFORE"
-
resultType="java.lang.String">
-
SELECT CURRENT_TIMESTAMP()
-
</selectKey>
-
insert into kangaiduoyaodian ( depart1, depart2, product_name,
-
generic_name, img, product_specification, unit,
-
approval_certificate, manufacturer, marketPrice, vipPrice,
-
website, fetch_time, productdesc ) values
-
<foreach collection="list" item="item" index="index"
-
separator=",">
-
( #{item.depart1}, #{item.depart2}, #{item.productName},
-
#{item.genericName}, #{item.img},
-
#{item.productSpecification}, #{item.unit},
-
#{item.approvalCertificate}, #{item.manufacturer},
-
#{item.marketprice}, #{item.vipprice}, #{item.website},
-
#{fetchTime}, #{item.productdesc} )
-
</foreach>
-
</insert>
在批处理中,我发现有几个需要注意的问题
1、主键的自动获取,在insert中添加useGeneratedKeys=”true” keyProperty=”id”这两个属性无效,并且或中断数据插入,如果id是数据库自增的话,可以什么都不写,在插入的语句中去除主键属性,还有就是利用
-
<selectKey keyProperty="id" order="BEFORE"
-
resultType="java.lang.Integer">
-
SELECT LAST_INSERT_ID()
-
</selectKey>
注意:<selectKey > 标签在insert下只能存在一个;批处理的时候不适合使用<selectKey >,主键自增最好,或者指定
2,插入时间的获取如上面所示,我用的是MySQL,只要是mysql函数都可以拿来使用,插入时间和主键都是mysql函数中的一个。。。
mybatis我也是在小试牛刀,如有不妥之处,请见谅。。。。