Springboot模式下Mybatis的batch模式怎么写
public int addUserSpace(List<UserSpace> userSpaceList) { SqlSessionFactory sqlSessionFactory = sqlSessionTemplate.getSqlSessionFactory(); SqlSession sqlSession = sqlSessionFactory.openSession(ExecutorType.BATCH,false ); UserSpaceMapper mapper = sqlSession.getMapper(UserSpaceMapper.class); for (UserSpace userSpace : userSpaceList) { UserSpaceEntity userSpaceEntity = userSpaceConverter.to(userSpace); mapper.insert(userSpaceEntity); } //进行批次提交 sqlSession.commit(); //清除缓存 sqlSession.clearCache(); //关闭连接 sqlSession.close(); return 0; }
还有个注意点
spring: datasource: url: jdbc:mysql://17x.2x.13x.x:33306/knowledge_base?serverTimezone=UTC&characterEncoding=utf8&characterSetResults=utf8&autoReconnect=true&rewriteBatchedStatements=true username: root password: root123