Commit 8047500a authored by eddie.woo's avatar eddie.woo

modify batch insert

parent e84c5a33
package pwc.taxtech.atms.dao;
import org.apache.ibatis.session.ExecutorType;
import org.apache.ibatis.session.SqlSession;
import org.mybatis.spring.SqlSessionTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import pwc.taxtech.atms.entity.CellTemplateConfig;
import pwc.taxtech.atms.entity.CellTemplateConfigExample;
import javax.annotation.Resource;
import java.util.List;
@Service
public class CellTemplateConfigDao {
@Autowired
private CellTemplateConfigMapper mapper;
@Resource(name = "sqlSessionTemplate")
private SqlSessionTemplate sqlSessionTemplate;
public List<CellTemplateConfig> getByTemplateId(Long id) {
CellTemplateConfigExample example = new CellTemplateConfigExample();
......@@ -19,5 +25,15 @@ public class CellTemplateConfigDao {
return mapper.selectByExample(example);
}
public void batchInsert(List<CellTemplateConfig> list) {
long start = System.currentTimeMillis();
SqlSession sqlSession = sqlSessionTemplate.getSqlSessionFactory().openSession(ExecutorType.BATCH);
for (CellTemplateConfig cellTemplate : list) {
mapper.insertSelective(cellTemplate);
}
sqlSession.commit();
long end = System.currentTimeMillis();
System.out.println("---------------batch insert: count:" + list.size() + " time: " + (start - end) + "---------------");
}
}
package pwc.taxtech.atms.dao;
import org.apache.ibatis.session.ExecutorType;
import org.apache.ibatis.session.SqlSession;
import org.mybatis.spring.SqlSessionTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import pwc.taxtech.atms.entity.CellTemplate;
import pwc.taxtech.atms.entity.CellTemplateExample;
import javax.annotation.Resource;
import java.util.List;
@Service
public class CellTemplateDao {
@Autowired
private CellTemplateMapper mapper;
@Resource(name = "sqlSessionTemplate")
private SqlSessionTemplate sqlSessionTemplate;
public List<CellTemplate> getByTemplateId(Long id) {
CellTemplateExample example = new CellTemplateExample();
......@@ -19,5 +25,14 @@ public class CellTemplateDao {
return mapper.selectByExample(example);
}
public void batchInsert(List<CellTemplate> list) {
long start = System.currentTimeMillis();
SqlSession sqlSession = sqlSessionTemplate.getSqlSessionFactory().openSession(ExecutorType.BATCH);
for (CellTemplate cellTemplate : list) {
mapper.insertSelective(cellTemplate);
}
sqlSession.commit();
long end = System.currentTimeMillis();
System.out.println("---------------batch insert: count:" + list.size() + " time: " + (start - end) + "---------------");
}
}
......@@ -371,9 +371,10 @@ public class TemplateGroupServiceImpl extends AbstractService {
}
}
List<List<CellTemplate>> tmpList = CommonUtils.subListWithLen(cellTemplateList, CommonUtils.BATCH_NUM);
tmpList.forEach(list -> cellTemplateMapper.batchInsert2(list));
// tmpList.forEach(list -> cellTemplateMapper.batchInsert2(list));
tmpList.forEach(list -> cellTemplateDao.batchInsert(list));//todo 批量插入优化
List<List<CellTemplateConfig>> tmpConfigList = CommonUtils.subListWithLen(cellTemplateConfigList, CommonUtils.BATCH_NUM);
tmpConfigList.forEach(list -> cellTemplateConfigMapper.batchInsert(list));
tmpConfigList.forEach(list -> cellTemplateConfigDao.batchInsert(list));
}
} catch (Exception e) {
logger.error("importTemplateExcelFile error.", e);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment