首先在你的dao中需要继承org.springframework.orm.ibatis.support.SqlMapClientDaoSupport
然后在代码中调用getSqlMapClientTemplate方法, 覆写SqlMapClientCallback类中的doInSqlMapClient的方法
public void testBatch(final List<String> list) throws DataAccessException{
this.getSqlMapClientTemplate().execute(new SqlMapClientCallback(){
public Object doInSqlMapClient(SqlMapExecutor executor)
throws SQLException {
executor.startBatch();
int batch = 0;
for(String str:list){
//相关业务处理
batch++;
//每500条批量提交一次。
if(batch==500){
executor.executeBatch();
batch = 0;
}
}
executor.executeBatch();
return null;
}
});
}
批量插入减少了获取数据库连接池的次数,经过测试可以提高60%到70%的性能
然后在代码中调用getSqlMapClientTemplate方法, 覆写SqlMapClientCallback类中的doInSqlMapClient的方法
public void testBatch(final List<String> list) throws DataAccessException{
this.getSqlMapClientTemplate().execute(new SqlMapClientCallback(){
public Object doInSqlMapClient(SqlMapExecutor executor)
throws SQLException {
executor.startBatch();
int batch = 0;
for(String str:list){
//相关业务处理
batch++;
//每500条批量提交一次。
if(batch==500){
executor.executeBatch();
batch = 0;
}
}
executor.executeBatch();
return null;
}
});
}
批量插入减少了获取数据库连接池的次数,经过测试可以提高60%到70%的性能