iT邦幫忙

2022 iThome 鐵人賽

DAY 27
0
DevOps

一個人也能 DevOps ? 用 Angular + Spring Boot 演示專案由開發到部署系列 第 27

Day27: Sql Server 搭建 (Container) & Spring Data JPA 介紹

  • 分享至 

  • xImage
  •  

昨天,我們為專案開啟了 Gitflow 跟專屬的 Feature 分支,今天,讓我們用 Container 的形式,一起創建一個 SQL Server,並且為我們的後端增加 Repository 與資料庫對應的程式。

SQL Server in Container

MS SQL Server 是目前各公司常用的資料庫資之一,具有多種搭建的模式,其中一種經典的方式是將 SQL Server 安裝在 Windows Server 上,並且使用 SQL Server Management Studio, SSMS 當操作介面,其中好處是 Developer 可以用電腦遠端連線到 Windows Server 上,並以熟悉的 Windows 介面操作,能大幅度將低使用上的難度。

而今天,我們以 Container 的方式來搭建一台 SQL Server;使用 Container 的好處在於它能夠非常快速 (在幾分鐘內) 搭建完成,再配上 Spring Data JPA,能夠映射 Entity(物件實體) 和 Table,並自動建立對應的 Table。不過在正式的 Production 環境中,還是推薦大家使用更穩定的搭建方式,以及手動撰寫 SQL init Script,並放置於 Git Repository 內管理。

另外一點需要讀者們注意,Container 在移除的時候,會將裡面的資料一併移除,因此若有要儲存 Container 內部資料的需求,通常會使用 Docker Volume,將 Container 內的檔案路徑映射到實體機的檔案路徑中,也就是常見的 docker -v 指令;而本次不特別映射檔案路徑,若有興趣可以參考微軟的官方文件

以下流程 follow Microsoft 官方文件:

首先,我們一樣使用 Putty 遠端連線到 SIT 機器 (192.168.1.188) 上,並將 Docker Image 拉取到機器上,而關於 Image 的資訊也能夠在 Docker Hub 上看到微軟的官方說明,我們先看看拉取的指令:

sudo docker pull mcr.microsoft.com/mssql/server:2019-CU18-ubuntu-20.04

接著,我們來看看啟動的指令:

sudo docker run -e "ACCEPT_EULA=Y" -e "MSSQL_SA_PASSWORD=<YourStrong@Passw0rd>" \
   -p 1433:1433 --name sqlserver \
   -d \
   server:2019-CU18-ubuntu-20.04

這裡稍微解釋一下:

  • ACCEPT_EULA=Y: 是必要的環境變數,主要是同意 End-User 的授權合約
  • MSSQL_SA_PASSWORD=<YourStrong@Passw0rd>: 也是必要的環境變數,為 SA 的密碼
  • -p 1433:1433: 將 Container 的 port 對應到實體機的 port
  • --name: 為 Container 取名
  • -d: 在背景執行

其中 SA 的密碼規定長度必須至少為八個字元,且包含下列四組的三個字元:大寫字母、小寫字母、基底 10 位數和符號。我們把 <YourStrong@Passw0rd> 更改為自定義的密碼後,即可來啟動 Container。

啟動後,我們便可以使用 SSMS 進行連線,在伺服器名稱輸入 IP, port,也就是 192.168.1.188, 1433,帳號為 sa,密碼則是剛才定義的密碼。

https://ithelp.ithome.com.tw/upload/images/20221012/201328786jJLzkxeFD.png

連線後,我們則可以在裡面新增一個資料庫,並命名為 CCT_DB。

https://ithelp.ithome.com.tw/upload/images/20221012/20132878AAB8LGHtXq.png

Spring Data JPA 介紹

在介紹 Spring Data JPA 之前,我們要先明白什麼是 JPA (Java Persistence API),JPA 是 SUN 公司 (後來被 Oracle 收購了) 提出的 ORM 規範,目的是簡化和整合各家 ORM 的技術,而 Spring Data JPA 則是 Spring 基於 ORM 框架和 JPA 的規範所封裝出的一套框架。

基本上,我們只要撰寫對應到資料庫的實體物件 (Entity),並利用 Repository 這個 Interface,就可以對資料庫進行 CRUD,甚至還能夠以方法的命名 (Ex: findAllByXxx) 來完成對資料庫的操作;當面對較複雜的需求時,還能夠使用 Native Query 完成。

這裡可以回顧一下 Day8 的 Controller、Service、Repository 三層式架構,我們原現在 Controller 內注入了 Service (Interface),並創建 ServiceImpl 來實作顏色分群的方法;接下來我們則會建立 Repository 資料夾,並在內部建立 dao 資料夾存放 Repository,以及 po 資料夾放置 Entity;之後,我們即可在 ServiceImpl 內部注入 Repository,將產生的色碼字串儲存到 DB 內部。

首先,我們在 pom 內先引入 jpa 和 sqlserver 的依賴:

<dependency>
	<groupId>com.microsoft.sqlserver</groupId>
	<artifactId>mssql-jdbc</artifactId>
	<scope>runtime</scope>
</dependency>
<dependency>
	<groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>

接著,我們創建 Repository 和 dao、po 資料夾,並建立 Entity 和 Repository。

@Entity
@Data
@Table(name = "COLOR_CODE")
public class ColorCodeEntity implements Serializable {

	private static final long serialVersionUID = -5387706567843475294L;
	
	@Id
	@GeneratedValue(strategy = GenerationType.IDENTITY)
	@Column(name = "ID")
	private Long id;
	
	@Column(name = "COLOR_CODE_STRING")
	private String colorCodeString;

}
@Repository
public interface ColorCodeEntityRepository extends JpaRepository<ColorCodeEntity, Long> {

}

接著,我們來設定 application.properties

spring.datasource.url=jdbc:sqlserver://192.168.1.188:1433;DatabaseName=CCT_DB;
spring.datasource.username=sa
spring.datasource.password=Passw3rd

spring.jpa.hibernate.ddl-auto= update
spring.jpa.hibernate.dialect: org.hibernate.dialect.SQLServer2012Dialect    

到這裡,我們將專案啟動,這時可以看到 JPA 為我們建立了 Table。

https://ithelp.ithome.com.tw/upload/images/20221012/20132878CDf2SlrC9W.png

今日總結

今天,我們快速地在 SIT 環境內搭建了 SQL Server,並且利用 Spring Data JPA 為我們創建了表格。明天,我們就可以實踐顏色在 DB 的儲存,並且一起完成後面的需求。


上一篇
Day26: Gitflow 創建 & Backlogs 整理
下一篇
Day28: Gitflow 合併與觸發部署、Backlogs 完善
系列文
一個人也能 DevOps ? 用 Angular + Spring Boot 演示專案由開發到部署30
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言