数据库批量插入操作出现的sql过长问题的解决

@Autowired
private SqlSessionFactoryBean sqlSessionFactoryBean;

@Override
public void addBatch(List users) throws Exception {
	try (
			SqlSession sqlSession = sqlSessionFactoryBean.getObject().openSession(ExecutorType.BATCH)) {
			try {
				UserMapper mapper = sqlSession.getMapper(UserMapper.class);
				for (User user : users) {
					mapper.addUser(user);
				}
				sqlSession.commit();
			} catch (Exception e) {
				sqlSession.rollback();
				log.error("批量插入User失败", e);
				throw e;
			} finally {
				sqlSession.clearCache();
				sqlSession.close();
			}
		} catch (Exception e) {
			log.error("获取sqlSession异常", e);
			throw e;
		}
}

对应mapper.xml:

  
    		insert into fyujia.T_User (C_ID, N_VALID)
    		values (#{id, jdbcType=CHAR},#{valid, jdbcType=INTEGER})
   

你可能感兴趣的:(Java)