美文网首页阿里云Java 杂谈
mybatis执行批量操作

mybatis执行批量操作

作者: WANGGGGG | 来源:发表于2019-03-15 10:20 被阅读1次
// 引入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();
    }
}

相关文章

网友评论

    本文标题:mybatis执行批量操作

    本文链接:https://www.haomeiwen.com/subject/bftlmqtx.html