mybatis bulk inserted into the main return key ID into a bulk problems unsuccessful

This blog talking about the example of bulk inserts

dao layer framework with mybatis the very beginning of my bulk insert is an artificial bulk of it is similar to many bars statements directly insert into a spell and then run directly

It found that such efficiency is really very low I 285 data into a test needs to do more than 10S

And when I modify the sql wording:

<insert id="addFlowTableItemNullList" parameterType="java.util.List" useGeneratedKeys="true" keyProperty="id"  >
  	
  	
  		insert into cus_sh_gongziceshi
  		( applicant,createtime,isdel,
  		<foreach collection="list" item="table" index="a" >
  		<if test="a == 0">
  			<foreach collection="table.map.keys" item="key1" separator=",">
  				${key1}
  			</foreach>
  		</if>
  		</foreach>
  		) values
  		
  		<foreach collection="list" item="table2" separator=",">
  			(${table2.applicant},#{table2.time,jdbcType=TIMESTAMP},0 ,
  				<foreach collection="table2.map.keys" item="key2" separator=",">
  					${table.map[key2]}
  				</foreach>
  			)
  		</foreach>
  		
  		
  </insert>
The effect is probably insert into table (a, bc) values ​​(1,2,3), (1,2,3)

This insert statement is the real way to perform bulk insert only need about 50 times faster 0.2s

Please note that such an approach must be foreach in the collection list or will be error Another very important point is not supported by default mybatis bulk inserts

You need to add allowMultiQueries = true when your connection information jdbc URL

However, use of this insert has also brought a number of other issues

Such as how to return to the main key is inserted in the ID string types of data error and other


First of all on how to return the primary key ID after I read a lot of information found on the Internet mybatis version I am using does not support bulk insert a little low return only above mybatis3.3.1 version ID before they can

This is mybatis3.3.1 integrated spring jar package mybatis-spring

This is a jar package download link mybatis 3.3.1 of mybatis3.3.1

This is mybatis3.4 spring and the corresponding integration of maven dependent mybatis

<dependency>
    <groupId>org.mybatis</groupId>
    <artifactId>mybatis</artifactId>
    <version>3.4.0</version>
</dependency>
<dependency>
    <groupId>org.mybatis</groupId>
    <artifactId>mybatis-spring</artifactId>
    <version>1.3.0</version>
</dependency>
You need to add on to return the primary key ID in the insert tab
 useGeneratedKeys="true" keyProperty="id"
keyProperty that you want to return to the primary key attribute name of acceptable ID


In a bulk insert there is often overlooked question: when inserting string type of data to add '' single quotes

Otherwise you insert Chinese words will be reported com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Unknown column '??????' in 'field list'



Published 16 original articles · won praise 21 · views 30000 +

Guess you like

Origin blog.csdn.net/q690080900/article/details/77247650