您的位置:首页 > 其它

mybatis数据批量插入

2013-10-28 14:54 239 查看


mybatis数据批量插入

分类: mybatis java2011-09-09
12:03 11209人阅读 评论(6) 收藏 举报

insertmysqlwebsite数据库list

首先看看批处理的mapper.xml文件

[html] view
plaincopy

<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是数据库自增的话,可以什么都不写,在插入的语句中去除主键属性,还有就是利用

[html] view
plaincopy

<selectKey keyProperty="id" order="BEFORE"

resultType="java.lang.Integer">

SELECT LAST_INSERT_ID()

</selectKey>

注意:<selectKey > 标签在insert下只能存在一个;批处理的时候不适合使用<selectKey >,主键自增最好,或者指定

2,插入时间的获取如上面所示,我用的是mysql,只要是mysql函数都可以拿来使用,插入时间和主键都是mysql函数中的一个。。。

mybatis我也是在小试牛刀,如有不妥之处,请见谅。。。。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: