`
hekuilove
  • 浏览: 156205 次
  • 性别: Icon_minigender_1
  • 来自: 魔都
社区版块
存档分类
最新评论

使用Mybatis做批量插入

阅读更多
最近有个需求,将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>
	

如此简单的操作,我也不做多说吧。相信都能看懂#{item.uuid,jdbcType=CHAR}其中uuid是对象的字段,CHAR是对应的数据库字段类型
分享到:
评论
15 楼 renzhengzhi 2015-08-24  
mybatis的这个批量insert为什么不是在一个事务里?
14 楼 hekuilove 2014-03-20  
恋上你的味道 写道
花家狗V587 一统江湖

聋家狗 FU*CK YOU
13 楼 恋上你的味道 2014-03-20  
花家狗V587 一统江湖
12 楼 hekuilove 2014-01-21  
qianmo666 写道
最大插入量是多少?

我当时的业务是导入csv到数据库里,当时一个excel多则上百万条,少则几万条
,由于太多,我就1000条一次执行一次批量,插完为止。。
当时用的数据库是MySQL,我最近用oracle也有一个类似的情况想这样干结果不行了,oracle不支持这种语法
11 楼 qianmo666 2014-01-06  
最大插入量是多少?
10 楼 RyanLu 2013-01-09  
请问如果我的ID是自增长的,我批量插入的时候,每个domain object的id,能返回吗?尝试过在里面加<select key>好像不起作用。我用是sql server 2005

  
<insert id="batchInsert" parameterType="java.util.List">
    <selectKey keyProperty="id" order="AFTER" resultType="java.lang.Integer">  
        SELECT SCOPE_IDENTITY() AS id  
    </selectKey>  
   	<foreach collection="list" item="item" index="index" separator=" ">
      insert into itd.Approval (ApplicationId, ApproverId, ApproverProjectId, Priority, Status,  ApprovedTime, Comments) select #{item.applicationId,jdbcType=INTEGER}, #{item.approverId,jdbcType=INTEGER}, #{item.approverProjectId,jdbcType=INTEGER}, #{item.priority,jdbcType=INTEGER}, #{item.status,jdbcType=CHAR}, #{item.approvedTime,jdbcType=TIMESTAMP}, #{item.comments,jdbcType=NVARCHAR}
    </foreach>
  </insert>
9 楼 zq93741833 2012-11-23  
小五哥丶 写道
妹的,全是SELECT,

select标签里面是可以包含insert语句
8 楼 hekuilove 2012-11-06  
hanzhicheng754 写道
哥们,你插入的最大量是多少?

我是把大量的数据拆开来N个1000条来执行的,执行N次
7 楼 hanzhicheng754 2012-11-05  
哥们,你插入的最大量是多少?
6 楼 hekuilove 2012-10-24  
natian306 写道
select? 确定不是insert? 楼主是否直接用的mapper操作? 是否有dao的实现类?

当然确定,select一样一样的。。insert也行。。没实现类
5 楼 natian306 2012-10-21  
select? 确定不是insert? 楼主是否直接用的mapper操作? 是否有dao的实现类?
4 楼 hekuilove 2012-10-11  
kingliu 写道
哥们儿,你用的什么数据库,我用sybase发现不行啊

我的是MySQL
3 楼 kingliu 2012-10-11  
哥们儿,你用的什么数据库,我用sybase发现不行啊
2 楼 j315321 2012-05-22  
能不能把批量操作的java方法贴出来看看,谢……
1 楼 小五哥丶 2012-04-20  
妹的,全是SELECT,

相关推荐

Global site tag (gtag.js) - Google Analytics