目录
- [SpringBoot 简介](#SpringBoot 简介)
 - 配置文件
 - SpringBoot整合junit
 - 
- [回顾 `Spring` 整合 `junit`](#回顾 
Spring整合junit) 
 - [回顾 `Spring` 整合 `junit`](#回顾 
 - [SpringBoot 整合 Mybatis](#SpringBoot 整合 Mybatis)
 - [SpringBoot 整合 SSM](#SpringBoot 整合 SSM)
 
SpringBoot 简介
- 
用来简化Spring应用的初始搭建以及开发过程。
 - 
原始
Spring环境搭建和开发存在以下问题:- 配置繁琐
 - 依赖设置繁琐
 
 - 
SpringBoot程序优点恰巧就是针对Spring的缺点- 自动配置。这个是用来解决 
Spring程序配置繁琐的问题 - 起步依赖。这个是用来解决 
Spring程序依赖设置繁琐的问题 - 辅助功能(内置服务器,...)。我们在启动 
SpringBoot程序时既没有使用本地的tomcat也没有使用tomcat插件,而是使用SpringBoot内置的服务器。 
 - 自动配置。这个是用来解决 
 - 
starter
SpringBoot中常见项目名称,定义了当前项目使用的所有项目坐标,以达到减少依赖配置的目的
 - 
parent
- 
所有
SpringBoot项目要继承的项目,定义了若干个坐标版本号(依赖管理,而非依赖),以达到减少依赖冲突的目的 - 
spring-boot-starter-parent(2.5.0)与spring-boot-starter-parent(2.4.6)共计57处坐标版本不同 
 - 
 - 
引导类
 
            
            
              java
              
              
            
          
          @SpringBootApplication
public class Springboot01QuickstartApplication {
    
    public static void main(String[] args) {
        SpringApplication.run(Springboot01QuickstartApplication.class, args);
    }
}
        * `SpringBoot` 在创建项目时,采用jar的打包方式
* `SpringBoot` 的引导类是项目的入口,运行 `main` 方法就可以启动项目
 因为我们在 `pom.xml` 中配置了 `spring-boot-starter-web` 依赖,而该依赖通过前面的学习知道它依赖 `tomcat` ,所以运行 `main` 方法就可以使用 `tomcat` 启动咱们的工程。
        配置文件
配置文件格式
SpringBoot提供了多种属性配置方式
application.properties
            
            
              yaml
              
              
            
          
          server.port=80
        application.yml
            
            
              yaml
              
              
            
          
          server:
	port: 81
        application.yaml
            
            
              yaml
              
              
            
          
          server:
	port: 82
        - 
SpringBoot程序的配置文件名必须是application,只是后缀名不同而已。 - 
三种配置文件的优先级是:
application.properties>application.yml>application.yaml 
yaml格式
- YAML(YAML Ain't Markup Language),一种数据序列化格式
 
优点:
- 
容易阅读
yaml类型的配置文件比xml类型的配置文件更容易阅读,结构更加清晰 - 
容易与脚本语言交互
 - 
以数据为核心,重数据轻格式
yaml更注重数据,而xml更注重格式 
YAML 文件扩展名:
.yml(主流).yaml
上面两种后缀名都可以,以后使用更多的还是 yml 的。
命令行启动参数设置
            
            
              yaml
              
              
            
          
          java --jar xxx.jar ---spring.profiles.active=test
        - 配置优先级

 
配置文件分类
SpringBoot 定义了配置文件不同的放置的位置;而放在不同位置的优先级时不同的。
SpringBoot 中4级配置文件放置位置:
- 1级:classpath:application.yml
 - 2级:classpath:config/application.yml
 - 3级:file :application.yml
 - 4级:file :config/application.yml
 
说明 : 级别越高优先级越高
SpringBoot整合junit
回顾 Spring 整合 junit
        
            
            
              java
              
              
            
          
          @RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = SpringConfig.class)
public class UserServiceTest {
    
    @Autowired
    private BookService bookService;
    
    @Test
    public void testSave(){
        bookService.save();
    }
}
        使用 @RunWith 注解指定运行器,使用 @ContextConfiguration 注解来指定配置类或者配置文件。而 SpringBoot 整合 junit 特别简单,分为以下三步完成
- 在测试类上添加 
SpringBootTest注解 - 使用 
@Autowired注入要测试的资源 - 定义测试方法进行测试
 
SpringBoot 整合 Mybatis
- Spring Boot推崇 " 约定大于配置 "
 
- 定义实体类
 - 定义dao接口
Mybatis会扫描接口并创建接口的代码对象交给Spring管理,但是现在并没有告诉Mybatis哪个是dao接口。而我们要解决这个问题需要在BookDao接口上使用@Mapper 
            
            
              java
              
              
            
          
          @Mapper
public interface BookDao {
    @Select("select * from tbl_book where id = #{id}")
    public Book getById(Integer id);
}
        - 定义测试类
 - 编写配置
 
            
            
              java
              
              
            
          
          spring:
  datasource:
    driver-class-name: com.mysql.jdbc.Driver
    url: jdbc:mysql://localhost:3306/ssm_db
    username: root
    password: root
        - 使用Druid数据源
 
- 导入依赖
 
            
            
              java
              
              
            
          
          <dependency>
    <groupId>com.alibaba</groupId>
    <artifactId>druid</artifactId>
    <version>1.1.16</version>
</dependency>
        - 
在
application.yml配置文件配置 - 
可以通过
spring.datasource.type来配置使用什么数据源。配置文件内容可以改进为 
            
            
              java
              
              
            
          
          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 整合 SSM
- 
pom.xml
配置起步依赖,必要的资源坐标(druid)
 - 
application.yml
设置数据源、端口等
 - 
配置类
全部删除
 - 
dao
设置@Mapper
 - 
测试类
 - 
页面
放置在resources目录下的static目录中