1 SpringBoot分層
1.1 Controller
控制業(yè)務(wù)層Service的,它的作用主要是架起了外界與業(yè)務(wù)層溝通的橋梁,移動(dòng)端,前端在調(diào)用接口訪問(wèn)相關(guān)業(yè)務(wù)時(shí),都會(huì)通過(guò)Controller,由Controller去調(diào)相關(guān)的業(yè)務(wù)層代碼并把數(shù)據(jù)返回給移動(dòng)端和前端。
api接口可以直接寫(xiě)在這一層。
1.2 Service
業(yè)務(wù)層,所有的內(nèi)部的業(yè)務(wù)邏輯都會(huì)放在這里處理,比如用戶(hù)的增刪改查,或者發(fā)送個(gè)驗(yàn)證碼或郵件,或者做?個(gè)抽獎(jiǎng)活動(dòng)等,都會(huì)在Service中進(jìn)行。
1.3 dao
數(shù)據(jù)持久化層,就是和數(shù)據(jù)庫(kù)打交道的,而實(shí)現(xiàn)持久化層的框架有很多,而常用的有兩種:JPA和MyBatis,JPA是SpringBoot官方的,前身就是著名的三大框架之一的Hibernate,好處是不用手寫(xiě)SQL。MyBatis則在國(guó)內(nèi)比較流行,原因是它的靈活性非常高,但是需要手寫(xiě)SQL語(yǔ)句。
2 POM文件
2.1 parent
<parent>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-parentartifactId>
<version>2.2.6.RELEASEversion>
parent>
- spring-boot-starter是一個(gè)場(chǎng)景啟動(dòng)器。springboot將所有的功能場(chǎng)景抽取出來(lái),做成一個(gè)個(gè)的啟動(dòng)器starter,只需要在項(xiàng)目里引入這些starter,相關(guān)場(chǎng)景的所有依賴(lài)都會(huì)導(dǎo)入進(jìn)來(lái),要用什么功能就導(dǎo)入什么啟動(dòng)器
這個(gè)parent
為我們管理依賴(lài)的版本,是springboot的版本仲裁中心,以后我們導(dǎo)入的依賴(lài)中不需要寫(xiě)版本。
2.2 starter-web
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-webartifactId>
dependency>
spring-boot-starter-web是一個(gè)場(chǎng)景啟動(dòng)器,啟動(dòng)的是springboot的web場(chǎng)景,同上Ctrl+鼠標(biāo)左鍵
,可以看到啟動(dòng)web場(chǎng)景需要的依賴(lài)有: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>
測(cè)試場(chǎng)景的啟動(dòng)器
2.4 maven-plugin
maven的插件,配置插件的依賴(lài)以后可以進(jìn)行打jar包等操作
<build>
<plugins>
<plugin>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-maven-pluginartifactId>
plugin>
plugins>
build>
2.5 hutool
在 pom 文件內(nèi)添加 hutool 依賴(lài):
<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ù)層,主要用來(lái)進(jìn)行業(yè)務(wù)的邏輯處理
3.3 @repository
實(shí)現(xiàn)dao訪問(wèn)
用于標(biāo)注數(shù)據(jù)訪問(wèn)層,也可以說(shuō)用于標(biāo)注數(shù)據(jù)訪問(wèn)組件,即DAO組件.
3.4 @component
把普通pojo實(shí)例化到spring容器中,相當(dāng)于配置文件中的
泛指各種組件,就是說(shuō)當(dāng)我們的類(lèi)不屬于各種歸類(lèi)的時(shí)候(不屬于@Controller、@Services等的時(shí)候),我們就可以使用@Component來(lái)標(biāo)注這個(gè)類(lèi)。
3.5 @Autowired
與component 相互配合,實(shí)現(xiàn)調(diào)用。
審核編輯:湯梓紅
-
JAVA
+關(guān)注
關(guān)注
19文章
2975瀏覽量
105156 -
代碼
+關(guān)注
關(guān)注
30文章
4828瀏覽量
69061 -
spring
+關(guān)注
關(guān)注
0文章
340瀏覽量
14391 -
Boot
+關(guān)注
關(guān)注
0文章
150瀏覽量
35946 -
SpringBoot
+關(guān)注
關(guān)注
0文章
174瀏覽量
201
發(fā)布評(píng)論請(qǐng)先 登錄
相關(guān)推薦
評(píng)論