ibatis批量导入mysql数据库
工作中需要批量导入excel数据到mysql数据库中
excel数据量比较大,一次大概导入1万多条
用传统的for循环导入mysql数据库的话,非常耗时,大概3分钟左右
所有就需要用到批处理,核心算法如下
/** * 批量插入功能 */ @SuppressWarnings("unchecked") public void insertBatch(final List<WinddataBo> list) { SqlMapClient sqlMapClientTemplate = baseDao.getSqlMapClient(); try { sqlMapClientTemplate.startTransaction();//开启事务 /**事务todo start***/ /**先执行删除所有**/ sqlMapClientTemplate.delete("com.zero2ipo.ylcf.winddata.deleteAll"); sqlMapClientTemplate.insert("com.zero2ipo.ylcf.winddata.insertBatch",list); /**事务todo end***/ sqlMapClientTemplate.commitTransaction();//提交事务 } catch (SQLException e) { e.printStackTrace(); }finally { try { sqlMapClientTemplate.endTransaction();//事务完成 } catch (SQLException e) { try { sqlMapClientTemplate.getCurrentConnection().rollback(); //事务回滚 } catch (SQLException e1) { e1.printStackTrace(); } e.printStackTrace(); } } }
ibatis sql文件如下
<!--批量插入--> <insert id ="com.zero2ipo.ylcf.winddata.insertBatch" parameterClass ="java.util.List"> <![CDATA[ insert into winddata(CRETE_DATE , SHOUPANJIA , COLUMN1,COLUMN2 ) values ]]> <iterate conjunction ="," > <![CDATA[ (#list[].creteDate#,#list[].shoupanjia#,#list[].column1#,#list[].column2#) ]]> </iterate > </insert >
1次导入excel1万多条数据,耗时大概3秒钟
部分效果图
相关推荐
lbyd0 2020-11-17
sushuanglei 2020-11-12
腾讯soso团队 2020-11-06
gaobudong 2020-11-04
yangkang 2020-11-09
85477104 2020-11-17
KANSYOUKYOU 2020-11-16
wushengyong 2020-10-28
lizhengjava 2020-11-13
星月情缘 2020-11-13
huangxiaoyun00 2020-11-13
luyong0 2020-11-08
Apsaravod 2020-11-05
PeterChangyb 2020-11-05
wwwjun 2020-11-02
gyunwh 2020-11-02
EchoYY 2020-10-31
dingyahui 2020-10-30