目录
【yaml】—— YAML Ain't Markup Language
【概述】
SpringBoot是由Pivotal团队提供的全新框架,其设计目的是用来简化Spring应用的初始搭建以及开发过程
【优点】
1、创建新模块,选择Spring初始化,并配置模块相关基础信息(根据需求改)

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

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

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

1、对SpringBoot项目打包(执行Maven构建指令package)
2、执行启动指令:java -jar spring.jar(jar包名)
【注意】
1、jar支持命令行启动需要依赖maven插件支持,请确认打包时是否具有SpringBoot对应的maven插件
- <build>
- <plugins>
- <plugin>
- <groupId>org.springframework.bootgroupId>
- <artifactId>spring-boot-maven-pluginartifactId>
- plugin>
- plugins>
- build>
2、打jar包前注意:

1、使用maven依赖管理变更起步依赖项
- <dependencies>
- <dependency>
- <groupId>org.springframework.bootgroupId>
- <artifactId>spring-boot-starter-webartifactId>
-
- <exclusions>
- <exclusion>
- <groupId>org.springframework.bootgroupId>
- <artifactId>spring-boot-starter-tomcatartifactId>
- exclusion>
- exclusions>
- dependency>
-
- <dependency>
- <groupId>org.springframework.bootgroupId>
- <artifactId>spring-boot-starter-jettyartifactId>
- dependency>
- dependencies>
【注意】
Jetty比Tomcat更轻量级,可扩展性更强(相较于Tomcat),谷歌应用引擎(GAE)已经全面切换为Jetty
【配置格式】
server.port=80
- server:
- port: 81
- server:
- port: 82
【注意】
1、自动提示功能消失解决方案

2、SpringBoot配置文件加载顺序
application.properties > application.yml > application.yaml
3、主要使用application.yml
4、SpringBoot核心配置文件名为application
5、SpringBoot内置属性过多,且所有属性集中在一起修改,在使用时,通过提示键+关键字修改属性
【概述】
一种数据序列化格式
【优点】
【YAML文件扩展名】
【语法规则】
【yaml数组数据】
数组数据在数据书写位置的下方使用减号作为数据开始符号,每行书写一个数据,减号与数据间空格分隔
例:
- enterprise:
- name: itcast
- age: 16
- tel: 4006184000
- subject:
- - Java
- - 前端
- - 大数据
【yaml数据读取】
【数据】
- lesson: SpringBoot
-
- server:
- port: 8080
-
- enterprise:
- name: zhangsan
- age: 15
- tel: 12345678910
- subject:
- - java
- - web
- - 大数据
- @RestController
- @RequestMapping("/books")
- public class BookController {
-
- @Value("${lesson}")
- private String lessonName;
-
- @Value("${server.port}")
- private int port;
-
- @Value("${enterprise.subject[1]}")
- private String[] subject_01;
- }
- @RestController
- @RequestMapping("/books")
- public class BookController {
- @Autowired
- private Environment env;
- @GetMapping("/{id}")
- public String getById(@PathVariable Integer id){
- System.out.println(env.getProperty("lesson"));
- System.out.println(env.getProperty("enterprise.name"));
- System.out.println(env.getProperty("enterprise.subject[0]"));
- return "hello , spring boot!";
- }
- }
1、定义一个对象(属性与数据名称一致)
- @Component
- @ConfigurationProperties(prefix = "enterprise")
- public class Enterprise {
- private String name;
- private Integer age;
- private String tel;
- private String[] subject;
- }
2、引用对象
- @RestController
- @RequestMapping("/books")
- public class BookController {
- @Autowired
- private Enterprise enterprise;
- }
【注意】
自定义对象封装数据警告解决方案
- <dependency>
- <groupId>org.springframework.bootgroupId>
- <artifactId>spring-boot-configuration-processorartifactId>
- <optional>trueoptional>
- dependency>
- #设置启用环境
- spring:
- profiles:
- active: dev
- ---
- #开发
- server:
- port: 8080 #开发环境具体参数指定
- spring:
- config:
- activate:
- on-profile: dev #设置开发环境
- ---
- #生产
- server:
- port: 8081 #生产环境具体参数指定
- spring:
- config:
- activate:
- on-profile: pro #设置生产环境
- ---
- #测试
- server:
- port: 8082 #测试环境具体参数指定
- spring:
- config:
- activate:
- on-profile: test #设置测试环境
spring.profiles.active=pro
server.port=80
server.port=81
server.port=82
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
【参数加载优先顺序】
SpringBoot听从Maven的设定
1、对资源文件开启对默认占位符的解析
- <build>
- <plugins>
- <plugin>
- <groupId>org.apache.maven.pluginsgroupId>
- <artifactId>maven-resources-pluginartifactId>
- <version>3.2.0version>
- <configuration>
- <encoding>utf-8encoding>
- <useDefaultDelimiters>trueuseDefaultDelimiters>
- configuration>
- plugin>
- plugins>
- build>
2、Maven中设置多环境属性
- <profiles>
- <profile>
- <id>dev_envid>
- <properties>
- <profile.active>devprofile.active>
- properties>
- <activation>
- <activeByDefault>trueactiveByDefault>
- activation>
- profile>
- <profile>
- <id>pro_envid>
- <properties>
- <profile.active>proprofile.active>
- properties>
- profile>
- <profile>
- <id>test_envid>
- <properties>
- <profile.active>testprofile.active>
- properties>
- profile>
- profiles>
3、SpringBoot中引用Maven属性
- #设置启用环境
- spring:
- profiles:
- active: ${profile.active}
- ---
- #开发
- server:
- port: 8080
- spring:
- config:
- activate:
- on-profile: dev
- ---
- #生产
- server:
- port: 8081
- spring:
- config:
- activate:
- on-profile: pro
- ---
- #测试
- server:
- port: 8082
- spring:
- config:
- activate:
- on-profile: test
4、执行Maven打包指令
SpringBoot中4级配置文件
【作用】
1级与2级留做系统打包后设置通用属性
3级与4级用于系统开发阶段设置通用属性
- @SpringBootTest
- class SpringBoot04TestApplicationTests {
- @Autowired
- private BookService bookService;
- @Test
- void contextLoads() {
- bookService.save();
- }
- }
【@SpringBootTest】
例:
- @SpringBootTest(classes = Springboot07JunitApplication.class)
- class Springboot07JunitApplicationTests {}
【注意】
如果测试类在SpringBoot启动类的包或子包中,可以省略启动类的设置,也就是省略classes的设定
【流程】
【步骤】
1、创建新模块,选择Spring初始化,并配置模块相关基础信息

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

3、设置数据源参数
- spring:
- datasource:
- driver-class-name: com.mysql.cj.jdbc.Driver
- url: jdbc:mysql://localhost:3306/db1
- username: root
- passwords: root
- type: com.alibaba.druid.pool.DruidDataSource
【注意】
SpringBoot版本低于2.4.3(不含),Mysql驱动版本大于8.0时,需要在url连接串中配置时区
jdbc:mysql://localhost:3306/db1?serverTimezone=UTC
4、定义数据层接口与配置
- @Mapper
- public interface BookDao {
- @Select("select * from tb_book where id = #{id}")
- public Book getById(Integer Id);
- }
5、测试类中注入dao接口,测试功能
- @SpringBootTest
- class SpringBoot05MybatisApplicationTests {
- @Autowired
- private BookDao bookDao;
- @Test
- void testById() {
- Book book=bookDao.getById(1);
- System.out.println(book);
- }
- }
【注意】
在SpringBoot的服务器中,访问HTML需要增加一个坐标
- <dependency>
- <groupId>org.springframework.bootgroupId>
- <artifactId>spring-boot-starter-thymeleafartifactId>
- dependency>