文章目录
- 配置文件
- properties
- Spring提供的配置文件类型及其优先级
- yaml
- 基本语法
- 数据读取
- 整合第三方技术
- JUnit
- 整合MyBatis
- 整合MyBatis-Plus
- 整合Druid
配置文件
properties
熟悉的配方,熟悉的味道,大家再熟悉不过的格式(key=value)。是SpringBoot默认的配置文件:application.properties
server.port=80
spring.main.banner-mode=off
logging.level.root=debug
Spring内置属性查询:
注:只有在SpringBoot中导入对应的starter后,配置文件中才提供对应配置属性
Spring提供的配置文件类型及其优先级
SpringBoot提供了3种配置文件格式:
- properties(默认格式)
- yml(主流格式)
- yaml
yml和yaml其实是一个东西,只不过文件扩展名不一样。
这三种配置文件是可以共存的,其文件加载顺序如下:
application.properties > application.yml > application.yaml
不同配置文件中相同配置会按照加载优先级相互覆盖,不同配置文件中的不同配置则全部保留。
yaml
今天的角儿叫yaml,介绍一种新的配置文件类型。
YAML 是 “YAML Ain’t a Markup Language”(YAML 不是一种标记语言)的递归缩写。在开发的这种语言时,YAML 的意思其实是:“Yet Another Markup Language”(仍是一种标记语言)。
非常适合用来做以数据为中心的配置文件。
基本语法
- key:value;kv之间有空格。
- 大小写敏感
- 使用缩写表示层级关系
- 缩进不允许使用tab,只允许空格
- 缩进的空格数不重要,只要相同层级的元素左对齐即可
- '#'表示注释
- 字符串无需加引号,如果要加,单引号’’、双引号""表示字符串内容会被 转义、不转义
字面值表示方式
boolean: TRUE # TRUE、true、True、false、False均可
float: 3.14 # 支持科学计数法 6.8534e+5
int: 123 # 支持二进制 八进制 十六进制
null: ~ # 使用~表示null
string: HelloWorld
date: 2022-03-31 # 日期必须使用yyyy-MM-dd格式
datetime: 2022-03-31T08:41:33+08:00 # 日期和时间之间使用T连接,最后使用+代表时区
数组表示方式:在属性名书写位置下方使用-作为数据开始符号,每书写一个数据,减号与数据间空格分隔。
subject:
- Java
- 前端
- 大数据
enterprise:
name: itcast
age: 16
tel: 4006184000
subject:
- Java
- 前端
- 大数据
# subject: [Java,前端,大数据]
users:
- name: Tom
age: 4
- name: Jerry
age: 5
users:
-
name: Tom
age: 4
-
name: Jerry
age:5
users: [{ name: Tom,age: 4},{name: Jerry, age: 5}]
数据读取
使用@Value配合EL表达式读取单个数据,属性名引用方式:${一级属性名.二级属性名……}
如果有多个层级,依次书写层级名称即可。
@RestController
@RequestMapping("/books")
public class BookController {
@Value("${city}")
private String city;
@Value("${company}")
private String company;
@Value("${enterprise.subject[1]}")
private String subject;
在配置文件中可以使用${属性名}方式引用属性值
baseDir: /usr/local/bin
center:
dataDir: ${baseDir}/data
tmpDir: ${baseDir}/tmp
logDir: ${baseDir}/log
属性值中如果出现转义字符,需要使用双引号进行包裹
lesson: "Spring\tboot\nlesson"
还可以将配置文件中的全部数据封装到Environment对象
package com.itheima.controller;
import com.itheima.bean.Person;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* @version 1.0
* @Description
* @Author 月上叁竿
* @Date 2022-03-30 7:52
**/
@RestController
@RequestMapping("/books")
public class BookController {
@Autowired
private Environment env;
@GetMapping
public String getById(){
System.out.println(env.getProperty("enterprise.name"));
System.out.println(env.getProperty("enterprise.subject[0]"));
return "spring boot is running";
}
}
自定义对象封装指定数据
- 使用@ConfigurationProperties注解绑定配置信息到封装类中。
- 封装类需要定义为Spring管理的bean,否则无法进行属性注入。
自定义类:
// 定义数据模型封装yaml文件中对应的数据
// 定义为Spring管控的bean
@Component
// 指定加载的数据
@ConfigurationProperties(prefix = "enterprise")
public class Enterprise{
private String name;
private Integer age;
private String[] subject;
@RestController
@RequestMapping("/books")
public class BookController {
@Autowired
private Enterprise enterprise;
整合第三方技术
JUnit
SpringBoot整合JUnit:
引入场景启动器:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
测试类
package com.itheima;
import com.itheima.service.BookService;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
@SpringBootTest
class Springboot02MybatisApplicationTests {
@Autowired
private BookService bookService;
@Test
void contextLoads() {
bookService.save();
}
}
整合步骤:
- 导入测试对应的starter
- 测试类使用@SpringBootTest修饰
- 使用自动装配的形式添加要测试的对象
测试类不存在于引导类所在的包或子包的情况
只需设置@SpringBootTest注解的classes属性为SpringBoot启动类
@SpringBootTest(classes = Springboot02MybatisApplication.class)
class Springboot02MybatisApplicationTests {
测试类如果存在于引导类所在的包或子包中无需指定引导类。
整合MyBatis
步骤一:创建新模块,选择Spring初始化,并配置模块相关基础信息,选择当前模块需要使用的技术集(MyBatis、MySQL)
步骤二:设置数据源参数
spring:
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://localhost:3306/bjpowernode
username: root
password: 123456
步骤三: 定义数据层接口与映射配置
在注解开发时,可以不用加@Mapper注解,但数据库SQL映射需要添加@Mapper被容器识别到。
@Repository
@Mapper
public interface EmpDao {
@Select("select * from t_emp")
public List<Emp> getAll();
}
步骤四:测试类中注入dao接口,测试功能
@Test
public void testGetAll(){
List<Emp> empList = empDao.getAll();
empList.forEach(System.out::println);
}
整合MyBatis-Plus
手动添加SpringBoot整个MyBatis-Plus的坐标
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-generator</artifactId>
<version>3.5.1</version>
</dependency>
定义数据层接口与映射配置,继承BaseMapper
@Mapper
public interface UserMapper extends BaseMapper<User> {
}
其他配置同MyBatis,也可以移步看我写的MyBatis-Plus文章。
整合Druid
指定数据源类型
spring:
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://localhost:3306/
username: root
password: 123456
type: com.alibaba.druid.pool.DruidDataSource
引入场景启动器
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
<version>1.2.6</version>
</dependency>
变更后的数据源配置
spring:
datasource:
druid:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://localhost:3306/ssm_db?serverTimezone=UTC
username: root
password: 123456