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

标签 java spring interface

第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/

相关文章:

java - 从无孔阵列中移除

java - Junit测试调用Dao类的Business类

java - 通过@Configuration启动Spring应用程序

java - hibernate中的两个表之间可以有@OneToMany和@ManyToOne关系吗?

java - Bukkit - 为什么转换为 Damageable 有效?

java - 如何在编程语言中生成一些长度固定为 r 的同分布伪随机 vector ?

java - 关于 Dice Array 程序

spring - spring如何跨多个类使用redis事务?

c# - 在 C# 中使用接口(interface)的问题

c# - 在 C# 中返回给定接口(interface)的泛型类型