目录
【yaml】—— YAML Ain't Markup Language
【SpringBoot】
【概述】
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.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
2、打jar包前注意:
- package之前进行一次clean,防止之前的缓存影响之后的结果
- 更改语言环境为UTF-8,防止配置文件中的中文出现乱码
【辅助功能——切换服务器】
1、使用maven依赖管理变更起步依赖项
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<!--web起步依赖环境中,排除Tomcat起步依赖-->
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
</exclusion>
</exclusions>
</dependency>
<!--添加Jetty起步依赖,版本由SpringBoot的starter控制-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jetty</artifactId>
</dependency>
</dependencies>
【注意】
Jetty比Tomcat更轻量级,可扩展性更强(相较于Tomcat),谷歌应用引擎(GAE)已经全面切换为Jetty
【基础配置】
【配置格式】
- application.properties
server.port=80
- application.yml
server:
port: 81
- application.yaml
server:
port: 82
【注意】
1、自动提示功能消失解决方案
2、SpringBoot配置文件加载顺序
application.properties > application.yml > application.yaml
3、主要使用application.yml
4、SpringBoot核心配置文件名为application
5、SpringBoot内置属性过多,且所有属性集中在一起修改,在使用时,通过提示键+关键字修改属性
【yaml】—— YAML Ain't Markup Language
【概述】
一种数据序列化格式
【优点】
- 容易阅读
- 容易与脚本语言交互
- 以数据为核心,重数据轻格式
【YAML文件扩展名】
- .yml(主流)
- .yaml
【语法规则】
- 核心:数据前面要加空格与冒号隔开
- 大小写敏感
- 属性层级关系使用多行描述,每行结尾使用冒号结束
- 使用缩进表示层级关系,同层级左侧对齐,只允许使用空格(不允许使用Tab键)
- 属性值前面添加空格(属性名与属性值之间使用冒号+空格作为分隔)
- # 表示注释
【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
- 大数据
- 使用@Value读取单个数据,属性名引用方式${一级属性名.二级属性名……}
@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;
}
- 读取多个数据,封装全部数据到Environment对象
@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.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
【多环境开发】
【yaml文件多环境启动】
#设置启用环境
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 #设置测试环境
【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
【多环境启动命令格式】
- 带参数启动Spring
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多环境兼容】
SpringBoot听从Maven的设定
1、对资源文件开启对默认占位符的解析
<build>
<plugins>
<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>
</plugins>
</build>
2、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>
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级: file :config/application.yml 【最高】
- 2级: file :application.yml
- 3级:classpath:config/application.yml
- 4级:classpath:application.yml 【最低】
【作用】
1级与2级留做系统打包后设置通用属性
3级与4级用于系统开发阶段设置通用属性
【SpringBoot整合JUnit】
@SpringBootTest
class SpringBoot04TestApplicationTests {
@Autowired
private BookService bookService;
@Test
void contextLoads() {
bookService.save();
}
}
【@SpringBootTest】
- 名称:@SpringBootTest
- 类型:测试类注解
- 作用:设置JUnit加载的SpringBoot启动类
- 属性:classes:设置SpringBoot启动类
例:
@SpringBootTest(classes = Springboot07JunitApplication.class)
class Springboot07JunitApplicationTests {}
【注意】
如果测试类在SpringBoot启动类的包或子包中,可以省略启动类的设置,也就是省略classes的设定
【SpringBoot实现SSM整合】
【SpringBoot整合MyBatis】
【流程】
- pom.xml:配置起步依赖,必要的资源坐标(druid)
- application.yml:设置数据源、端口等
- 配置类:全部删除
- dao:设置@Mapper
- 测试类
- 页面:放置在resources目录下的static目录中
【步骤】
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.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>