Spring Boot 入門或 .NET 開發人員部分在 Spring Boot 中建立產品實體 CRUD 應用程式
在上一篇文章中探索了Spring Boot 3 的基礎知識後,讓我們透過實作Product 實體CRUD(創建、讀取、更新、刪除)操作來更深入地了解。在此過程中,我們將比較核心 Spring Boot 概念與 .NET Core 對應概念,以幫助彌合 .NET 開發人員過渡到 Java 生態系統的差距。
設定項目
開始之前,請確保您已準備好一個具有以下相依性的 Spring Boot 專案:
- Spring Web:用於建立 REST API。
- Spring Data JPA:用於資料庫互動。
- PostgreSQL 驅動程式:用於連接到 PostgreSQL 資料庫。
使用 Docker 在本地運行 PostgreSQL
要在本地運行 PostgreSQL,請使用 Docker 快速設定實例:
-
拉取 PostgreSQL 鏡像:
docker pull postgres
登入後複製登入後複製 -
運行 PostgreSQL 容器:
docker run --name postgres-db -e POSTGRES_PASSWORD=yourpassword -e POSTGRES_USER=yourusername -e POSTGRES_DB=mydatabase -p 5432:5432 -d postgres
登入後複製登入後複製將您的使用者名稱、您的密碼和 mydatabase 替換為您想要的使用者名稱、密碼和資料庫名稱。
-
驗證資料庫正在運作:
docker ps
登入後複製登入後複製 使用資料庫用戶端(例如 DBeaver、pgAdmin 或 psql)連接到 localhost:5432 並驗證您的資料庫是否可存取。
更新 pom.xml 檔案
如果您使用 Maven,請在 pom.xml 檔案中包含以下依賴項,以確保所有必需的程式庫可用:
<dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-jpa</artifactId> </dependency> <dependency> <groupId>org.postgresql</groupId> <artifactId>postgresql</artifactId> <version>42.5.0</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> </dependencies>
此外,確保包含以下外掛程式來建置專案:
<build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build>
與 .NET Core 的比較:
在 .NET Core 中,套件參考是使用 csproj 檔案管理的。 PostgreSQL 支援的 Web API 的等效相依性可能如下所示:
<ItemGroup> <PackageReference Include="Microsoft.AspNetCore.App" /> <PackageReference Include="Npgsql.EntityFrameworkCore.PostgreSQL" Version="6.0.0" /> </ItemGroup>
設定 PostgreSQL 資料庫
更新您的 application.yml 檔案以連接到 PostgreSQL 資料庫:
spring: datasource: url: jdbc:postgresql://localhost:5432/mydatabase username: yourusername password: yourpassword jpa: properties: hibernate: dialect: org.hibernate.dialect.PostgreSQLDialect hibernate: ddl-auto: update
將 mydatabase、yourusername 和 yourpassword 替換為您的實際 PostgreSQL 資料庫詳細資訊。 ddl-auto=update 設定確保 Hibernate 根據您的實體定義自動建立或更新表。
與 .NET Core 的比較:
在 .NET Core 中,類似的配置將駐留在 appsettings.json 中:
{ "ConnectionStrings": { "DefaultConnection": "Host=localhost;Database=mydatabase;Username=yourusername;Password=yourpassword" }, "EntityFramework": { "MigrationsAssembly": "YourProjectName" } }
專案結構概述
Spring Boot 專案將程式碼組織到套件中:
- 實體:包含資料模型。
- repository:資料庫操作的介面。
- 控制器:REST端點。
- 服務(可選):業務邏輯。
此結構類似 .NET Core 專案中的典型層:模型、資料/儲存庫、控制器和服務。
第 1 步:定義產品實體
在 Spring Boot 中,實體代表資料庫表,類似 Entity Framework Core 中的模型。使用 @Entity 和 @Id 等註解將類別對應到表:
docker pull postgres
.NET Core 等效項
docker run --name postgres-db -e POSTGRES_PASSWORD=yourpassword -e POSTGRES_USER=yourusername -e POSTGRES_DB=mydatabase -p 5432:5432 -d postgres
第 2 步:建立儲存庫
在 Spring Boot 中,儲存庫是擴充 JpaRepository 的介面。它們提供內建的 CRUD 操作,類似於 EF Core 中的 DbContext。
docker ps
.NET Core 等效項
<dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-jpa</artifactId> </dependency> <dependency> <groupId>org.postgresql</groupId> <artifactId>postgresql</artifactId> <version>42.5.0</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> </dependencies>
第 3 步:實作服務層(選用)
服務層處理業務邏輯。雖然是可選的,但對於大型應用程式來說這是一個很好的做法。
<build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build>
.NET Core 等效項
<ItemGroup> <PackageReference Include="Microsoft.AspNetCore.App" /> <PackageReference Include="Npgsql.EntityFrameworkCore.PostgreSQL" Version="6.0.0" /> </ItemGroup>
第 4 步:建構控制器
控制器處理 HTTP 請求,就像在 ASP.NET Core 中一樣。
spring: datasource: url: jdbc:postgresql://localhost:5432/mydatabase username: yourusername password: yourpassword jpa: properties: hibernate: dialect: org.hibernate.dialect.PostgreSQLDialect hibernate: ddl-auto: update
.NET Core 等效項
{ "ConnectionStrings": { "DefaultConnection": "Host=localhost;Database=mydatabase;Username=yourusername;Password=yourpassword" }, "EntityFramework": { "MigrationsAssembly": "YourProjectName" } }
第 5 步:測試您的 API
運行您的應用程式並使用 Postman 或 cURL 等工具測試端點。確保您的 PostgreSQL 資料庫正在運作並正確配置。
應用程式啟動並執行後,使用 Postman 或 cURL 測試 CRUD 端點。確保 PostgreSQL 正在運作並正確配置。
使用 Postman 測試端點:
- GET /api/products:取得所有產品。
- GET /api/products/{id}:透過 ID 取得單一產品。
- POST /api/products:建立一個新產品。
- DELETE /api/products/{id}:透過 ID 刪除產品。
主要比較
Feature | Spring Boot 3 | .NET Core |
---|---|---|
Dependency Injection | Built-in with @Autowired or constructor injection | Built-in with AddScoped, AddSingleton |
ORM Tool | Spring Data JPA | Entity Framework Core |
Routing | @RequestMapping, @GetMapping | [Route], [HttpGet] |
Middleware | Spring Interceptors | ASP.NET Middleware |
Response Handling | ResponseEntity | IActionResult |
結論
在 Spring Boot 中建立 CRUD 應用程式很簡單,特別是對於熟悉 .NET Core 的人來說。依賴注入、ORM 和 RESTful API 的原理在兩個生態系中都很相似。本指南只是一個開始 - 未來的貼文將涵蓋 Lombok 整合、Swagger/OpenAPI、驗證、錯誤處理和資料庫遷移。敬請期待!
編碼愉快!
參考文獻
- Spring Boot 文件:https://spring.io/projects/spring-boot
- PostgreSQL 文件:https://www.postgresql.org/docs/
- Spring Data JPA 文件:https://spring.io/projects/spring-data-jpa
- .NET Core 文件:https://docs.microsoft.com/en-us/aspnet/core/?view=aspnetcore-7.0
以上是Spring Boot 入門或 .NET 開發人員部分在 Spring Boot 中建立產品實體 CRUD 應用程式的詳細內容。更多資訊請關注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)

公司安全軟件導致部分應用無法正常運行的排查與解決方法許多公司為了保障內部網絡安全,會部署安全軟件。 ...

系統對接中的字段映射處理在進行系統對接時,常常會遇到一個棘手的問題:如何將A系統的接口字段有效地映�...

在使用MyBatis-Plus或其他ORM框架進行數據庫操作時,經常需要根據實體類的屬性名構造查詢條件。如果每次都手動...

將姓名轉換為數字以實現排序的解決方案在許多應用場景中,用戶可能需要在群組中進行排序,尤其是在一個用...

在使用IntelliJIDEAUltimate版本啟動Spring...

Java對象與數組的轉換:深入探討強制類型轉換的風險與正確方法很多Java初學者會遇到將一個對象轉換成數組的�...

電商平台SKU和SPU表設計詳解本文將探討電商平台中SKU和SPU的數據庫設計問題,特別是如何處理用戶自定義銷售屬...

在使用TKMyBatis進行數據庫查詢時,如何優雅地獲取實體類變量名以構建查詢條件,是一個常見的難題。本文將針...
