由于系统中一次性需要导入1000条数据,为防止超时以及长时间占用数据库连接,采取了批量更新插入。
1.后台代码每50条,与数据库进行一次交互
//批量插入/更新
int dataLimitNum = 50;// 限制分批条数
int i = 0;
while (i < list.size()) {
int from = i;
int to = i + dataLimitNum;
if (to >= list.size()) {
to = list.size();
}
List
if (partList.isEmpty()) {
break;
}
i = to;
userDao.insertSelectiveBatch(partList);
}
2.批量插入
insert into User
ID,
USER_NAME,
AGE,
Mobile,
CREATED,
CREATE_BY,
UPDATED,
UPDATE_BY,
#{item.id,jdbcType=BIGINT},
#{item.userName,jdbcType=VARCHAR},
#{item.age,jdbcType=BIGINT},
#{item.mobile,jdbcType=VARCHAR},
#{item.created,jdbcType=TIMESTAMP},
#{item.createBy,jdbcType=VARCHAR},
#{item.updated,jdbcType=TIMESTAMP},
#{item.updateBy,jdbcType=VARCHAR},
3.批量更新
update USER
ID= #{item.id,jdbcType=BIGINT},
UserName= #{item.userName,jdbcType=VARCHAR},
AGE= #{item.age,jdbcType=BIGINT},
Mobile= #{item.mobile,jdbcType=VARCHAR},
CREATED = #{item.created,jdbcType=TIMESTAMP},
CREATE_BY = #{item.createBy,jdbcType=VARCHAR},
UPDATED = #{item.updated,jdbcType=TIMESTAMP},
UPDATED_BY = #{item.updatedBy,jdbcType=VARCHAR},
where ID = #{item.id,jdbcType=BIGINT}