阿里云Java 杂谈

mybatis执行批量操作

2019-03-15  本文已影响1人  WANGGGGG
// 引入template
@Autowired
private SqlSessionTemplatesqlSessionTemplate;
public void saveAndUpdate(List list)throws Exception {
// 当前会话开启批量模式
SqlSession sqlSession =sqlSessionTemplate.getSqlSessionFactory().openSession(ExecutorType.BATCH, false);
// 从会话中获取到mapper
    CustomerInsightMapper mapper = sqlSession.getMapper(CustomerInsightMapper.class);
    try {
for (CustomerInsightInfo info : list) {
mapper.saveAndUpdate(info);
        }
//提交
sqlSession.commit();
        sqlSession.clearCache();
    }catch (Exception e) {
sqlSession.rollback();
        throw new Exception(e);
    }finally {
sqlSession.close();
    }
}
上一篇 下一篇

猜你喜欢

热点阅读