gpt4 book ai didi

java - Spring in Action 第 4 版中 JPA 存储库示例的问题

转载 作者:行者123 更新时间:2023-12-01 10:50:43 25 4
gpt4 key购买 nike

第325页左右:

SpitterRepository extends JpaRepository.
SpitterRepositoryImpl implements SpitterRepository. Also SpitterRepositoryImpl extends SpitterSweeper interface.
public interface SpitterSweeper{
int eliteSweep();
}

它表示应该在 SpitterRepository 接口(interface)中声明 eliteSweep 方法。所以不太明白,为什么我们不能只在eliteSweep接口(interface)中声明呢?

最佳答案

来自他们的doc :

The repository proxy has two ways to derive a store-specific query from the method name. It can derive the query from the method name directly, or by using an additionally created query. Available options depend on the actual store. However, there's got to be an strategy that decides what actual query is created. Let's have a look at the available options.

The query builder mechanism built into Spring Data repository infrastructure is useful for building constraining queries over entities of the repository. The mechanism strips the prefixes find…By, read…By, and get…By from the method and starts parsing the rest of it. The introducing clause can contain further expressions such as a Distinct to set a distinct flag on the query to be created. However, the first By acts as delimiter to indicate the start of the actual criteria. At a very basic level you can define conditions on entity properties and concatenate them with And and Or .

这意味着,Spring Data JPA 将从方法名称构造查询,对于简单查询,您不需要编写 jpql/hql 查询,Spring Data JPA 将自动创建它们。 Spring data jpa 将解析方法名称并构建所需的查询。

至于为什么要在接口(interface)中声明?框架将尝试创建一个存储库代理实例,该接口(interface)的具体实现将在您注入(inject)该接口(interface)的地方使用。

关于java - Spring in Action 第 4 版中 JPA 存储库示例的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33932906/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com