Udemy 課程Microservices with Spring Cloud 33

改進CurrencyExchangeService專案-JPA & H2 DB

ZONGRU Li
3 min readJul 16, 2019

在pom檔內加入兩段,引入JPA與H2 DB

並設定application.properties

將ExchangeValue Bean改寫為Entity

在resources目錄下建立data.sql

data.sql內容

接著執行程式

隨便挑個8087

執行過程式會失敗並看到以下

無預期的表示式

是因為我們用到DB的關鍵字"from"

所以要再改造一下ExchangeValue Bean & data.sql

然後再看到

看來port也得給個預設值

所以再改data.sql

再一次重新執行得

進到網址列:http://localhost:8087/currency-exchange/from/Hog/to/PIG

回應目前是寫死的,不是DB的數值回傳

進到網址:http://localhost:8087/h2-console可以看到以下

按連線

接著使用JAP Repository

建立interface ExchangeValueRepository

在CurrencyExchangeController注入ExchangeValueRepository並使用Method

然後真的要進到合理的網址:

http://localhost:8087/currency-exchange/from/USD/to/INR

正常執行結果!!

--

--

ZONGRU Li
ZONGRU Li

Written by ZONGRU Li

2022/11/17 開源部分個人筆記給LINE "Java程式語言討論區"社群,希望能對社群的技術學習做一點點貢獻.(掩面....記得退訂閱!

No responses yet