YAML基本语法
1. 基本要求
大小写敏感
数据值前必须有空格作为分隔符
使用缩进表示层级关系
缩进不允许使用Tab键,只允许使用空格
#表示注释
2. 数据格式
对象(map):键值对集合
数组:一组按次序排序的值
1 2 3
| address: -beijing -shanghai
|
纯量:单个不可再分的值
1 2
| msg1: 'hello \n world' msg2: "hello \n world"
|
3. 参数引用
1 2 3
| name: lisi person: name: ${name}#引用上面定义的name值
|
4. Springboot配置
成员变量的名称与配置文件里的名称可不同,但配置文件键的名称要与表达式中的一样
5.注入方式
1)通过@Value
1 2
| @Value("${name1}") private String name1;
|
2)通过@Autowired
通过env.getProperty来访问
1 2
| @Autowired private Environment env;
|
注入的快捷键Fn+Alt+Insert
3)@ConfigurationProperites可以用来代替@Value
@ConfigurationProperties 的基本用法非常简单:我们为每个要捕获的外部属性提供一个带有字段的类。请注意以下几点:
- 前缀定义了哪些外部属性将绑定到类的字段上
- 根据 Spring Boot 宽松的绑定规则,类的属性名称必须与外部属性的名称匹配
- 我们可以简单地用一个值初始化一个字段来定义一个默认值
- 类本身可以是包私有的
- 类的字段必须有公共 setter 方法
Person
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39
| @Component @ConfigurationProperties(prefix = "person") public class Person { private String name; private int age; private String[] address ;
public String getName() { return name; }
public void setName(String name) { this.name = name; }
public int getAge() { return age; }
public void setAge(int age) { this.age = age; }
public String[] getAddress() { return address; }
public void setAddress(String[] address) { this.address = address; }
@Override public String toString() { return "Person{" + "name='" + name + '\'' + ", age=" + age + '}'; } }
|
HelloController
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43
| @RestController public class HelloController { @Value("${name}") private String name;
@Value("${person.name}") private String name2;
@Value("${address[0]}") private String address0;
//注入对象 @Autowired private Environment env;
@Autowired private Person person;
@RequestMapping("/hello2") public String hello2(){ System.out.println(name); System.out.println(name2); System.out.println(address0); System.out.println("----------------------"); //第二种注入方式,只需要注入一个对象 System.out.println(env.getProperty("person.name")); System.out.println(person); System.out.println("----------------------"); String[] address = person.getAddress(); for (String s : address) { System.out.println(s); }
return "hello World"; }
@RequestMapping("/hello") public String hello(){ return "hello World"; } }
|
6. profile
功能:在开发Spring boot应用时,一套程序会被安装到不同环境,可以通过激活不同的环境版本,实现动态配置切换
profile配置方式
多profile文件方式
application-dev.properties/yml 开发环境
application-test.properties/yml测试环境
application-pro.properties/yml生产环境
yml多文档方式
得用—分隔开
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
|
spring: profiles: active: pro --- server: port: 8081 spring: config: activate: on-profile: dev --- server: port: 8082 spring: config: activate: on-profile: pro --- server: port: 8083 spring: config: activate: on-profile: test ---
|
profile激活方式
配置文件
1 2 3
|
spring.profiles.active=dev
|
虚拟机参数
进入配置里面找VM
1
| -Dspring.profiles.active=test
|
命令行参数
1
| java-jar xxx.jar --spring.profiles.active=dev
|
7.Springboot配置
内部配置加载顺序如下,高优先级会覆盖低优先级
- file:./config/:当前项目下的/config目录下
- file:./:当前项目根目录下
- classpath:/config/:classoath的config目录
- classpath:/:classpath的根目录
8. 整合jdbc
9.整合Junit
- 搭建springboot工程
- 引入starter-test依赖
- 编写测试类
- 添加测试相关注解
- @RunWith(SpringRunner.class)
- @SpringBootTest(classes=启动类.class)
- 编写测试方法
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
| package com.zkjg.test;
/* * userServiceTest测试类 * */
import com.zkjg.springboottest.SpringbootTestApplication; import com.zkjg.springboottest.UserService; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.test.context.junit4.SpringRunner;
@RunWith(SpringRunner.class) @SpringBootTest(classes = SpringbootTestApplication.class) public class UserServiceTest { @Autowired private UserService userService;
@Test public void testAdd(){ userService.add(); } }
|
10.整合Redis
- 搭建Springboot工程
- 引入redis起步依赖
- 配置redis相关属性
- 注入RedisTemplate模板
- 编写测试方法
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
| package com.zkjg.springboot_redis;
import org.junit.jupiter.api.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.autoconfigure.cache.CacheProperties; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.data.redis.core.RedisTemplate; import org.springframework.test.context.junit4.SpringRunner;
@RunWith(SpringRunner.class) @SpringBootTest class SpringbootRedisApplicationTests {
@Autowired(required = false) private RedisTemplate redisTemplate;
@Test public void testSet(){ //存数据 redisTemplate.boundValueOps("name").set("zhangsan"); }
@Test public void testGet(){ //获取数据 Object name=redisTemplate.boundValueOps("name").get(); System.out.println(name); }
}
|
11.整合MyBatis
- 搭建Springboot工程
- 引入MyBatis起步依赖,添加mysql驱动
- 编写DataSource和MyBatis相关配置
- 定义表和实体类
- 编写dao和mapper文件/纯注解开发
- 测试
application.yml
1 2 3 4 5 6 7 8 9 10
| spring: datasource: username: root password: ych021029 url: jdbc:mysql://localhost:3306/data?serverTimezone=UTC&useUnicode=true&characterEncoding=utf-8 driver-class-name: com.mysql.cj.jdbc.Driver
|
UserMapper.java(接口)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
| package com.zkjg.springbootmybatis.mapper;
import com.zkjg.springbootmybatis.domain.User; import org.apache.ibatis.annotations.Mapper; import org.apache.ibatis.annotations.Select; import org.springframework.stereotype.Repository;
import java.util.List;
/** * @author Yuuu */ @Mapper @Repository public interface UserMapper {
@Select("select * from demo") public List<User> findAll(); }
|
测试类
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
| package com.zkjg.springbootmybatis;
import com.zkjg.springbootmybatis.domain.User; import com.zkjg.springbootmybatis.mapper.UserMapper; import org.junit.jupiter.api.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.test.context.junit4.SpringRunner;
import java.util.List;
@RunWith(SpringRunner.class) @SpringBootTest class SpringbootMybatisApplicationTests {
@Autowired private UserMapper userMapper;
@Autowired private UserMapper userXmlMapper;
@Test public void testFindAll(){ List<User> list = userMapper.findAll(); System.out.println(list); }
@Test public void testFindAll2(){ List<User> list = userXmlMapper.findAll(); System.out.println(list); }
@Test void contextLoads() { }
}
|
User.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
| package com.zkjg.springbootmybatis.domain;
public class User { private int id; private String name;
public int getId() { return id; }
public void setId(int id) { this.id = id; }
public String getName() { return name; }
public void setName(String name) { this.name = name; }
@Override public String toString() { return "User{" + "id=" + id + ", name='" + name + '\'' + '}'; } }
|
UserMapper.xml
1 2 3 4 5 6 7
| <?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <mapper namespace="com.zkjg.springbootmybatis.mapper.UserXmlMapper"> <select id="finAll" resultType="user"> select * from demo </select> </mapper>
|