当前位置: 首页 > 知识库问答 >
问题:

如何在Spring Data JPA中添加自定义方法

徐杰
2023-03-14
@Transactional(readOnly = true)
public interface AccountRepository extends JpaRepository<Account, Long> {

  @Query("<JPQ statement here>")
  List<Account> findByCustomer(Customer customer);
}

共有1个答案

邢鸿博
2023-03-14

您需要为自定义方法创建一个单独的接口:

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

另见:

 类似资料:
  • 是否有一个时间格式来指定这样的日期序列到轴在D3和v4?

  • 我想在向url发送请求时添加假用户代理。但它并没有添加假useragent,而是使用默认的useragent。

  • 我必须将自定义方法方法添加到我的日志系统中。假设我有几行代码,例如: 日志记录由slf4j处理。如何通过添加新(!)扩展我的记录器方法,如public void error(SomeTypeObject obj){/implementation/} 目标是不更改现有代码。如何强制LoggerFactory返回使用上述方法扩展的自己的记录器实现? 我跟踪了这个问题的答案:stackoverflow.

  • 问题内容: 我正在研究Spring Data JPA。考虑下面的示例,默认情况下我将使所有crud和finder功能正常工作,如果我想自定义finder,那么也可以在界面本身中轻松完成。 我想知道如何为上述AccountRepository的实现添加完整的自定义方法?由于它是一个接口,所以我不能在那里实现该方法。 问题答案: 你需要为自定义方法创建一个单独的接口: 并提供该接口的实现类:

  • Thymeleaf有许多有用的实用程序,如或。我试图添加一个自定义的,但不知道如何注册这一点。

  • 问题内容: 我正在使用Twitter Bootstrap添加图标,没有问题。他们有很多选择。 http://twitter.github.com/bootstrap/base- css.html#icons 但是,我找不到菜单项之一的适当图标。关于“汽车”。我想要的是我想添加我的自定义图标。我怎样才能做到这一点? 问题答案: 您可以通过在自己的类(如)中定义图标来创建自己的图标: 当然要记住使用前