如何向Spring Data JPA添加自定义方法
我正在研究Spring Data JPA。考虑下面的示例,默认情况下我将使所有crud和finder功能正常工作,如果我想自定义finder,那么也可以在界面本身中轻松完成。
@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() { ... }
}
以上是 如何向Spring Data JPA添加自定义方法 的全部内容, 来源链接: utcz.com/qa/428190.html