MyBatisPagingItemReader를 사용할 때 유의점

2015. 10. 2. 09:59OpenSource/Spring Batch

반응형


Caused by: java.sql.BatchUpdateException: Duplicate entry '123212' for key 'PRIMARY'
	at com.mysql.jdbc.PreparedStatement.executeBatchSerially(PreparedStatement.java:2055) ~[neositebatch.jar:na]
	at com.mysql.jdbc.PreparedStatement.executeBatch(PreparedStatement.java:1467) ~[neositebatch.jar:na]


위의 오류가 났다.



해결방안으로 아래처럼 MyBatisPagingItemReader를 사용할 때 유의하여 사용하면 된다.

<batch:reader>
<bean class="org.mybatis.spring.batch.MyBatisPagingItemReader">
<property name="sqlSessionFactory" ref="sqlSessionFactoryForMysql"/>
<property name="pageSize" value="100"/>
<property name="queryId"
value="xxx.xxx.xxx.xxxRepository.get블라블라"/>
</bean>
</batch:reader>


SELECT Query에서 MySQL이라면 LIMIT #{_skiprows}, #{_pagesize} 를 걸어줘야 한다.

읽어와서 페이징 단위로 자동으로 데이터를 가져오지 못한다는 점이다.

  • _page: 읽을 페이지 수(0부터 시작)
  • _pagesize: 페이지의 크기, 이를테면 리턴하는 로우 수
  • _skiprows_page 와 _pagesize의 결과

참고 : https://mybatis.github.io/spring/ko/batch.html


  끝~


반응형