注:本文章项目背景为使用excel文档批量导入Java数据。
原404条数据导入时间为30秒+,加入多线程后为12秒+,加入Mybatis 批量处理后优化至1.8秒。
Java多线程配置
/**
* 创建定长线程池
*/
ExecutorService fixedThreadPool = Executors.newFixedThreadPool(5);
/**
* 存放多线程submit数据
*/
List<Future<Void>> futures = new ArrayList<>();
List<CsWordGroup> csWordGroups = new ArrayList<>();
/**
* 取Excel表传入信息循环
*/
for (int index = 0; index < csWordBook.getFeedbackData().size(); index++) {
/**
* 使用利用匿名内部类实现多线程 用submit执行任务 用Callable返回值 Callable返回值固定为Future类型
*/
int finalIndex = index;
futures.add(fixedThreadPool.submit(new Callable<Void>() {
@Override
public Void call() throws Exception {
try {
Map<String, Object> item = csWordBook.getFeedbackData().get(finalIndex);
/**
* 赋值单词/词组信息表并循环添加
*/
CsWordGroup csWordGroup = new CsWordGroup();
csWordGroup.setWgWbId(csWordBook.getWbId());
csWordGroup.setWgWord(item.get("单词/词组").toString());
csWordGroup.setWgAbbr(item.get("单词缩写").toString());
csWordGroup.setWgInterpretation(item.get("单词释义(多个意思以英文,隔开)").toString());
csWordGroup.setWgCreateBy(csWordBook.getWbCreateBy());
csWordGroups.add(csWordGroup);
} catch (Exception e) {
// 处理异常
e.printStackTrace();
}
return null;
}
}));
}
/**
* 批量执行添加 缩减添加时间
*/
rows += csWordGroupMapper.insertCsWordGroupForeach(csWordGroups);
/**
* 关闭线程池执行,子线程不结束线程池不会关闭
* 执行后退出方法执行回收掉线程
*/
fixedThreadPool.shutdown();
使用foreach 批量添加:
mapper.java:
public int insertCsWordGroupForeach(List<CsWordGroup> csWordGroups);
由于mapper.java
中未指定参数,所以默认xml
中的collection
为list
<insert id="insertCsWordGroupForeach">
insert into cs_word_group
(
wg_wb_id,
wg_word,
wg_abbr,
wg_interpretation,
wg_create_by,
wg_create_time,
wg_is_del
)
<foreach collection="list" item="item" open="values" separator=",">
(
#{item.wgWbId},
#{item.wgWord},
#{item.wgAbbr},
#{item.wgInterpretation},
#{item.wgCreateBy},
now(),
0
)
</foreach>
</insert>
批量修改:(此处注意判断时加item,以及注意逗号问题)
<update id="batchUpdate">
<foreach collection="list" item="item" separator=";">
update table
set
pdjlid = #{item.pdjlid}
<if test="item.pmggnm != null">,pmggnm = #{item.pmggnm}</if>
<if test="item.pdsl != null">,pdsl = #{item.pdsl}</if>
<if test="item.sysl != null">,sysl = #{item.sysl}</if>
<if test="item.zwsl != null">,zwsl = #{item.zwsl}</if>
<if test="item.zzsl != null">,zzsl = #{item.zzsl}</if>
<if test="item.pysl != null">,pysl = #{item.pysl}</if>
<if test="item.pksl != null">,pksl = #{item.pksl}</if>
<if test="item.qmkc != null">,qmkc = #{item.qmkc}</if>
<if test="item.qcsl != null">,qcsl = #{item.qcsl}</if>
<if test="item.idx != null">,idx = #{item.idx}</if>
<if test="item.updateBy != null">,update_by = #{item.updateBy}</if>
<if test="item.updateTime != null">,update_time = #{item.updateTime}</if>
<if test="item.updateIp != null">,update_ip = #{item.updateIp}</if>
<if test="item.remark != null and item.remark != ''">,remark = #{item.remark}</if>
<if test="item.version != null">,version = #{item.version}</if>
<if test="item.delFlag != null and item.delFlag != ''">,del_flag = #{item.delFlag}</if>
<if test="item.ljPksl != null">,lj_pksl = #{item.ljPksl}</if>
where
pdjlmxid = #{item.pdjlmxid}
</foreach>
</update>