我正在研究 Spring Data JPA。考虑下面的示例,其中我将使所有 CRUD 和查找器功能默认工作,如果我想自定义查找器,那么也可以在界面本身中轻松完成。
@Transactional(readOnly = true)
public interface AccountRepository extends JpaRepository<Account, Long> {
@Query("<JPQ statement here>")
List<Account> findByCustomer(Customer customer);
}
我想知道如何为上述 AccountRepository 添加完整的自定义方法及其实现?由于它是一个接口,我无法在那里实现该方法。
您需要为自定义方法创建一个单独的接口:
public interface AccountRepository
extends JpaRepository<Account, Long>, AccountRepositoryCustom { ... }
public interface AccountRepositoryCustom {
public void customMethod();
}
并为该接口提供一个实现类:
public class AccountRepositoryImpl implements AccountRepositoryCustom {
@Autowired
@Lazy
AccountRepository accountRepository; /* Optional - if you need it */
public void customMethod() { ... }
}
也可以看看:
4.6 Spring数据存储库的自定义实现 https://docs.spring.io/spring-data/jpa/docs/current/reference/html/#repositories.custom-implementations
请注意,不同版本之间的命名方案已发生变化。看https://stackoverflow.com/a/52624752/66686 https://stackoverflow.com/a/52624752/66686了解详情。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)