Spring Boot的JpaRepository接口

huangapple go评论93阅读模式
英文:

Spring boot JpaRepository interface

问题

我是你的中文翻译,以下是已翻译的内容:

新手学习Spring Boot,从在线资源学习,有问题。能否有人请解释一下?我参考了链接https://www.springboottutorial.com/creating-microservices-with-spring-boot-part-2-forex-microservice

我能够按预期创建一切并看到结果。我理解了RestController,但在理解以下代码时遇到了困难。

有人可以请解释一下,我应该如何阅读/理解下面的代码,以及如何知道发生了什么?请注意,我没有收到任何错误。我在本地服务器上按预期获得响应。这个方法findByFromAndTo在我理解的接口中没有任何实现,但在RestController中也没有任何实现。那么它是如何工作的?


public interface ExchangeValueRepository extends
        JpaRepository<ExchangeValue, Long> {

    ExchangeValue findByFromAndTo(String from, String to);
}
英文:

New to Spring Boot here, learning from online resource, have question. Can someone please explain me? I am referring from link 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.

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. So how does this work?


public interface ExchangeValueRepository extends
        JpaRepository<ExchangeValue, Long> {

    ExchangeValue findByFromAndTo(String from, String to);
}

答案1

得分: 1

ExchangeValue findByFromAndTo(String from, String to);

在上述语句中,ExchangeValue 是预期的响应。我们需要查找的两列是 from 和 to。

用途:如果我们想要查询从一种货币到另一种货币的转换值,请从数据库中获取汇率。

如果我们想要根据单个列来查找数据,可以传递列名。例如:

ExchangeValue findByFrom (String from);

内部工作:

我们将使用 JPA criteria API 创建一个查询,但实质上这将转换为以下查询:

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

Spring Data JPA 将进行属性检查并遍历嵌套属性,如 ???. 中所述。

如果 And 是关键字,
findByLastnameAndFirstname 是示例,
则 JPQL 片段/查询为 … where x.lastname = ?1 and x.firstname = ?2

更多详细信息请参阅官方文档:https://docs.spring.io/spring-data/jpa/docs/1.5.0.RELEASE/reference/html/jpa.repositories.html

希望对你有所帮助!

英文:
ExchangeValue findByFromAndTo(String from, String to);  

In the above statement, ExchangeValue is the expected response. There are two columns that we have to find are from and 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:

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 ???.

If And is the Keyword,
findByLastnameAndFirstname is the sample,
then JPQL snippet/query is … 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

Hope this helps.!

huangapple
  • 本文由 发表于 2020年4月6日 07:30:25
  • 转载请务必保留本文链接:https://go.coder-hub.com/61050853.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定