批量插入sql語句: INSERT INTO table (field1,field2,field3) VALUES ('a',"b","c"), ('a',"b","c"),('a',"b","c") mybatis通過foreach迴圈拼裝瞭如上的sql語句。 一、xml 說明: mysql批量 ...
批量插入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