mybatis数据批量插入

来源:互联网 发布:人工智能典型案例 编辑:程序博客网 时间:2024/06/10 15:14
 

mybatis数据批量插入

分类: mybatis java 11209人阅读 评论(6) 收藏 举报
insertmysqlwebsite数据库list

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

[html] view plaincopy
  1. <insert id="insertbatch" parameterType="java.util.List">  
  2.     <selectKey keyProperty="fetchTime" order="BEFORE"  
  3.         resultType="java.lang.String">  
  4.         SELECT CURRENT_TIMESTAMP()  
  5.     </selectKey>  
  6.     insert into kangaiduoyaodian ( depart1, depart2, product_name,  
  7.     generic_name, img, product_specification, unit,  
  8.     approval_certificate, manufacturer, marketPrice, vipPrice,  
  9.     website, fetch_time, productdesc ) values  
  10.     <foreach collection="list" item="item" index="index"  
  11.         separator=",">  
  12.         ( #{item.depart1}, #{item.depart2}, #{item.productName},  
  13.         #{item.genericName}, #{item.img},  
  14.         #{item.productSpecification}, #{item.unit},  
  15.         #{item.approvalCertificate}, #{item.manufacturer},  
  16.         #{item.marketprice}, #{item.vipprice}, #{item.website},  
  17.         #{fetchTime}, #{item.productdesc} )  
  18.     </foreach>  
  19.   </insert>  

在批处理中,我发现有几个需要注意的问题

1、主键的自动获取,在insert中添加useGeneratedKeys=”true” keyProperty=”id”这两个属性无效,并且或中断数据插入,如果id是数据库自增的话,可以什么都不写,在插入的语句中去除主键属性,还有就是利用

[html] view plaincopy
  1. <selectKey keyProperty="id" order="BEFORE"  
  2.         resultType="java.lang.Integer">  
  3.         SELECT LAST_INSERT_ID()  
  4.   </selectKey>  
注意<selectKey > 标签在insert下只能存在一个;批处理的时候不适合使用<selectKey >,主键自增最好,或者指定

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



mybatis我也是在小试牛刀,如有不妥之处,请见谅。。。。

原创粉丝点击