SSM学习笔记_SpringBoot

1. SpringBoot简介

1.1 入门案例

  1. 创建新模块,选择Spring初始化(Spring Initializr),并配置模块相关基础信息;
  2. 选择当前模块需要使用的技术集;
  3. 开发控制器类
  4. 运行自动生成的Application类
    SpringBoot运行信息
  • Spring程序与SpringBoot程序对比

    类/配置文件SpringSpringBoot
    pom文件中的坐标手工制作勾选添加
    web3.0配置类手工制作
    Spring/SpringMVC配置类手工制作
    控制器手工制作手工制作

    注:基于idea开发SpringBoot程序需要确保联网能够加载到程序框架结构

  • 特殊情况下可通过Spring官网的Spring Initializr快速构建项目

1.2 SpringBoot项目快速启动

  1. 对SpringBoot项目打包(执行maven构建指令package

  2. 执行启动指令

    java -jar springboot.jar
    

    注:jar支持命令行启动需要依赖maven插件支持,因此打包前需确认是否具有SpringBoot对应的maven插件

    <build>
    	<plugins>
    		<plugin>
    			<groupId>org.springframework.boot</groupId>
    			<artifactId>spring-boot-maven-plugin</artifactId>
    		</plugin>
    	</plugins>
    </build>
    

1.3 SpringBoot概述

  • Spring程序缺点
    • 配置繁琐
    • 依赖设置繁琐
  • SpringBoot程序优点
    • 自动配置
    • 起步依赖(简化依赖配置)
    • 辅助功能(内置Tomcat服务器…)

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(要小心版本冲突)

SpringBoot程序启动

  • 启动方式
    @SpringBootApplication
    public class MySpringbootApplication {
        public static void main(String[] args) {
            SpringApplication.run(MySpringbootApplication.class, args);
        }
    }
    
  • SpringBoot在创建项目时,采用jar的打包方式
  • SpringBoot的引导类是项目的入口,运行main方法就可以启动项目
  • 此外,还可以使用maven依赖管理变更起步依赖项(Jetty比Tomcat更轻量级,可拓展性更强)
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
            <!-- 剔除Tomcat的起步依赖 -->
            <exclusions>
                <exclusion>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-starter-tomcat</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        
        <!-- 增加jetty的起步依赖 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-jetty</artifactId>
        </dependency>
    </dependencies>
    

2. 基础配置

2.1 配置文件格式

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内置属性过多,且所有属性集中到一起修改,在使用时可通过提示+关键词修改属性

2.2 yaml

2.2.1 yaml格式

  • YAML(YAML Ain’t Markup Language),一种数据序列化格式
  • 优点
    • 容易阅读
    • 容易与脚本语言交互
    • 以数据为核心,重数据轻格式
  • YAML文件扩展名
    • .yml(主流)
    • .yaml

yaml语法规则

  • 大小写敏感
  • 属性层级关系使用多行描述,每行结尾使用冒号结束
  • 使用缩进表示层级关系,同层级左侧对齐,只允许使用空格(不允许使用Tab键)
  • 属性值前面添加空格(属性名与属性值之间使用冒号+空格作为分隔)
  • #表示注释

yaml数组数据

  • 数组数据在数据书写位置的下方使用减号作为数据开始符号,每行书写一个数据,减号与数据间空格分隔
    subject:
      - Java
      - 前端
      - 大数据
    

2.3 yaml配置文件数据读取(三种)

  • 使用@Value读取单个数据,属性名引用方式:${一级属性名.二级属性名…}
    yaml读取数据方式一
  • 封装全部数据到Environment对象
    yaml读取数据方式二
  • 自定义对象封装指定数据
    yaml读取数据方式三

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

<dependency>
	<groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-configuration-processor</artifactId>
	<optional>true</optional>
</dependency>

2.4 多环境配置

2.4.1 yaml文件配置

application.yml中使用 — 来分割不同的配置,内容如下

#启用的配置
spring:
  profiles:
    active: dev  #表示使用的是开发环境的配置
---
#开发
spring:
  profiles: dev
server:
  port: 80
---
#生产
spring:
  profiles: pro
server:
  port: 81
---
#测试
spring:
  profiles: test
server:
  port: 82

注:上述配置中的spring.profiles 配置项已经过时,最新用来起名字的配置项是

#开发
spring:
  config:
    activate:
      on-profile: dev

2.4.2 properties文件

properties类型的配置文件配置多环境需要定义不同的配置文件

  • application-dev.properties是开发环境的配置文件
    server.port=80
    
  • application-test.properties是测试环境的配置文件
    server.port=81
    
  • application-pro.properties是生产环境的配置文件
    server.port=82
    

2.4.3 多环境启动命令格式

  • 带参数启动SpringBoot
    // SpringBoot 提供了在运行 jar 时设置开启指定的环境的方式
    java –jar xxx.jar –-spring.profiles.active=test
    
    // 指定端口号
    java –jar xxx.jar –-server.port=88
    
    // 同时设置多个配置,比如即指定启用哪个环境配置,又临时指定端口
    java –jar springboot.jar –-server.port=88-spring.profiles.active=test	
    

注:在执行package命令前执行一次clean命令,防止之前执行结果影响打包;
文件编码问题可能导致打包失败,在setting->Editor->File Encoding中将Project Encoding和Default encoding for properties files改为utf-8即可

参数加载优先顺序

  参考官网文档

参数加载顺序

2.4.4 多环境开发兼容性问题(maven和boot兼容)

  1. Maven中设置多环境配置

    <profiles>
    	<!--开发环境-->
    	<profile>
    		<id>dev</id>
    		<properties>
    			<profile.active>dev</profile.active>
    		</properties>
    	</profile>
    	
    	<!--生产环境-->
    	<profile>
    		<id>pro</id>
    		<properties>
    			<profile.active>pro</profile.active>
    		</properties>
    		<activation>
    			<activeByDefault>true</activeByDefault>
    		</activation>
    	</profile>
    	
    	<!--测试环境-->
    	<profile>
    		<id>test</id>
    		<properties>
    			<profile.active>test</profile.active>
    		</properties>
    	</profile>
    </profiles>
    
  2. SpringBoot中也设置了多环境,要使他们兼容就得让配置文件读取maven配置的多环境,设置完成后就会加载profile.active这个属性了。

    #设置启用的环境
    spring:
      profiles:
        active: ${profile.active}
    ---
    #开发
    spring:
      profiles: dev
    server:
      port: 80
    ---
    #生产
    spring:
      profiles: pro
    server:
      port: 81
    ---
    #测试
    spring:
      profiles: test
    server:
      port: 82
    
  3. 执行maven打包命令。但这样直接打包会失败,因为Maven执行完毕后,其中类参与编译,但是配置文件没有编译,而是复制到包中;解决方法:对于源码中非java类的操作要求加载Maven对应的属性,解析${}占位符。

  4. 对资源文件开启对默认占位符的解析

<plugin>
	<groupId>org.apache.maven.plugins</groupId>
	<artifactId>maven-resources-plugin</artifactId>
	<version>3.2.0</version>
	<configuration>
		<encoding>UTF-8</encoding>
		<useDefaultDelimiters>true</useDefaultDelimiters>
	</configuration>
</plugin>

2.4 配置文件分类

  • SpringBoot中4级配置文件
    • 1级:file:config/application.yml 【最高】(资源管理器中)
    • 2级:file:application.yml
    • 3级:classpath:config/application.yml
    • 4级:classpath:application.yml 【最低】(项目中)
  • 作用:
    • 1级与2级留作系统打包后设置通用属性
    • 3级与4级用于系统开发阶段设置通用属性

3. 整合第三方技术

3.1 SpringBoot整合junit

  • 在test/java下创建包以及测试类,将BookService注入到该测试类中
@SpringBootTest   // 第一步,加上该注解
class Springboot07TestApplicationTests {
    @Autowired   // 第二步,将要测试的bean自动装配
    private BookService bookService;

    @Test   // 第三步,测试
    public void save() {
        bookService.save();
    }
}
名称@SpringBootTest
类型测试类注解
位置测试类定义上方
作用设置JUnit加载的SpringBoot启动类
相关属性classes:设置SpringBoot启动类

注:如果测试类在SpringBoot启动类的包或子包中,可以忽略启动类的设置,也就是忽略classes的设定

3.2 SpringBoot整合mybatis

  1. 创建新模块,选择spring初始化,并配置模块相关基础信息

  2. 选择当前模块需要的技术集(MyBatis、MySQL)

  3. 在定义好实体类,dao接口后定义测试类

    @SpringBootTest
    class Springboot08MybatisApplicationTests {
    	@Autowired
    	private BookDao bookDao;
    
    	@Test
    	void testGetById() {
    		Book book = bookDao.getById(1);
    		System.out.println(book);
    	}
    }
    
  4. 设置数据源参数

    spring:
      datasource:
        driver-class-name: com.mysql.jdbc.Driver
        url: jdbc:mysql://localhost:3306/ssm_db?serverTimezone=UTC
        username: root
        password: root
    

    :SpringBoot版本低于2.4.3(不含)且MySQL驱动版本大于8.0时,需要在url连接串中配置时区

    jdbc:mysql://localhost:3306/ssm_db?serverTimezone=UTC
    

    或在MySQL数据库端配置时区解决此问题

  5. 给dao接口加上@Mapper注解(Mybatis会扫描接口并创建接口的代码对象交给Spring管理,但是现在并没有告诉Mybatis哪个是dao接口。而我们要解决这个问题需要在Dao接口上使用@Mapper

    @Mapper
    public interface BookDao {
        @Select("select * from tbl_book where id = #{id}")
        public Book getById(Integer id);
    }
    
  6. 使用Druid数据源(提前导入Druid依赖)

    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
    
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值