1. Spring Boot簡介 初次接觸Spring的時候,我感覺這是一個很難接觸的框架,因為其龐雜的配置文件,我最不喜歡的就是xml文件,這種文件的可讀性很不好。所以很久以來我的Spring學習都是出於停滯狀態的。 不過這種狀態在我接觸了Spring Boot之後,就發生了改變。Spring官方 ...
1. Spring Boot簡介
初次接觸Spring的時候,我感覺這是一個很難接觸的框架,因為其龐雜的配置文件,我最不喜歡的就是xml文件,這種文件的可讀性很不好。所以很久以來我的Spring學習都是出於停滯狀態的。
不過這種狀態在我接觸了Spring Boot之後,就發生了改變。Spring官方可能也覺得龐雜的配置是一件很不優雅的事情,雖然加入了包掃描,但是也沒有觸及靈魂。
Spring還有一個問題就是依賴的衝突問題,這個對我這種半道出家的程式員來說更是痛苦至極。
還好,所有的痛苦都被Spring Boot終結了。
Spring Boot有三個特點:
- 自動配置
- 起步依賴
- Actuator對運行狀態的監控
每一個特點都那麼的美好。
其實開發人員的本職工作是什麼?是完成業務代碼的編寫,實現業務功能,因此如果消耗大量時間在Spring本身的配置和依賴衝突解決上,那麼等於是浪費了大量的時間。
Spring Boot的出現,可以說是對生產力的一次解放。
閑話少敘,看一個需求。
我現在想要寫一個工具,連接資料庫,實現增刪改查功能,恐怕很多程式員會回憶起最初學習Java的時候,那堪稱ugly的JDBC模板代碼了吧。我本身是一個DBA,因為公司安排,寫過很多JDBC代碼,深刻的感覺到這種代碼實在浪費時間,後來接觸了JPA框架,感覺非常好。下麵就用Spring Boot來實現一個資料庫的增刪改查功能。
2. Spring Boot實戰JPA
資料庫我會使用H2,這種嵌入式的資料庫最適合在家學習的時候使用,很小,支持最基本的資料庫操作,只要不涉及到太深刻的內容,感覺和MySQL差不多。至於如何在本機上啟動一個H2 Server,就不在這裡描述了。
首先呢,打開IDEA,遵循下麵的順序:
new Project ->Spring Initializr ->填寫group、artifact ->鉤上web(開啟web功能)->點下一步就行了。
上圖是我選擇的需要的組件。
既然是JPA,那麼我們首先要定義一個Entity,我的表叫做Demo,那麼Entity也就是叫做Demo:
package com.example.springwithjdbc.entity;
import lombok.Data;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
@Entity
@Data
public class Demo {
@Id
@GeneratedValue
private int id;
private String uname;
}
註解解釋:
- @Entity:表示這個類是個Entity;
- @Data:這是lombok提供的功能,這個註解加上之後,就不需要寫getter和setter了,也不用寫toString方法,都會自動生成;
- @Id:表示這個是主鍵;
- @GeneratedValue:表示採用自增
接下來,需要經典的DAO層出現了:
package com.example.springwithjdbc.dao;
import com.example.springwithjdbc.entity.Demo;
import org.springframework.data.jpa.repository.JpaRepository;
public interface DemoDao extends JpaRepository<Demo, Integer> { }
DAO層只是定義了一個interface,沒有進行任何實現,其實也不需要進行任何具體的實現,註意繼承的JpaRepository,它幫我們做了很多需要我們原先手動編碼的工作。
接下來就是經典的Service層了,Service即業務層:
package com.example.springwithjdbc.service;
import com.example.springwithjdbc.entity.Demo;
import java.util.List;
public interface IDemoService {
Demo add(Demo demo);
Demo findById(int id);
List<Demo> findAll();
}
以上代碼是service的介面,接下來編寫具體的實現:
package com.example.springwithjdbc.service;
import com.example.springwithjdbc.dao.DemoDao;
import com.example.springwithjdbc.entity.Demo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class DemoService implements IDemoService {
@Autowired
private DemoDao demoDao;
@Override
public Demo add(Demo demo) {
return demoDao.save(demo);
}
@Override
public Demo findById(int id) {
return demoDao.findById(id).get();
}
@Override
public List<Demo> findAll() {
return demoDao.findAll();
}
}
註解解釋:
@Service:表示這是一個Service;
@Autowired:將DemoDao註入。
接下來,我們需要一個Controller來實現REST介面:
package com.example.springwithjdbc.controller;
import com.example.springwithjdbc.entity.Demo;
import com.example.springwithjdbc.service.IDemoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
@RestController
@RequestMapping("/")
public class DemoRestController {
@Autowired
IDemoService demoService;
@PostMapping("/save")
public String save(@RequestParam(name = "name")String name) {
Demo demo = new Demo();
demo.setUname(name);
return demoService.add(demo).toString();
}
@GetMapping("/find")
public String findById(@RequestParam(name = "id")int id) {
return demoService.findById(id).toString();
}
@GetMapping("/list")
public String findAll() {
List<Demo> demoList = demoService.findAll();
return demoList.toString();
}
}
最後,配置application.yml,配置數據源:
spring:
datasource:
url: jdbc:h2:tcp://localhost/~/code/h2/bin/demo
username: admin
password: admin
jpa:
show-sql: true
啟動這個工程即可,接下來就可以用瀏覽器或者postman進行測試了,這是我用postman進行的測試,首先發送一個POST請求,寫入一條數據:
下麵我們查詢這條數據:
多寫幾條數據以後,調用list介面:
到這裡,我們已經成功的編寫了一段基於Spring Boot的JPA代碼,實現了簡單的新增和查詢功能。比我之前寫的JDBC代碼不知道簡單到哪裡去了,而且也更加的優雅了,再也沒有那麼多複雜的讓人難以看懂的xml配置了。
Spring甚至貼心到做了一個網站專門生成工程骨架。
3. 小結
我最近在學習微服務,要學習微服務繞不開的就是Spring Cloud,而Spring Cloud離不開Spring Boot。因此首先瞭解Spring Boot是很有必要的。
這是我的筆記整理出來的第一篇,希望能夠幫助到其他和我一樣在學習微服務,學習Spring Cloud,學習Spring Boot的人。