JdbcBatchItemWriterでMapを使用できるようにする部品

JdbcBatchItemWriterはSpringBatchでSpringJdbcを使用できるようにするための部品です。(SpringBatchがデフォルトで用意しているクラスです)

これを使用すると、バッチ処理の記述からSQLの記述まですべてを、Spring設定ファイルのみで記述させることができます。

SQLの実行のみを行うバッチ処理の場合、ファイルが分散しない分、簡単になることがあると思います。

しかし、SQLのパラメタとしてMapを使用することができず、かならずPOJOを使用しなければなりません。

この仕様自体は間違ってはいないとは思いますが、割と面倒です。

soracaneではMapを使用できるようにする部品を用意しています。

MapSqlParameterSourceProviderクラスがその部品です。

以下では、具体的に利用方法を見ていきます。

目標

まず、以下のサンプルの目標(ゴール)を示します。

Spring設定ファイル上でデータMap配列を設定し、ItemReaderで読み込んでいくようにします。

そして、JdbcBatchItemWriterでデータMapを受け取り、次々にSQLを実行していきます。

このとき、SQL文にはパラメタとして、:user_id, :user_pwを使用することにします。

このパラメタ名はデータMapのキー名になっていますので、MapによってSQL文が置換されたことが分かるかと思います。

【実行予定のSQL文】

update SEC_OPE_ACCOUNT

set test_remarks = :user_id

where

user_id=:user_id

and user_pw=:user_pw

使用サンプル

<バッチ処理設定サンプル: batch-context.xml>

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

<beans xmlns="http://www.springframework.org/schema/beans"

xmlns:p="http://www.springframework.org/schema/p"

xmlns:aop ="http://www.springframework.org/schema/aop"

xmlns:util="http://www.springframework.org/schema/util"

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

xsi:schemaLocation="http://www.springframework.org/schema/batch

http://www.springframework.org/schema/batch/spring-batch-2.1.xsd

http://www.springframework.org/schema/aop

http://www.springframework.org/schema/aop/spring-aop-2.5.xsd

http://www.springframework.org/schema/util

http://www.springframework.org/schema/util/spring-util-2.5.xsd

http://www.springframework.org/schema/beans

http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">

<import resource="classpath:/test/batch/item/back-context.xml"/>

<import resource="classpath:/test/hsqldb-context.xml"/>

<!-- ジョブの処理1 -->

<job id="job1" xmlns="http://www.springframework.org/schema/batch" incrementer="jobParametersIncrementer">

<step id="step1" parent="simpleStep" >

<tasklet>

<chunk reader="listItemReader" writer="jdbcItemWriter" />

</tasklet>

</step>

</job>

<!-- enables the functionality of JobOperator.startNextInstance(jobName) -->

<bean id="jobParametersIncrementer" class="org.springframework.batch.core.launch.support.RunIdIncrementer" />

<bean id="simpleStep"

class="org.springframework.batch.core.step.item.SimpleStepFactoryBean"

abstract="true">

<property name="jobRepository" ref="jobRepository" />

<property name="commitInterval" value="10" />

</bean>

<!-- テストデータ -->

<bean id="listItemReader" class="org.springframework.batch.item.support.ListItemReader">

<constructor-arg index="0" >

<!-- テストデータ1 -->

<list>

<map>

<entry key="user_id" value="taro" />

<entry key="user_pw" value="taro" />

</map>

<map>

<entry key="user_id" value="hana" />

<entry key="user_pw" value="hana" />

</map>

</list>

</constructor-arg>

</bean>

<!-- テスト対象1 -->

<bean id="jdbcItemWriter" class="org.springframework.batch.item.database.JdbcBatchItemWriter">

<property name="dataSource" ref="dataSource"/>

<property name="itemSqlParameterSourceProvider">

<bean class="jp.co.soracane.batch.item.MapSqlParameterSourceProvider" />

</property>

<property name="sql" value="

update SEC_OPE_ACCOUNT

set test_remarks = :user_id

where

user_id=:user_id

and user_pw=:user_pw

" />

</bean>

</beans>

最初のimportは、SpringBatchの基本的なクラスの設定と、DB接続設定(dataSource)を記述したファイルを読み込んでいます。

<import resource="classpath:/test/batch/item/back-context.xml"/>

<import resource="classpath:/test/hsqldb-context.xml"/>

SpringBatchの一般的な設定ですので、各自で設定ファイルをご用意いただければと思います。

Spring概要の記事などをご参照いただくか、Webで検索すると分かるかと思います。

すみませんがここでは説明と設定の記述を省略します。

さて、しかし、あまり説明することが無いかなと思います。

上記で赤字のMapSqlParameterSourceProviderが今回紹介するクラスですが、特にプロパティもありません。

このクラスは単純に内部でMapをSQLのパラメタ(MapSqlParameterSource)に変換しているだけです。

データは、テストデータの記述をご覧になっていただければ分かりますようにMapで記述しています。

ですので、ItemWriterへはMapが渡ることになります。

当然ながらuser_idなどのパラメタ名がMapに設定されていないとエラーになりますのでお気をつけください。

SpringJdbcではキー名は大文字小文字も区別しますのでそこもお気をつけください。

最後に

1つの設定ファイル内で全てが完結するのは魅力があることだと思います。

もしSpringJdbcをO/Rマッピングとして採用するのであれば、ここで紹介した部品も使用してみても良いのではないでしょうか。

IBatisを使用する場合はMapも使用できますので、SpringJdbcと同様の心配事項はありません。

複雑なSQLを実行するのであれば、IBatisがかなり強力な選択肢の1つになるかと思います。

追記:

最近知ったのですが、IBatisは終了したようですね。(この記事を書いたときには知りませんでした。)

代わりにmybatisという新しいプロジェクトで立ち上がっているようです。

しかしSpringBatchではIBatisとの連携機能は用意されていますが、mybatisはまだのようです。

IBatisもバグも枯れているでしょうし使用するのは悪くはないかと思っています。

mybatisはまだ調査していませんが、ホームページの雰囲気を見るとSpringとの連携も意識しているようですのでもしかしたら

そのままSpringBatchでも使用できるかもしれないですね。

Created Date: 2010/01/11