有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java RepositoryItemReader找不到带参数的方法

我正在为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();

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

谢谢


共 (1) 个答案

  1. # 1 楼答案

    在使用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.