论坛首页 Java企业应用论坛

使用Mybatis做批量插入

浏览 14593 次
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
作者 正文
   发表时间:2012-03-30  
最近有个需求,将excel的数据导入的数据库的这个一个操作。
工作主要分为:解析excel,将excel中的数据单条循环插入数据库。
使用框架:mybatis+spring
使用过Mybatis的人都知道,自动生成的Mapper里是不支持批量插入的,也不支持SQL。这个让我有点小小的郁闷,网上查资料发现对这方面的资料颇少。于是决定写一篇blog案例分享心得。
或许有人要问既然Mybatis既然支持插入了,为何非要要使用批量插入。我这里的excel中的数据最少也是上W条,如果是使用单条循环插入的话会对数据库造成很大的负荷状态,数据库的连接资源是有限的,循环插入的时候会直接的影响其它的数据库操作。
pojo
package me.gall.business.model.mybatis.bean;

/**
 * @author Quinn He
 * @dateTime 2012-2-9 下午4:35:18
 *
 */
public class ApkStatisticRaw {

	private Integer id;
	private String uuid;
	private String apkId;
	private String eventId;
	private Integer supplyId;
	private Integer channelId;
	private String fileUploadRecordId;
	private String productName;
	private String content;
	private Long time;
	private Integer numbers;
	private Integer status;
	private String creator;
	private Long createTime;
	private String other;

	set...
         get...


}


interface
import me.gall.business.model.mybatis.bean.ApkStatisticRaw;

/**
 * @author Quinn He
 * @dateTime 2012-2-20 下午7:48:39
 */
public interface ApkStatisticRawExtMapper {

/**
	 * 专门针对在导入CSV文件时
	 * 频繁操作数据库造成的数据库并发问题
	 * 固此方法为批量插入方法
	 * 
	 * @author Quinn He
	 * @dateTime 2012-3-30 上午11:34:22
	 * @param list
	 */
	void batchInsert(List<ApkStatisticRaw> list);
}

再看看XML里的操作
<select id="batchInsert" parameterType="java.util.List">
		insert into apk_statistic_raw
		(uuid,apk_id,event_id,supply_id,channel_id,file_upload_record_id,product_name,content,time,numbers,status,creator,create_time,other
		)values
		<foreach collection="list" item="item" index="index"
			separator=",">
			(#{item.uuid,jdbcType=CHAR},#{item.apkId,jdbcType=CHAR},#{item.eventId,jdbcType=CHAR},#{item.supplyId,jdbcType=INTEGER},#{item.channelId,jdbcType=INTEGER}
			,#{item.fileUploadRecordId,jdbcType=CHAR},#{item.productName,jdbcType=VARCHAR},#{item.content,jdbcType=VARCHAR},#{item.time,jdbcType=BIGINT},
			#{item.numbers,jdbcType=INTEGER},#{item.status,jdbcType=INTEGER},#{item.creator,jdbcType=VARCHAR},#{item.createTime,jdbcType=BIGINT},#{item.other,jdbcType=VARCHAR}
			)
		</foreach>
	</select>
	<select id="batchInsertFailData" parameterType="java.util.List">
		insert into apk_statistic_raw_fail
		(uuid,file_upload_record_id,time,event,product_name,version,number,percentage,create_time,status,creator,other
		)values
		<foreach collection="list" item="item" index="index"
			separator=",">
			(#{item.uuid,jdbcType=CHAR},#{item.fileUploadRecordId,jdbcType=CHAR},#{item.time,jdbcType=VARCHAR},#{item.event,jdbcType=VARCHAR},#{item.productName,jdbcType=VARCHAR},#{item.version,jdbcType=VARCHAR},
			#{item.number,jdbcType=INTEGER},#{item.percentage,jdbcType=VARCHAR},#{item.createTime,jdbcType=BIGINT},#{item.status,jdbcType=INTEGER},#{item.creator,jdbcType=VARCHAR},#{item.other,jdbcType=VARCHAR}
			)
		</foreach>
	</select>

如此简单的操作,我也不做多说吧。相信都能看懂#{item.uuid,jdbcType=CHAR}其中uuid是对象的字段,CHAR是对应的数据库字段类型
   发表时间:2012-04-02  
插入操作原来还可以用<select></select>啊?
0 请登录后投票
   发表时间:2012-04-02  
恩,想法不错, 但是数据量多的话,  数据库不接受big query的,  还是老老实实用  PreparedStatem.addBatch吧
0 请登录后投票
   发表时间:2012-04-02  
Rhain 写道
插入操作原来还可以用<select></select>啊?

同问
0 请登录后投票
   发表时间:2012-04-02  

spring +mybatis集成,  怎么做批量插入?

 

0 请登录后投票
   发表时间:2012-09-01   最后修改:2012-09-01
楼主,按你的例子做,为什么在后台打印出来看null的呢?list中每个实体打印出来看过,是有值的
0 请登录后投票
论坛首页 Java企业应用版

跳转论坛:
Global site tag (gtag.js) - Google Analytics