1

我在我的 Spring Batch 代码中遇到一个错误,它说:在类路径资源中定义名称为“itemReader”的 bean 创建错误,无法将类型“$Proxy0”的属性值转换为属性“preparedStatementSetter”所需的类型。

这是我的 ItemReader 代码:

<beans:bean id="itemReader"
 class="org.springframework.batch.item.database.JdbcCursorItemReader">
    <beans:property name="dataSource" ref="dataSource"/>
    <beans:property name="sql" value="select * from Trans_Tst WHERE id =?"/>
    <beans:property name="rowMapper" ref="romMapper"/>
    <beans:property name="preparedStatementSetter" ref="ReaderSqlParameterSetter"/>
</beans:bean>



<beans:bean id="ReaderSqlParameterSetter"
 class="sa.com.anb.itg.dev.settlement.batch.ReaderSqlParameterSetter" scope="step">
<beans:property name="id" value="#{jobParameters[id]}"/>
</beans:bean>

这是我的 ReaderSqlParameterSetter 类:

package sa.com.anb.itg.dev.settlement.batch;
import java.sql.PreparedStatement;
import org.springframework.batch.item.database.ItemPreparedStatementSetter;
import java.sql.SQLException;

public class ReaderSqlParameterSetter implements
 ItemPreparedStatementSetter<transactionas>{

    public void setValues(transactionas transactionas,PreparedStatement ps) 
          throws SQLException {
        ps.setInt(1, transactionas.GetID());

    } 

}

<beans:bean id="jdbcWriter" 
  class="org.springframework.batch.item.database.JdbcBatchItemWriter">
    <beans:property name="dataSource" ref="dataSource"/> 
    <beans:property name="sql" value="update Trans_Tst set ()amount) values (?) 
           where id= ?"/>
    <beans:property name="preparedStatementSetter" 
          ref="WriterSqlParameterSetter"/> 
</beans:bean>

<beans:bean id="WriterSqlParameterSetter" 
  class="sa.com.anb.itg.dev.settlement.batch.WriterSqlParameterSetter" scope="step">
    <beans:property name="amount" value="#{jobParameters[amount]}"/>
    <beans:property name="id" value="#{jobParameters[id]}"/>  
</beans:bean>



    public class WriterSqlParameterSetter implements PreparedStatementSetter {

    transactionas transactionas = new transactionas();

    public void setValues(PreparedStatement ps) throws SQLException {
        ps.setDouble(1, transactionas.GetAmount());
        ps.setInt(2, transactionas.GetID());

    } 

}

任何人都可以帮助我吗?

4

1 回答 1

2

您没有使用正确的界面。您的 ReaderSqlParameterSetter 应该实现PreparedStatementSetter而不是ItemPreparedStatementSetter

如果你看一下 JdbcCursorItemReader 的 impl:

public class JdbcCursorItemReader<T> extends AbstractCursorItemReader<T> {

PreparedStatement preparedStatement;

PreparedStatementSetter preparedStatementSetter;

String sql;

RowMapper rowMapper;

该属性是 PreparedStatementSetter 。

如果您查看您尝试使用的界面:

public interface ItemPreparedStatementSetter<T> {
/**
 * Set parameter values on the given PreparedStatement as determined from
 * the provided item.
 * @param ps the PreparedStatement to invoke setter methods on
 * @throws SQLException if a SQLException is encountered (i.e. there is no
 * need to catch SQLException)
 */
void setValues(T item, PreparedStatement ps) throws SQLException;

}

此接口不扩展 PreparedStatementSetter ...

所以它不起作用。如果您没有设置 scope=step,您可能会遇到 ClassCastException 类型的错误!

于 2013-06-04T14:15:44.823 回答