文章目录
SpringBoot简介
入门案例
SpringBoot是由Pivotal团队提供的全新框架,其设计目的是用来简化Spring应用的初始搭建及开发过程
 
入门案例搭建
手动创建方式:
- 新建maven项目
 - 引入依赖
 

- 创建引导类
 

- 创建开发控制器类
 
@RestController
@RequestMapping("/books")
public class BookController {
    @GetMapping("/{id}")
    public String getById(@PathVariable Integer id) {
        System.out.println("id ==> " + id);
        return "hello , spring boot! ";
    }
}
 
- 运行生成的Application类
 

SpringBoot脚手架方式:
- 创建新模块,选择Spring初始化,并配置模块相关基础信息
 

- 选择当前模块需要使用的技术集
 

- 开发控制器类
 
@RestController
@RequestMapping("/books")
public class BookController {
    @GetMapping("/{id}")
    public String getById(@PathVariable Integer id) {
        System.out.println("id ==> " + id);
        return "hello , spring boot! ";
    }
}
 
- 运行自动生成的Application类
 

- Spring程序与SpringBoot程序对比
 

注意事项:
基于idea开发SpringBoot程序需要确保联网且能够加载到程序框架结构
 
基于SpringBoot官网创建项目

 
SpringBoot项目快速启动
- 对SpringBoot项目进行打包(执行Maven构建指令package)
 - 执行启动指令
 
java -jar springboot_01_quickstart.jar	# 项目的名称根据实际情况修改
 
注意:
jar支持命令行启动需要依赖maven插件支持,请确定打包时是否具有SpringBoot对应的maven插件
<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
        </plugin>
    </plugins>
</build>
 
 
SpringBoot概述
起步依赖
- starter 
  
- SpringBoot中常见项目名称,定义了当前项目使用的所有项目坐标,以达到减少依赖配置的目的
 
 - parent 
  
- 所有SpringBoot项目要继承的项目,定义了若干个坐标版本号(依赖管理,而非依赖),以达到减少依赖冲突的目的
 - spring-boot-starter-parent(2.5.0)与 spring-boot-starter-parent(2.4.6)共计57处坐标版本不同
 
 - 实际开发 
  
- 使用任意坐标时,仅书写GAV中的G和A,V由SpringBoot提供
 - 如发生坐标错误,再指定version(要小心版本冲突)
 
 
默认配置
tomact默认配置了端口号为8080
 
基础配置
配置文件格式
修改服务器端口
SpringBoot提供了多种属性配置方式
- application.properties
 
server.port=80
 
- application.yml
 
server:
  port: 81
 
- application.yaml
 
server:
  port: 82
 
 
SpringBoot配置文件加载顺序
application.properties> application.yml>application.yaml
注意事项:
- SpringBoot核心配置文件文件名为application
 - SpringBoot内置属性过多,且所有属性集中在一起修改,在使用时,通过提示键+关键字修改属性
 
yaml
- YAML(YAML Ain’t Markup Language),一种数据序列化格式
 - 优点 
  
- 容易阅读
 - 容易与脚本语言交互
 - 以数据为核心,重数据轻格式
 
 - YAML文件扩展名 
  
- .yml(主流)
 - .yaml
 
 
yaml语法规则
- 大小写敏感
 - 属性层级关系使用多行描述,每行结尾使用冒号结束
 - 使用缩进表示层级关系,同层级左侧对其,只允许使用空格(不允许使用Tab键)
 - 属性值前面添加空格(属性名与属性值之间使用冒号+空格作为分隔)
 - #表示注释
 - 核心规则:数据前面加空格与冒号隔开
 
yaml数组数据
- 数组数据在数据书写位置的下方使用减号作为数据开始符号,每行书写一个数据,减号与数据中间用空格分割
 

 
yaml数据读取
- 使用@Value读取单个数据,属性名引用方式:${一级属性名.二级属性名…}
 

- 封装全部数据到Environment对象
 

- 自定义对象封装指定数据(常用)
 
public class Enterprise {
    private String name;
    private Integer age;
    private String tel;
    private String[] subject;
    //自行添加getter、setter、toString()等方法
}
 

- 自定义对象封装数据警告解决方案
 

<dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-configuration-processor</artifactId>
  <optional>true</optional>
</dependency>
 
 
多环境开发配置
多环境启动配置
- yaml文件多环境启动
 



- properties文件多环境启动
 
#主启动配置文件 application.properties
spring.profiles.active=pro
 
#环境分类配置文件 application-pro.properties
server.port=80
 
#环境分类配置文件 application-dev.properties
server.port=81
 
#环境分类配置文件application-test.properties
server.port=82
 
 
多环境启动命令格式
- 带参数启动SpringBoot
 
java –jar springboot.jar --spring.profiles.active=test
java –jar springboot.jar --server.port=88
java –jar springboot.jar --server.port=88 --spring.profiles.active=test
 
- 参数加载优先顺序
 

 
多环境开发控制
Maven与SpringBoot多环境兼容(步骤)
- Maven中设置多环境属性
 
<profiles>
    <profile>
        <id>dev_env</id>
        <properties>
            <profile.active>dev</profile.active>
        </properties>
        <activation>
            <activeByDefault>true</activeByDefault>
        </activation>
    </profile>
    <profile>
        <id>pro_env</id>
        <properties>
            <profile.active>pro</profile.active>
        </properties>
    </profile>
    <profile>
        <id>test_env</id>
        <properties>
            <profile.active>test</profile.active>
        </properties>
    </profile>
</profiles>
 
- SpringBoot中引入Maven属性
 

- 执行Maven打包指令
 
- Maven指令执行完毕后,生成了对应的包,其中类参与编译,但是配置文件并没有编译,而是复制到包中
 

- 解决思路:对于源码中非java类的操作要求加载Maven对应的属性,解析${}占位符
 
- 对资源文件开启对默认占位符的解析
 
<build>
    <plugins>
        <plugin>
            <artifactId>maven-resources-plugin</artifactId>
            <configuration>
                <encoding>utf-8</encoding>
                <useDefaultDelimiters>true</useDefaultDelimiters>
            </configuration>
        </plugin>
    </plugins>
</build>
 
- Maven打包加载属性,打包顺利通过
 

 
配置文件分类

- SpringBoot中4级配置文件 
  
- 1级: file :config/application.yml 【最高】
 - 2级: file :application.yml
 - 3级:classpath:config/application.yml
 - 4级:classpath:application.yml 【最低】
 
 - 作用: 
  
- 1级与2级留做系统打包后设置通用属性
 - 3级与4级用于系统开发阶段设置通用属性
 
 
整合第三方技术
整合JUnit
Spring整合JUnit

 
SpringBoot整合JUnit
- 添加整合Junit起步依赖(可以直接勾选)
 
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-test</artifactId>
    <scope>test</scope>
</dependency>
 
- 编写测试类,默认自动生成了一个
 
@SpringBootTest
class Springboot07JunitApplicationTests {
    @Autowired
    private BookService bookService;
    @Test
    public void testSave() {
        bookService.save();
    }
}
 
 
基于SpringBoot实现SSM整合
- SpringBoot整合Spring(不存在)
 - SpringBoot整合SpringMVC(不存在)
 - SpringBoot整合MyBatis(主要)
 
- 创建新模块,选择Spring初始化,并配置模块相关基础信息
 

- 选择当前模块需要使用的技术集(Mybatis,MySQL)
 

- 设置数据源参数
 
spring:
  datasource:
    driver-class-name: com.mysql.cj.jdbc.Driver
    url: jdbc:mysql://localhost:3306/ssm_db?serverTimezone=UTC
    username: root
    password: root
    type: com.alibaba.druid.pool.DruidDataSource
 
注意事项:
SpringBoot版本低于2.4.3(不含),Mysql驱动版本大于8.0时,需要在url连接串中配置时区,或在MySQL数据库端配置时区解决此问题
jdbc:mysql://localhost:3306/ssm_db?serverTimezone=UTC
 
- 定义数据层接口与映射配置
 
@Mapper
public interface UserDao {
    @Select("select * from tbl_book where id=#{id}")
    Book getById(Integer id);
}
 
- 测试类中注入dao接口,测试功能
 
@SpringBootTest
class Springboot08MybatisApplicationTests {
    @Autowired
    private BookDao bookDao;
    @Test
    public void testGetById() {
        Book book = bookDao.getById(1);
        System.out.println(book);
    }
}
 
 
SpringBoot实现SSM整合
- 创建SpringBoot工程,将三个依赖选中,添加druid依赖
 
<dependency>
    <groupId>com.alibaba</groupId>
    <artifactId>druid</artifactId>
    <version>1.2.6</version>
</dependency>
 
- 复制ssm_demo中的各种资源
 - 删除config包中的所有配置,在BookDao接口上加@Mapper注解
 - 将application.properties修改成application.yml,配置端口号和连接参数
 
server:
  port: 80
# todo 4 配置数据库连接参数
spring:
  datasource:
    driver-class-name: com.mysql.cj.jdbc.Driver
    url: jdbc:mysql://localhost:3306/ssm_db
    username: root
    password: root
    type: com.alibaba.druid.pool.DruidDataSource
 
- 修改BookServiceTest配置类,进行配置
 - 在static目录中提供index.html页面,跳转到"pages/books.html"
 
<script>
  location.href="pages/books.html"
</script>
 
最后:运行引导类即可访问 localhost










