Announcement Announcement Module
No announcement yet.
No Map-based equivalent of ListPreparedStatementSetter Page Title Module
Move Remove Collapse
Conversation Detail Module
  • Filter
  • Time
  • Show
Clear All
new posts

  • No Map-based equivalent of ListPreparedStatementSetter

    I have done a great deal of CTFI on this matter, so I hope I'm not asking a completely stupid question.

    I need a Map-based implementation of PreparedStatementSetter. Since ListPreparedStatementSetter is in the Spring Batch project, I think this forum is the most appropriate place to pose a question. In my experience with the Spring Framework, if I need it - it probably already exists. So that's why I am curious...

    Here would be a usage scenario - feel free to correct this scenario as well.

    	<bean id="myReader" class="org.springframework.batch.item.database.JdbcCursorItemReader" scope="step">
    		<property name="dataSource" ref="myDataSource"/>
    		<property name="rowMapper"><bean class="org.springframework.jdbc.core.ColumnMapRowMapper"/></property>
    		<property name="sql">
    			<bean class="" factory-method="toString">
    				<constructor-arg type="" value="classpath:/my.sql"/>
    		<property name="preparedStatementSetter">
    			<bean class="my.MapPreparedStatementSetter">
    				<property name="parameters">
    						<entry key="begin" value="#{jobParameters['']}"/>
    						<entry key="end" value="#{jobParameters['']}"/>
    I went ahead and implemented a solution, based on the source for ListPreparedStatementSetter - keep in mind this is my first go at it... Be nice!

    public class MapPreparedStatementSetter implements PreparedStatementSetter, InitializingBean {
        private Map<String, ?> parameters;
        public void setValues(PreparedStatement ps) throws SQLException {
    		int i=0;
    		for (Map.Entry<String, ?> entry : parameters.entrySet()) {
    					new SqlParameter(entry.getKey(), StatementCreatorUtils.javaTypeToSqlParameterType(entry.getValue().getClass())),
        public void setParameters(Map<String, ?> parameters) {
            this.parameters = parameters;
        public void afterPropertiesSet() throws Exception {
            Assert.notNull(parameters, "Parameters must be provided");
    Lot's of mutable state there - that implementation is NOT thread safe. That will be in version 2.

    Is there a different or better way to do this? I see the usage for ListPreparedStatementSetter, but the SQL I have to execute is far too complex to use '?' to bind ordered parameters - it truly needs named parameters.


  • #2
    afaik there is no NamedParameter Support for JdbcCursorItemReader in Spring Batch yet, but you can wrap a NamedParameterJdbcTemplate dao implementation as itemReader