问题
I have a functioning program that reads from a DB and inputs it into a flat file. I'm using Spring batch for these I want be able to choose the parameters for my query. How can I do that.
My xml look something like this:
<bean id="databaseitemreader" class="JdbcursorItemReader">
<property name = "datasource" <ref = ...>
<property name = sql value= "Select fname , lname , address from tbl_student"/>
Item file writer stuff .....
(This one does not need any change)
I want to be be able to pass dynamic parameters to my query that is stored in the xml file. example: where id = 1234 and current = 'Y' without being hard coded
To map the values I am currently using the rowMapper interface
回答1:
This section in the Spring Batch Document http://docs.spring.io/spring-batch/reference/html/configureStep.html#late-binding explains how parameter can be passed to the spring batch job.
As to how you extract those parameters from the XML - that would something you would do outside the job - depending on how you are starting up the Job.
回答2:
You can try to write a custom reader composed by one XML and one JDBC readers.
For every node read from XML prepare an inner JDBC reader with correct sql query and delegate ItemReader.read() to JDBCItemReader.read() until exhausts; move to next XML node and continue until XML reader is terminated.
Remember to register streams (or implements ItemStream in custom reader and manage ItemStream' methods according).
来源:https://stackoverflow.com/questions/28845219/use-a-query-that-takes-parameters-in-spring-batch