原文链接

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 相互配合,实现调用。

 
 

学习更多编程常识,请关注我的公众号:

代码的路