我正在研究Spring数据JPA.考虑下面的例子,我将得到所有的CRUD和FIDER功能默认工作,如果我想定制一个取景器,那么也可以很容易在界面本身.

@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() { ... }
}

See also:

Java相关问答推荐

如何用Java表示C++类以通过FFI使用?

Spring Jpa findById会导致StackOverFlow错误,但其他查询没有问题

如何跟踪我在数组中的位置

Spring Boot找不到Mapper bean

Javascript更新alert 可扩展内容样式与CSS—按钮更多/更少

无法传递消费者<;>;实例

流迭代列表<;对象>;上的NoSuchElementException

JVM会优化这个数学运算吗?

Java记录的不同序列化/反序列化

SpringBoot+Java 17@Valid未验证POJO

测试容器无法加载类路径初始化脚本

在Oracle JDBC连接中,连接失效和身份验证失效是什么意思?

为什么在下面的Java泛型方法中没有类型限制?

除0错误/抱歉我的句子是PT

如果执行@BeForeEach#repository.save(),则测试中的UnitTest最终UUID会发生更改

模拟JUnit未检测到返回字符串的方法的任何声纳覆盖

通过/失败的参数化junit测试方法执行数

Java编译器是否进行了持续的折叠优化,以及如何进行判断?

java21预览未命名的符号用于try-with-resources

为什么我得到默认方法的值而不是被覆盖的方法的值?