欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Mybatis批量插入数据(Mysql / Oracle)

程序员文章站 2022-07-12 13:52:48
...

 Mysql 的批量插入方式:

    <insert id="insertDataByBatch">
	   insert into RECORD_IMFORMATION
		(recordId,toUserId,roomName,fromUserId,entityType,
		 entityContent,url)
		 
		values
	   <foreach collection="records" item="record"  separator="," >
	   (#{record.recordId},#{record.toUserId},#{record.roomName},
		 #{record.fromUserId},#{record.entityType},
		 #{record.entityContent},#{record.url})
	   </foreach>
	</insert>

Oracle的批量插入方式:

    <insert id="insertDataByBatch">
	   insert into RECORD_IMFORMATION
		(recordId,toUserId,roomName,fromUserId,entityType,
		 entityContent,url)
		SELECT A.* FROM (
		   <foreach collection="records" item="record" index="index" separator="UNION ALL" >
		   		SELECT
			     #{record.recordId, jdbcType=VARCHAR} recordId,
			     #{record.toUserId, jdbcType=VARCHAR} toUserId,
			     #{record.roomName, jdbcType=VARCHAR} roomName,
				 #{record.fromUserId, jdbcType=VARCHAR} fromUserId,
				 #{record.entityType, jdbcType=VARCHAR} entityType,
			 	 #{record.entityContent, jdbcType=VARCHAR} entityContent,
			 	 #{record.url, jdbcType=VARCHAR} url
			 	FROM dual
		   </foreach>
	   ) A
	</insert>

 

相关标签: 配置