mybatis數據批量插入

首先看看批處理的mapper.xml文件
html

<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>

在批處理中,我發現有幾個須要注意的問題java

一、主鍵的自動獲取,在insert中添加useGeneratedKeys=」true」 keyProperty=」id」這兩個屬性無效,而且或中斷數據插入,若是id是數據庫自增的話,能夠什麼都不寫,在插入的語句中去除主鍵屬性,還有就是利用mysql

<selectKey keyProperty="id" order="BEFORE"
  		resultType="java.lang.Integer">
  		SELECT LAST_INSERT_ID()
  </selectKey>
注意 <selectKey > 標籤在insert下只能存在一個;批處理的時候不適合使用<selectKey >,主鍵自增最好,或者指定

2,插入時間的獲取如上面所示,我用的是mysql,只要是mysql函數均可以拿來使用,插入時間和主鍵都是mysql函數中的一個。。。web



mybatis我也是在小試牛刀,若有不妥之處,請見諒。。。。
sql



版權聲明:本文爲博主原創文章,未經博主容許不得轉載。數據庫

相關文章
相關標籤/搜索