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

    第四章 mybatis批量insert
    批量插入sql语句:
    
    INSERT INTO table (field1,field2,field3) VALUES ('a',"b","c"), ('a',"b","c"),('a',"b","c")
    
    mybatis通过foreach循环拼装了如上的sql语句。
    
    一、xml
    
    复制代码
     1 <?xml version="1.0" encoding="UTF-8" ?>
     2 <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
     3 <mapper
     4     namespace="com.xxx.mapper.XXXRecordMapper">
     5     <resultMap id="BaseResultMap" type="com.xxx.model.XXXRecord">
     6         <id column="AutoId" property="autoid" jdbcType="BIGINT" />
     7         <result column="UserId" property="userid" jdbcType="BIGINT" />
     8         <result column="NoticedTime" property="noticedtime" jdbcType="TIMESTAMP" />
     9     </resultMap>
    10     <sql id="Base_Column_List">
    11         AutoId, UserId, NoticedTime
    12     </sql>
    13     <!-- myself:批量插入 -->
    14     <insert id="insertBatch" parameterType="java.util.List">
    15         insert into T_XXXRecord (AutoId, UserId, NoticedTime) values
    16         <foreach collection="list" item="item" index="index" separator=",">
    17             (#{item.autoid,jdbcType=BIGINT},
    18              #{item.userid,jdbcType=BIGINT},
    19              #{item.noticedtime,jdbcType=TIMESTAMP})
    20         </foreach>
    21     </insert>
    22 </mapper>
    复制代码
    说明:
    
    mysql批量插入的限制是一次批量:1M
    我这里插入的List,如上就好,如果是其他结构,查看这篇博客:http://www.cnblogs.com/admol/articles/4248159.html
    collection属性:
    1.如果传入的是单参数且参数类型是一个List的时候,collection属性值为list
    
    2.如果传入的是单参数且参数类型是一个array数组的时候,collection的属性值为array
    
    3.如果传入的参数是多个的时候,我们就需要把它们封装成一个Map了,当然单参数也可以封装成map
    
    分类: Mybatis相关
    

      

  • 相关阅读:
    古谚、评论与论断、名篇与名言
    重读《西游记》
    重读《西游记》
    命名之法 —— 时间、季节、地点
    命名之法 —— 时间、季节、地点
    文言的理解 —— 古时的称谓、别称、别名
    文言的理解 —— 古时的称谓、别称、别名
    Oracle GoldenGate for Oracle 11g to PostgreSQL 9.2.4 Configuration
    瀑布 敏捷 文档
    POJ 1325 ZOJ 1364 最小覆盖点集
  • 原文地址:https://www.cnblogs.com/leigepython/p/10153773.html
Copyright © 2011-2022 走看看