简体   繁体   English

Spring启动JpaRepository接口

[英]Spring boot JpaRepository interface

New to Spring Boot here, learning from online resource, have question. Spring 新手在这里启动,从在线资源学习,有问题。 Can someone please explain me?有人可以解释一下吗? I am referring from link https://www.springboottutorial.com/creating-microservices-with-spring-boot-part-2-forex-microservice .我指的是链接https://www.springboottutorial.com/creating-microservices-with-spring-boot-part-2-forex-microservice

I am able to create everything as expected and see the results.我能够按预期创建所有内容并查看结果。 I understood Restcontroller, and have trouble understanding below lines.我了解 Restcontroller,但无法理解以下几行。

Can someone please explain me how shall I read / understand below code and how to know what's happening?有人可以解释一下我应该如何阅读/理解下面的代码以及如何知道发生了什么? Please note I am not getting any error.请注意,我没有收到任何错误。 I get the response at my local server as expected.我按预期在本地服务器上得到响应。 This method findByFromAndTo does not have any implementation in interface which I understand but it does not have any implementation in RestController as well.这个方法findByFromAndTo在我理解的接口中没有任何实现,但它在 RestController 中也没有任何实现。 So how does this work?那么这是如何工作的呢?


public interface ExchangeValueRepository extends
        JpaRepository<ExchangeValue, Long> {

    ExchangeValue findByFromAndTo(String from, String to);
}
ExchangeValue findByFromAndTo(String from, String to);  

In the above statement, ExchangeValue is the expected response.在上面的语句中, ExchangeValue是预期的响应。 There are two columns that we have to find are from and to.我们必须找到两列 from 和 to。

Usage: If we want to query the conversion value from one currency to another.用法:如果我们要查询从一种货币到另一种货币的转换价值。 Get the exchange value from the database.从数据库中获取交换价值。

If we want to find data on the basis of single column, we can pass a column name.如果我们想根据单列查找数据,我们可以传递一个列名。 For example:例如:

ExchangeValue findByFrom (String from);  

Internal Working:内部工作:

We will create a query using the JPA criteria API from this but essentially this translates into the following query:我们将使用 JPA 标准 API 创建一个查询,但本质上这会转化为以下查询:

select e from ExchangeValue e where e.from = ?1 and e.to = ?2

Spring Data JPA will do a property check and traverse nested properties as described in???. Spring 数据 JPA 将执行属性检查并遍历嵌套属性,如???中所述。

If And is the Keyword, findByLastnameAndFirstname is the sample, then JPQL snippet/query is … where x.lastname =?1 and x.firstname =?2如果And是关键字, findByLastnameAndFirstname是样本,那么 JPQL 片段/查询是…… where x.lastname =?1 and x.firstname =?2

More details from official documentation: https://docs.spring.io/spring-data/jpa/docs/1.5.0.RELEASE/reference/html/jpa.repositories.html来自官方文档的更多详细信息: https://docs.spring.io/spring-data/jpa/docs/1.5.0.RELEASE/reference/html/jpa.repositories.ZFC35FDC70D5FC69D23EZ8C

Hope this helps.!希望这可以帮助。!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM