java批处理

2023-06-23 14:04:55 浏览数 (2)

代码语言:javascript复制
	@Resource
    private SqlSessionFactory sqlSessionFactory;

    @Override
    public <T>  void saveBatch(List<?> saveList, Class<? extends BaseMapper<T>> mappers) {
        SqlSession sqlSession = sqlSessionFactory.openSession(ExecutorType.BATCH, false);
        BaseMapper<T> mapper = sqlSession.getMapper(mappers);
        for(int i = 0; i <= saveList.size() - 1; i  ){
            mapper.insert((T) saveList.get(i));
            if(i % 1000 == 0){
                sqlSession.commit();
                sqlSession.clearCache();
            }
        }
        sqlSession.commit();
        sqlSession.clearCache();
    }

0 人点赞