簡體   English   中英

Spring 的 JpaRepository 如何查找並連接到數據庫服務器?

[英]How does Spring's JpaRepository find and connect to a DB server?

我正在瀏覽 Spring 的參考應用程序Sagan的源代碼。 我正在閱讀 Sagan 從相關 JpaRepository(例如PostRepository )中提取所需實體(例如Post )的邏輯。

我很確定這樣的 JpaRepositories 會為客戶端 Spring 類進行數據庫查找,但我腦子里缺少的部分是“JpaRepositories 如何找到並連接到目標數據庫?”

例如,可能有多個 DB 服務器在同一台機器上運行。 它們甚至可能具有不同的實現(一些可能是 PostgreSQL,而另一些可能是 MySQL)。 此外,它們可能具有具有同名表的數據庫,並且可能與@Entity類中聲明的表名一致。

那么,JpaRepository 如何篩選大量數據庫服務器並找到正確的服務器及其表? 我在 Sagan 的代碼庫中找不到任何將兩者連接起來的自定義邏輯。

默認情況下,JpaRepository 從 DataSourceProperties 獲取 DB 連接設置。 通常在 application.properties 文件中設置此設置。 舉個例子:

spring.jpa.hibernate.ddl-auto=update
spring.datasource.url=jdbc:mysql://localhost:3306/users
spring.datasource.username=user
spring.datasource.password=user123
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM