java - RepositoryItemReader 找不到带参数的方法

标签 java spring spring-batch

我正在 springBatch 步骤中为读取器设置一个 ItemRepositoryReader。

public ItemReader<EcheanceEntity> reader(){
    RepositoryItemReader<EcheanceEntity> reader = new RepositoryItemReader<EcheanceEntity>();
    reader.setRepository(echeanceRepository);
    reader.setMethodName("findById");
    List parameters = new ArrayList();
    long a = 0;
    parameters.add(a);
    reader.setArguments(parameters);
    Map<String, Direction> sort = new HashMap<String, Direction>();
    sort.put("id", Direction.ASC);
    reader.setSort(sort);
    return reader;
}

这是我存储库中的行。

public interface EcheanceRepository extends JpaRepository<EcheanceEntity, Long>{


public EcheanceEntity findById(long id);

@Override
public List<EcheanceEntity> findAll();

如果使用 findAll() 方法,那么在没有任何参数的情况下,它可以正常工作。但是,如果我使用方法 findById(long id),我会从 ItemRepositoryReader 得到“没有这样的方法异常,findById(java.lang.Long, org.springframework.data.domain.PageRequest)”。当我通过立即使用存储库对其进行测试时,该方法在不使用阅读器的情况下工作正常。

谢谢。

最佳答案

如果使用 RepositoryItemReader#setMethodName 方法,您需要在存储库方法签名的最后位置添加类型为 Pageable 的参数:

    public interface EcheanceRepository extends JpaRepository<EcheanceEntity, Long> {
    
        public Page<EcheanceEntity> findById(long id, Pageable pageable);
        ...

您可以在文档中找到它的描述:http://docs.spring.io/spring-batch/apidocs/org/springframework/batch/item/data/RepositoryItemReader.html#setMethodName-java.lang.String-

public void setMethodName(java.lang.String methodName)

Specifies what method on the repository to call. This method must take Pageable as the last argument.

关于java - RepositoryItemReader 找不到带参数的方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35844667/

相关文章:

java - Apache Commons Daemon procrun 访问在 Windows 7 上被拒绝

Java Arrays、Collections、Sets 和 Maps 属于 xx 对象类别?

java - 在 tomcat 中优雅地关闭 spring 批处理作业

java - Spring Batch CommandLineJobRunner -next 使用旧的 run.id

Java IO 异常捕获

java - Spring Autowiring 注解构造函数参数

java - Hibernate 4 和 Spring 4 HibernateException : No Session found for current thread

xml - Spring 配置

java - 在 Spring Batch 步骤中引用命令行参数

java - 使用 switch 语句比使用 if 语句更快,还是将 case 放在 hashMap 中并只使用 contains 方法?