Using Spring Batch JdbcCursorItemReader with NamedParameters

前端 未结 4 707
滥情空心
滥情空心 2021-01-05 13:34

The Spring Batch JdbcCursorItemReader can accept a preparedStatementSetter:



        
4条回答
  •  别那么骄傲
    2021-01-05 13:48

    original solution in https://jira.spring.io/browse/BATCH-2521, but which does not support id in (:ids) clause.

    here is an enhancement.

    import lombok.Setter;
    import lombok.extern.slf4j.Slf4j;
    import lombok.val;
    
    import org.springframework.batch.item.database.JdbcCursorItemReader;
    import org.springframework.jdbc.core.PreparedStatementCreatorFactory;
    import org.springframework.jdbc.core.namedparam.MapSqlParameterSource;
    import org.springframework.jdbc.core.namedparam.NamedParameterUtils;
    
    import java.util.Map;
    
    @Slf4j
    public class NamedParameterJdbcCursorItemReader extends JdbcCursorItemReader {
    
        protected void setNamedParametersSql(String sql, Map parameters) {
            val parsedSql = NamedParameterUtils.parseSqlStatement(sql);
            val paramSource = new MapSqlParameterSource(parameters);
    
            val sqlToUse = NamedParameterUtils.substituteNamedParameters(parsedSql, paramSource);
            val declaredParams = NamedParameterUtils.buildSqlParameterList(parsedSql, paramSource);
            val params = NamedParameterUtils.buildValueArray(parsedSql, paramSource, null);
            val pscf = new PreparedStatementCreatorFactory(sql, declaredParams);
            val pss = pscf.newPreparedStatementSetter(params);
    
            log.info("sql: {}", sqlToUse);
            log.info("parameters: {}", parameters);
    
            setSql(sqlToUse);
            setPreparedStatementSetter(pss);
        }
    }
    

    Usage:

    @Slf4j
    public class UserItemJdbcReader extends NamedParameterJdbcCursorItemReader {
    
        @PostConstruct
        public void init() {
            val sql = "SELECT * FROM users WHERE id IN (:ids)";
    
            val parameters = new HashMap(4);
            parameters.put("ids", Arrays.asList(1,2,3));
    
            setDataSource(dataSource);
            setRowMapper(new UserRowMapper());
            setNamedParametersSql(sql, parameters);
        }
    }
    

提交回复
热议问题