Spring Data JPA中saveAll方法报错Cannot resolve method,该如何解决?
解决方案
核心原因
PagingAndSortingRepository仅提供分页、排序相关的查询方法(如findAll(Pageable)、findAll(Sort)),不包含saveAll批量保存方法,这个方法是CrudRepository的核心方法之一。
可选解决方法
方法一:改用
JpaRepository继承JpaRepository同时继承了PagingAndSortingRepository和CrudRepository,既能保留分页排序能力,又能直接使用saveAll等CRUD方法,这是最简洁的方案:public interface RuleRepository extends JpaRepository<Entity, Long> { // 自定义方法保留不变 }方法二:手动声明
saveAll方法
如果不想修改继承的顶层接口,可以在RuleRepository中自行声明saveAll方法,Spring Data JPA会自动为你实现该方法:public interface RuleRepository extends PagingAndSortingRepository<Entity, Long> { <S extends Entity> Iterable<S> saveAll(Iterable<S> entities); // 自定义方法保留不变 }
内容的提问来源于stack exchange,提问作者Peter Penzov
相关产品推荐
相关产品推荐

