Announcement Announcement Module
Collapse
No announcement yet.
How to pass dynamically params to sql when using JdbcCursorItemReader Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • How to pass dynamically params to sql when using JdbcCursorItemReader

    Hi,

    I have read about ListPreparedStatementSetter, listeners, JobContext, StepContext, etc. but, I need to set the sql parameters dynamically and I do not want to pass as a job parameters. Could someone give me a good example, please? I mean, let me ask you:
    1) What about extending ListPreparedStatementSetter to override the constructor and initialize the parameters? There is no documentation about this class? I was not able to find it.
    2) If I extend JdbcCursomItermReader, how can I set the sql attribute?
    3) Trying to use #{stepExecutionContext[dateMax]} on the query does not work. I am declaring a listener and so on.

    Well, I would really appreciatte your help. Thanks in advance.

    Z.-

  • #2
    Use your own implementation of PreparedStatementSetter to set a constant value, or ItemPreparedStatementSetter to set fields from a pojo.

    Then you just feed it to your reader like so on your reader:

    Code:
            <property name="preparedStatementSetter">
                <bean class="com.my.company.MyStatementSetter"
                      autowire="byName">
           </property>

    Comment


    • #3
      Ei,
      thanks. Finally I did my own preparedStatementSetter:
      public class CustomerReaderPreparedStatementSetter implements
      PreparedStatementSetter, InitializingBean {

      private List<Object> parameters;

      /**
      * Default constructor.
      * parameters attribut is inicialized with required query parameters
      */
      public CustomerReaderPreparedStatementSetter(){
      parameters = new ArrayList<Object>();
      parameters.add(new Integer(1));
      parameters.add(new Integer(2));
      parameters.add(new Integer(3));
      }

      /**
      * This function fills the query with parameters attribute values.
      */
      public void setValues(PreparedStatement ps) throws SQLException {
      for (int i = 0; i < parameters.size(); i++) {
      StatementCreatorUtils.setParameterValue(ps, i + 1,
      SqlTypeValue.TYPE_UNKNOWN, parameters.get(i));
      }
      }

      /**
      * The parameter values that will be set on the PreparedStatement. It is
      * assumed that their order in the List is the order of the parameters in
      * the PreparedStatement.
      */
      public void setParameters(List<Object> parameters) {
      this.parameters = parameters;
      }

      /**
      * Assert that mandatory properties are set
      */
      public void afterPropertiesSet() throws Exception {
      Assert.notNull(parameters, "Parameters must be provided");
      }

      And I configured by reader:

      <bean id="customerReader"
      class="org.springframework.batch.item.database.Jdb cCursorItemReader">
      <property name="fetchSize" value="3000"/>
      <property name="useSharedExtendedConnection" value="true"/>
      <property name="dataSource" ref="extendedConnectionDataSourceProxy" />
      <property name="sql">
      <value>${query.customers.select}</value>
      </property>
      <property name="preparedStatementSetter">
      <bean
      class="my.project.CustomerReaderPreparedStatementS etter"/>
      </property>
      <property name="rowMapper">
      <bean
      class="my.project.data.access.daos.jdbcSpring.impl .CustomerRowMapper" />
      </property>
      </bean>

      Comment


      • #4
        I think you just rewrote ListPreparedStatementSetter


        http://static.springsource.org/sprin...entSetter.html

        Comment

        Working...
        X