Java儲存庫
在Java Spring框架中,通常使用基於JPA的儲存庫。這些儲存庫對於管理資料庫操作至關重要,並且是 Spring Data JPA 模組的一部分。儲存庫定義了一種新的優雅方法,用於儲存、更新和提取後端 JAVA 應用程式儲存的資料。所有 CRUD(建立、讀取、更新和刪除)操作都可以藉助儲存庫介面來實現。 JPA是JAVA Persistence API(應用程式介面)的縮寫。顧名思義,JPA 有助於將 java 物件持久保存在關聯式資料庫中。有兩種方法可以做到這一點,它們是:
廣告 該類別中的熱門課程 JAVA 掌握 - 專業化 | 78 課程系列 | 15 次模擬測驗開始您的免費軟體開發課程
網頁開發、程式語言、軟體測試及其他
- 關係表使用映射類別對應到子系統。
- EntityManager API。
在本文中,我們將了解語法以及 JPA 儲存庫在 CRUD 操作中的使用。
文法:
匯入所有必要的程式庫並將其連結到專案類別路徑中的 Spring、Persistence 物件和 Java EE 後,下一步就是透過擴充「JpaRepository」介面來建立介面。這是儲存庫庫的擴展,包含其他儲存庫,如 CrudRepository 和 PagingAndSortingRepository,以及儲存庫的基本功能。
注意: POM.XML 應該存在於您的專案中才能使用 JPA 應用程式。結構:
import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.stereotype.Repository; import net.guides.springboot.jparepository.model.Employee; @Repository public interface repositoryname extends JpaRepository<Parameter 1 column name, parameter 2 data type> { } //Invocation of the interface created above. private repositoryname RepositoryName; @Override //Different functions from JPA library to be used for enabling transactions with databases. public void run(String…... arguments) throws Exception { RepositoryName.save(new TableName("Data1", "Data2", "Data3")); RepositoryName.save(new TableName("Data1", "Data2", "Data3")); RepositoryName.save(new TableName("Data1", "Data2", "Data3")); RepositoryName.save(new TableName("Data1", "Data2", "Data3")); logger.info("number of elements in table now: {}", RepositoryName.count());
JPA 儲存庫如何運作?
這些實作對於在使用 Java 開發的 Web 或桌面應用程式中實現持久性至關重要。要使這些介面正常運作,必須將所有依賴庫載入到類別路徑中。 建立介面後,可以使用「save()」、「count()」、「info()」、「findAll()」、「sort()」等函數來完成資料查詢或所需的操作。數據操縱。我們需要設定一個資料庫,以便透過 java 應用程式插入、更新或刪除下面表中的值。使用儲存庫可以輕鬆處理資料庫中的資料以及安全交易。
Java 儲存庫範例
對於初學者來說,實作 JPA 儲存庫是一個複雜的時間專案。所有連結庫、JAR、依賴項、伺服器設定和資料庫設定都是先決條件。
檔:pom.xml
程式碼:可以從 https://start.spring.io 下載。人們可以產生一個檔案並在根據系統和應用程式要求選擇值後下載它。
注意: 假設 H2 資料庫中已存在名為「user」的表的資料庫,其中包含「Id」和「Name」兩列。 “Id”是系統自動產生的主鍵。檔案:UserControl.java
代碼:
package test.controller; import test.model.User import test.repository.UserJpaRespository; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.*; import java.util.List; @RestController @RequestMapping("/users") public class UsersControl { @Autowired private UserJpaRespository userJpaRespository; @GetMapping(value = "/all") public List<User> findAll() { return userJpaRespository.findAll(); } @PostMapping(value = "/load") public User load(@RequestBody final User users) { userJpaRespository.save(users); return userJpaRespository.findByName(users.getName()); } }
檔:User.java
代碼:
package test.model import test.controller; import javax.persistence.Entity; import javax.persistence.Id; import javax.persistence.GeneratedValue; @Entity public class User { private Long id; private String name; @Id @GeneratedValue public Long getId() { return id;} public void setId(Long id) { this.id = id;} public String getName() { return name;} public void setName(String name) { this.name = name;} }
檔:UserJPARepository.java
代碼:
package test.repository; import test.controller; import package.model.User; import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.stereotype.Component; @Component public interface UserJpaRespository extends JpaRepository<User, Long>{ }
檔:Implementation.java
代碼:
package test.implementation; import static org.hamcrest.CoreMatchers.is; import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post; import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; import java.util.ArrayList; import java.util.List; import test.controller; import org.junit.Before; import org.junit.*; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest; import org.springframework.http.MediaType; import org.springframework.test.context.junit4.SpringRunner; import org.springframework.test.web.servlet.MockMvc; import org.springframework.test.web.servlet.MvcResult; import com.fasterxml.jackson.databind.ObjectMapper; import test.model.user; import com.fasterxml.jackson.databind.ObjectMapper; import test.repository.UserJpaRespository; @RunWith(SpringRunner.class) public class Implementation { private User user; @MockBean private UserJpaRespository userJpaRespository; @Before public void setUp() { user = new User(); user.setId(1l); user.setName("Virat"); } }
輸出:
這裡的值被插入到資料庫中。
說明
第一個檔案「UserControl」包含有關使用「@GetMapping()」和「@PostMapping」等 JPA 函數提取或儲存值的詳細信息,這些函數需要一些基本檔案來支援它的工作。這些支援檔案是 User.java 和 UserJPARepository.java。
檔案「User.java」以Java物件的形式維護資料庫的結構,允許使用Java持久性物件存取資料庫。要開始處理該項目,您必須使用步驟 1 中提供的原始程式碼產生「pom.xml」檔案。在提供的輸出中,「pom.xml」檔案負責設定專案所需的所有相依性。依賴項包含「spring框架」相關資料和持久化物件相關資料。 UserJPARepository.java 檔案透過擴充內建庫 JpaRepository 來啟動 JPA 儲存庫。
結論
JPA 儲存庫非常有用,因為它提供了一個通用平台來用 JAVA 語言建立查詢,但可以與下面的任何資料庫一起使用。它透過提供優雅的函數來完成庫支援的任務,從而減少程式碼行數。它減少了“樣板”程式碼的使用,從而改善了外觀和執行速度。
以上是Java儲存庫的詳細內容。更多資訊請關注PHP中文網其他相關文章!

熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

Video Face Swap
使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱門文章

熱工具

記事本++7.3.1
好用且免費的程式碼編輯器

SublimeText3漢化版
中文版,非常好用

禪工作室 13.0.1
強大的PHP整合開發環境

Dreamweaver CS6
視覺化網頁開發工具

SublimeText3 Mac版
神級程式碼編輯軟體(SublimeText3)

Java 8引入了Stream API,提供了一種強大且表達力豐富的處理數據集合的方式。然而,使用Stream時,一個常見問題是:如何從forEach操作中中斷或返回? 傳統循環允許提前中斷或返回,但Stream的forEach方法並不直接支持這種方式。本文將解釋原因,並探討在Stream處理系統中實現提前終止的替代方法。 延伸閱讀: Java Stream API改進 理解Stream forEach forEach方法是一個終端操作,它對Stream中的每個元素執行一個操作。它的設計意圖是處

PHP是一種廣泛應用於服務器端的腳本語言,特別適合web開發。 1.PHP可以嵌入HTML,處理HTTP請求和響應,支持多種數據庫。 2.PHP用於生成動態網頁內容,處理表單數據,訪問數據庫等,具有強大的社區支持和開源資源。 3.PHP是解釋型語言,執行過程包括詞法分析、語法分析、編譯和執行。 4.PHP可以與MySQL結合用於用戶註冊系統等高級應用。 5.調試PHP時,可使用error_reporting()和var_dump()等函數。 6.優化PHP代碼可通過緩存機制、優化數據庫查詢和使用內置函數。 7

PHP和Python各有優勢,選擇應基於項目需求。 1.PHP適合web開發,語法簡單,執行效率高。 2.Python適用於數據科學和機器學習,語法簡潔,庫豐富。

PHP適合web開發,特別是在快速開發和處理動態內容方面表現出色,但不擅長數據科學和企業級應用。與Python相比,PHP在web開發中更具優勢,但在數據科學領域不如Python;與Java相比,PHP在企業級應用中表現較差,但在web開發中更靈活;與JavaScript相比,PHP在後端開發中更簡潔,但在前端開發中不如JavaScript。

PHP和Python各有優勢,適合不同場景。 1.PHP適用於web開發,提供內置web服務器和豐富函數庫。 2.Python適合數據科學和機器學習,語法簡潔且有強大標準庫。選擇時應根據項目需求決定。

膠囊是一種三維幾何圖形,由一個圓柱體和兩端各一個半球體組成。膠囊的體積可以通過將圓柱體的體積和兩端半球體的體積相加來計算。本教程將討論如何使用不同的方法在Java中計算給定膠囊的體積。 膠囊體積公式 膠囊體積的公式如下: 膠囊體積 = 圓柱體體積 兩個半球體體積 其中, r: 半球體的半徑。 h: 圓柱體的高度(不包括半球體)。 例子 1 輸入 半徑 = 5 單位 高度 = 10 單位 輸出 體積 = 1570.8 立方單位 解釋 使用公式計算體積: 體積 = π × r2 × h (4

PHPhassignificantlyimpactedwebdevelopmentandextendsbeyondit.1)ItpowersmajorplatformslikeWordPressandexcelsindatabaseinteractions.2)PHP'sadaptabilityallowsittoscaleforlargeapplicationsusingframeworkslikeLaravel.3)Beyondweb,PHPisusedincommand-linescrip

PHP成為許多網站首選技術棧的原因包括其易用性、強大社區支持和廣泛應用。 1)易於學習和使用,適合初學者。 2)擁有龐大的開發者社區,資源豐富。 3)廣泛應用於WordPress、Drupal等平台。 4)與Web服務器緊密集成,簡化開發部署。
