[英]Spring Data JPA/Boot: findBy ... or
我想在我的存储库中编写一个 finder 方法来查找基于一个字段或另一个字段的对象,同时提供一个参数,例如:
@RepositoryDefinition(domainClass = Person.class, idClass = Long.class)
public interface PersonRepository extends CrudRepository<Person, Long> {
List<Person> findAllByIdOrAnotherId(someId);
}
如何在不使用 SQL 的情况下做到这一点?
我向该方法添加了第二个参数并且它起作用了。
List<Transaction> findAllByIdOrParentId(Long id, Long parentId);
这只是该方法的定义,因为我将相同的参数从服务传递给该方法,如下所示:
List<Transaction> transactions = transactionRepository.findAllByIdOrParentId(transactionId, transactionId);
Spring Data JPA 的一项很酷的事情是您可以将自定义查询定义为接口中的抽象方法。
简而言之,您可以通过指定要查询的@Entity
字段名称来定义新的搜索条件。 所以,假设我想通过Id
和CustomId
findAll()
。 Id
和CustomId
都是我的域类中的字段。 我会做:
List<Person> findAllByIdOrCustomId(someId, someCustomId);
有关更多信息,请研究以下链接: Spring Data JPA - DEFINE QUERY METHODS
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.