1 SpringBoot分層
1.1 Controller
控制業(yè)務(wù)層Service的,它的作用主要是架起了外界與業(yè)務(wù)層溝通的橋梁,移動端,前端在調(diào)用接口訪問相關(guān)業(yè)務(wù)時,都會通過Controller,由Controller去調(diào)相關(guān)的業(yè)務(wù)層代碼并把數(shù)據(jù)返回給移動端和前端。
api接口可以直接寫在這一層。
1.2 Service
業(yè)務(wù)層,所有的內(nèi)部的業(yè)務(wù)邏輯都會放在這里處理,比如用戶的增刪改查,或者發(fā)送個驗證碼或郵件,或者做?個抽獎活動等,都會在Service中進行。
1.3 dao
數(shù)據(jù)持久化層,就是和數(shù)據(jù)庫打交道的,而實現(xiàn)持久化層的框架有很多,而常用的有兩種:JPA和MyBatis,JPA是SpringBoot官方的,前身就是著名的三大框架之一的Hibernate,好處是不用手寫SQL。MyBatis則在國內(nèi)比較流行,原因是它的靈活性非常高,但是需要手寫SQL語句。
2 POM文件
2.1 parent
<parent>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-parentartifactId>
<version>2.2.6.RELEASEversion>
parent>
- spring-boot-starter是一個場景啟動器。springboot將所有的功能場景抽取出來,做成一個個的啟動器starter,只需要在項目里引入這些starter,相關(guān)場景的所有依賴都會導(dǎo)入進來,要用什么功能就導(dǎo)入什么啟動器
這個parent
為我們管理依賴的版本,是springboot的版本仲裁中心,以后我們導(dǎo)入的依賴中不需要寫版本。
2.2 starter-web
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-webartifactId>
dependency>
spring-boot-starter-web是一個場景啟動器,啟動的是springboot的web場景,同上Ctrl+鼠標(biāo)左鍵
,可以看到啟動web場景需要的依賴有:spring-boot-starter、spring-boot-starter-json、spring-boot-starter-tomcat等。
2.3 starter-test
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-testartifactId>
<scope>testscope>
<exclusions>
<exclusion>
<groupId>org.junit.vintagegroupId>
<artifactId>junit-vintage-engineartifactId>
exclusion>
exclusions>
dependency>
測試場景的啟動器
2.4 maven-plugin
maven的插件,配置插件的依賴以后可以進行打jar包等操作
<build>
<plugins>
<plugin>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-maven-pluginartifactId>
plugin>
plugins>
build>
2.5 hutool
在 pom 文件內(nèi)添加 hutool 依賴:
<dependency>
<groupId>cn.hutoolgroupId>
<artifactId>hutool-allartifactId>
<version>5.0.6version>
dependency>
2.6 log
<dependency>
<groupId>log4jgroupId>
<artifactId>log4jartifactId>
<version>1.2.17version>
dependency>
2.7 lang
<dependency>
<groupId>commons-langgroupId>
<artifactId>commons-langartifactId>
<version>2.6version>
dependency>
2.8 lang3
<dependency>
<groupId>org.apache.commonsgroupId>
<artifactId>commons-lang3artifactId>
<version>3.3.2version>
dependency>
3 注解
3.1 @controller 控制器
注入服務(wù)
用于標(biāo)注控制層,相當(dāng)于struts中的action層
3.2 @service 服務(wù)
注入dao
用于標(biāo)注服務(wù)層,主要用來進行業(yè)務(wù)的邏輯處理
3.3 @repository
實現(xiàn)dao訪問
用于標(biāo)注數(shù)據(jù)訪問層,也可以說用于標(biāo)注數(shù)據(jù)訪問組件,即DAO組件.
3.4 @component
把普通pojo實例化到spring容器中,相當(dāng)于配置文件中的
泛指各種組件,就是說當(dāng)我們的類不屬于各種歸類的時候(不屬于@Controller、@Services等的時候),我們就可以使用@Component來標(biāo)注這個類。
3.5 @Autowired
與component 相互配合,實現(xiàn)調(diào)用。
審核編輯:湯梓紅
-
JAVA
+關(guān)注
關(guān)注
19文章
2943瀏覽量
104106 -
代碼
+關(guān)注
關(guān)注
30文章
4671瀏覽量
67770 -
spring
+關(guān)注
關(guān)注
0文章
335瀏覽量
14259 -
Boot
+關(guān)注
關(guān)注
0文章
148瀏覽量
35675 -
SpringBoot
+關(guān)注
關(guān)注
0文章
172瀏覽量
145
發(fā)布評論請先 登錄
相關(guān)推薦
評論