我正在 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);
...
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/