Need to batch import excel data into mysql database at work
The amount of excel data is relatively large, about 10,000 items are imported at a time
Using the traditional for loop to import the mysql database is very time-consuming, about 3 minutes
All need to use batch processing, the core algorithm is as follows
/** * Bulk insert function */ @SuppressWarnings("unchecked") public void insertBatch(final List<WinddataBo> list) { SqlMapClient sqlMapClientTemplate = baseDao.getSqlMapClient(); try { sqlMapClientTemplate.startTransaction();//Start transaction /**Transaction todo start***/ /**First execute delete all**/ sqlMapClientTemplate.delete("com.zero2ipo.ylcf.winddata.deleteAll"); sqlMapClientTemplate.insert("com.zero2ipo.ylcf.winddata.insertBatch",list); /**Transaction todo end***/ sqlMapClientTemplate.commitTransaction();//Commit the transaction } catch (SQLException e) { e.printStackTrace (); }finally { try { sqlMapClientTemplate.endTransaction();//The transaction is completed } catch (SQLException e) { try { sqlMapClientTemplate.getCurrentConnection().rollback(); //Transaction rollback } catch (SQLException e1) { e1.printStackTrace(); } e.printStackTrace (); } } }
The ibatis sql file is as follows
<!--Batch Insert--> <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 >
It takes about 3 seconds to import more than 10,000 pieces of data into excel at a time
Part of the renderings