You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Spring Batch 5.1.2调用自定义Repository方法失败求助

问题根源

Spring Batch的RepositoryItemReader是分页读取数据的组件,它在调用Repository方法时,会自动传入PageRequest类型的参数(包含分页大小、排序规则等信息)。但你定义的findByActiveIndIsTrue()方法没有接收这个参数,导致JDK动态代理找不到匹配的方法,抛出NoSuchMethodException。

解决方案

有两种可行的修复方式:

方式一:修改Repository方法,支持分页参数

直接调整DistrictRepository中的方法,让它接收Pageable参数并返回Page<DistrictT>类型,这样就能和RepositoryItemReader的调用逻辑匹配:

@Repository
public interface DistrictRepository extends PagingAndSortingRepository<DistrictT, Long> {
      // 新增Pageable参数,返回Page类型结果
      Page<DistrictT> findByActiveIndIsTrue(Pageable pageable);
}

你的RepositoryItemReader配置不需要做任何修改,它会自动将分页和排序信息封装成PageRequest传入这个方法。

方式二:自定义读取逻辑(不修改Repository)

如果不想改动Repository,可以改用ListItemReader实现,先一次性查询所有激活的District,再交由它处理分页逻辑:

@Bean
public ItemReader<DistrictT> districtItemReader(DistrictRepository repository) {
    // 先查询所有激活的数据
    List<DistrictT> activeDistricts = repository.findByActiveIndIsTrue();
    // 用ListItemReader处理分页逻辑
    return new ListItemReader<>(activeDistricts);
}

注意:这种方式适合数据量不大的场景,因为会一次性加载所有数据到内存中。

内容的提问来源于stack exchange,提问作者developer

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.18 02:23:16