使用SqlSessionTemplate批量保存对象到数据库

使用SqlSessionTemplate的方式对实体信息批量保存

    @Value("#{rdsSqlSession.getMapper(T(*.*.*.*.*.*.*.*PoMapper))}")
    private *Mapper *PoMapper;

    @Resource(name = "rdsSqlSession")
    private SqlSessionTemplate sqlSessionTemplate;

    public void insert(List<*Info> infoList) {

        List<*InfoPo> poList = infoList.stream().map(*Convert::convert2Po).collect(Collectors.toList());
        SqlSession batchSession = null;
        try{
            batchSession = sqlSessionTemplate.getSqlSessionFactory().openSession(ExecutorType.BATCH);
            batchSession.insert("*.*.*.*.*.*.*.*PoMapper.batchInsert", poList);
            batchSession.commit();
            batchSession.clearCache();
        } catch (Exception e) {
            logger.error("*Dal insert error, infoList:{}, e", infoList, e);
            throw new *Exception(*ErrorCodeConst.*);
        } finally {
            if (batchSession != null) {
                batchSession.close();
            }
        }

    }

版权声明:本文为HerenXie原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。