当前位置: 首页 > 知识库问答 >
问题:

将“$Proxy0”类型的属性值转换为所需类型失败意味着什么?

华化
2023-03-14

我在Spring批处理代码中遇到了一个错误:创建名为“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());

    } 

}

谁能帮帮我吗?

共有1个答案

巴学潞
2023-03-14

你没有使用正确的界面。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。。。

所以它不起作用。如果你没有把范围=步骤,你可能会有一个ClassCastException类型的错误!

 类似资料: