原文連結
1 SpringBoot分層
1.1 Controller
控制業務層Service的,它的作用主要是架起了外界與業務層溝通的橋梁,移動端,前端在調用接口通路相關業務時,都會通過Controller,由Controller去調相關的業務層代碼并把資料傳回給移動端和前端。
api接口可以直接寫在這一層。
1.2 Service
業務層,所有的内部的業務邏輯都會放在這裡處理,比如使用者的增删改查,或者發送個驗證碼或郵件,或者做⼀個抽獎活動等,都會在Service中進行。
1.3 dao
資料持久化層,就是和資料庫打交道的,而實作持久化層的架構有很多,而常用的有兩種:JPA和MyBatis,JPA是SpringBoot官方的,前身就是著名的三大架構之一的Hibernate,好處是不用手寫SQL。MyBatis則在國内比較流行,原因是它的靈活性非常高,但是需要手寫SQL語句。
2 POM檔案
2.1 parent
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.6.RELEASE</version>
</parent>
- spring-boot-starter是一個場景啟動器。springboot将所有的功能場景抽取出來,做成一個個的啟動器starter,隻需要在項目裡引入這些starter,相關場景的所有依賴都會導入進來,要用什麼功能就導入什麼啟動器
這個
parent
為我們管理依賴的版本,是springboot的版本仲裁中心,以後我們導入的依賴中不需要寫版本。
2.2 starter-web
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
spring-boot-starter-web是一個場景啟動器,啟動的是springboot的web場景,同上
Ctrl+滑鼠左鍵
,可以看到啟動web場景需要的依賴有:spring-boot-starter、spring-boot-starter-json、spring-boot-starter-tomcat等。
2.3 starter-test
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
測試場景的啟動器
2.4 maven-plugin
maven的插件,配置插件的依賴以後可以進行打jar包等操作
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
2.5 hutool
在 pom 檔案内添加 hutool 依賴:
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-all</artifactId>
<version>5.0.6</version>
</dependency>
2.6 log
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
2.7 lang
<dependency>
<groupId>commons-lang</groupId>
<artifactId>commons-lang</artifactId>
<version>2.6</version>
</dependency>
2.8 lang3
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.3.2</version>
</dependency>
3 注解
3.1 @controller 控制器
注入服務
用于标注控制層,相當于struts中的action層
3.2 @service 服務
注入dao
用于标注服務層,主要用來進行業務的邏輯處理
3.3 @repository
實作dao通路
用于标注資料通路層,也可以說用于标注資料通路元件,即DAO元件.
3.4 @component
把普通pojo執行個體化到spring容器中,相當于配置檔案中的 <bean id="" class=""/>
泛指各種元件,就是說當我們的類不屬于各種歸類的時候(不屬于@Controller、@Services等的時候),我們就可以使用@Component來标注這個類。
3.5 @Autowired
與component 互相配合,實作調用。