<?xml version="1.0" encoding="UTF-8"?>
<!--

    Copyright ${license.git.copyrightYears} the original author or authors.

    Licensed under the Apache License, Version 2.0 (the "License");
    you may not use this file except in compliance with the License.
    You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

    Unless required by applicable law or agreed to in writing, software
    distributed under the License is distributed on an "AS IS" BASIS,
    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    See the License for the specific language governing permissions and
    limitations under the License.

-->
<!--
    A sample context that shows how to integrate MyBatis with Spring Batch.
-->
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:jdbc="http://www.springframework.org/schema/jdbc" xmlns:batch="http://www.springframework.org/schema/batch"
       xsi:schemaLocation="
         http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
         http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc.xsd
         http://www.springframework.org/schema/batch http://www.springframework.org/schema/batch/spring-batch.xsd
       ">

  <!-- in-memory database and a datasource -->
  <jdbc:embedded-database id="dataSource">
    <jdbc:script location="classpath:org/mybatis/spring/sample/db/database-schema.sql"/>
    <jdbc:script location="classpath:org/springframework/batch/core/schema-drop-hsqldb.sql"/>
    <jdbc:script location="classpath:org/springframework/batch/core/schema-hsqldb.sql"/>
    <jdbc:script location="classpath:org/mybatis/spring/sample/db/database-test-data.sql"/>
  </jdbc:embedded-database>

  <!-- transaction manager, use JtaTransactionManager for global tx -->
  <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
    <constructor-arg ref="dataSource"/>
  </bean>

  <!-- SqlSessionFactory configuration -->
  <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
    <property name="dataSource" ref="dataSource"/>
    <property name="mapperLocations" value="classpath:org/mybatis/spring/sample/mapper/*.xml"/>
    <property name="configuration">
      <bean class="org.apache.ibatis.session.Configuration">
        <property name="defaultExecutorType" value="BATCH"/>
      </bean>
    </property>
  </bean>

  <batch:job-repository/>

  <bean id="jobLauncher" class="org.springframework.batch.core.launch.support.SimpleJobLauncher">
    <property name="jobRepository" ref="jobRepository"/>
  </bean>

  <bean id="reader" class="org.mybatis.spring.batch.MyBatisCursorItemReader">
    <property name="sqlSessionFactory" ref="sqlSessionFactory"/>
    <property name="queryId" value="org.mybatis.spring.sample.mapper.UserMapper.getUsers"/>
  </bean>

  <bean id="processor" class="org.mybatis.spring.sample.batch.UserToPersonItemProcessor"/>

  <bean id="writer" class="org.mybatis.spring.batch.MyBatisBatchItemWriter">
    <property name="sqlSessionFactory" ref="sqlSessionFactory"/>
    <property name="statementId" value="org.mybatis.spring.sample.mapper.PersonMapper.createPerson"/>
    <property name="itemToParameterConverter">
      <bean class="org.mybatis.spring.sample.config.SampleJobConfig" factory-method="createItemToParameterMapConverter">
        <constructor-arg type="java.lang.String" value="batch_xml_config_user"/>
        <constructor-arg type="java.time.LocalDateTime" value="#{T(java.time.LocalDateTime).now()}"/>
      </bean>
    </property>
  </bean>

  <batch:job id="importUserJob">
    <batch:step id="step1">
      <batch:tasklet transaction-manager="transactionManager">
        <batch:chunk reader="reader" writer="writer" processor="processor" commit-interval="10"/>
      </batch:tasklet>
    </batch:step>
  </batch:job>

</beans>
