网站首页

人工智能P2P分享搜索全网发布信息网站地图标签大全

当前位置:诺佳网 > 电子/半导体 > 嵌入式技术 >

SpringBoot基础知识

时间:2023-01-13 14:45

人气:

作者:admin

标签: 代码  Spring  Boot  JAVA 

导读:控制业务层Service的,它的作用主要是架起了外界与业务层沟通的桥梁,移动端,前端在调用接口访问相关业务时,都会通过Controller,由Controller去调相关的业务层代码并把数据返回给移...

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.bootgroupId>
    <artifactId>spring-boot-starter-parentartifactId>
    <version>2.2.6.RELEASEversion>
parent>
  • spring-boot-starter是一个场景启动器。springboot将所有的功能场景抽取出来,做成一个个的启动器starter,只需要在项目里引入这些starter,相关场景的所有依赖都会导入进来,要用什么功能就导入什么启动器

这个parent为我们管理依赖的版本,是springboot的版本仲裁中心,以后我们导入的依赖中不需要写版本。

2.2 starter-web

<dependency>
    <groupId>org.springframework.bootgroupId>
    <artifactId>spring-boot-starter-webartifactId>
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.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 文件内添加 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 控制器

注入服务

用于标注控制层,相当于struts中的action层

3.2 @service 服务

注入dao

用于标注服务层,主要用来进行业务的逻辑处理

3.3 @repository

实现dao访问

用于标注数据访问层,也可以说用于标注数据访问组件,即DAO组件.

3.4 @component

把普通pojo实例化到spring容器中,相当于配置文件中的

泛指各种组件,就是说当我们的类不属于各种归类的时候(不属于@Controller、@Services等的时候),我们就可以使用@Component来标注这个类。

3.5 @Autowired

与component 相互配合,实现调用。

审核编辑:汤梓红

温馨提示:以上内容整理于网络,仅供参考,如果对您有帮助,留下您的阅读感言吧!
相关阅读
本类排行
相关标签
本类推荐

CPU | 内存 | 硬盘 | 显卡 | 显示器 | 主板 | 电源 | 键鼠 | 网站地图

Copyright © 2025-2035 诺佳网 版权所有 备案号:赣ICP备2025066733号
本站资料均来源互联网收集整理,作品版权归作者所有,如果侵犯了您的版权,请跟我们联系。

关注微信