zoukankan      html  css  js  c++  java
  • mybatis数据批量插入

    首先看看批处理的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我也是在小试牛刀,如有不妥之处,请见谅。。。。

  • 相关阅读:
    Objective-C实用类和协议
    KVC(Key-Value-Coding)和KVO(Key-Value-Observer)
    Xcode
    IOS模拟器
    沙盒机制
    UIScrollView
    NSPredicate
    输入控件适应键盘
    10步成为专业iOS开发者——新手向,从零起步
    2015 年五大移动端设计趋势
  • 原文地址:https://www.cnblogs.com/hzcya1995/p/13318057.html
Copyright © 2011-2022 走看看