yaml配置注入

yaml语法学习

配置文件

SpringBoot使用一个全局的配置文件 , 配置文件名称是固定的

  • application.properties
    • 语法结构:key=value
  • application.yml
    • 语法结构:key: 空格value

        配置文件的作用:修改SpringBoot自动配置的默认值,因为SpringBoot在底层都给我们自动配置好了;比如我们可以在配置文件中修改Tomcat 默认启动的端口号!测试一下!

server.port=8081

yaml概述

        YAML是 "YAML Ain't a Markup Language" (YAML不是一种标记语言)的递归缩写。在开发的这种语言时,YAML 的意思其实是:"Yet Another Markup Language"(仍是一种标记语言)

        这种语言以数据作为中心,而不是以标记语言为重点!

        以前的配置文件,大多数都是使用xml来配置;比如一个简单的端口配置,我们来对比下yaml和xml

传统xml配置

<server>
    <port>8081<port>
</server>

yaml配置

server:
  port: 8080

yaml基础语法

    说明:语法要求严格

        1. 空格不能省略
        2. 以缩进来控制层级关系,只要是左边对齐的一列数据都是同一个层级的
        3. 属性和值的大小写都是十分敏感的

字面量:普通的值 [ 数字,布尔值,字符串 ]

        字面量直接写在后面就可以 , 字符串默认不用加上双引号或者单引号;

注意:

  • ""双引号,不会转义字符串里面的特殊字符 , 特殊字符会作为本身想表示的意思;

  比如 :name: "hello \n world"  输出 :hello 换行 world

  • ''单引号,会转义特殊字符 , 特殊字符最终会变成和普通字符一样输出

  比如 :name: ‘hello \n world’  输出 :hello \n world

    对象、Map(键值对)

#对象、Map格式
k: 
    v1:
    v2:

        在下一行来写对象的属性和值的关系,注意缩进;比如:

student: 
  name: xiaoming
  age: 18

        行内写法

student: {name: xiaoming, age: 18}

    数组( List、set )

        用 - 值表示数组中的一个元素,比如:

pets:
  - cat
  - dog
  - pig

        行内写法

pets: [cat, dog, pig]

        修改SpringBoot的默认端口号

server:

  port: 8082

注入配置文件

        yaml文件更强大的地方在于,他可以给我们的实体类直接注入匹配值!

yaml注入配置文件

  1. 在springboot项目中的resources目录下新建一个文件 application.yaml

  2. 编写一个实体类Dog,并用@Value给bean注入属性值(注:通过配置文件给属性注入值,一定要写set方法,下面类其实可以暂时不用写set方法,不过后面会用到)

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

@Component
public class Dog {
    @Value("旺财")
    private String name;
    @Value("3")
    private Integer age;

    public Dog() {
    }

    public Dog(String name, Integer age) {
        this.name = name;
        this.age = age;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }

    @Override
    public String toString() {
        return "Dog{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }
}

        在测试类验证一下

import com.yuan.pojo.Dog;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

@SpringBootTest
class Springboot02ConfigApplicationTests {

    @Autowired
    private Dog dog;

    @Test
    void contextLoads() {
        System.out.println(dog);

    }

}

        打印输入如下

        再新建一个复杂点的实体类Person 并注入到我们的类中

import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.List;
import java.util.Map;

/*
@ConfigurationProperties作用:
将配置文件中配置的每一个属性的值,映射到这个组件中;
告诉SpringBoot将本类中的所有属性和配置文件中相关的配置进行绑定
参数 prefix = “person” : 将配置文件中的person下面的所有属性一一对应
*/

@Component
@ConfigurationProperties(prefix = "person")
public class Person {
    private String name;
    private Integer age;
    private Boolean happy;
    private Date birth;
    private Map<String,Object> maps;
    private List<Object> list;
    private Dog dog;

    public Person() {
    }

    public Person(String name, Integer age, Boolean happy, Date birth, Map<String, Object> maps, List<Object> list, Dog dog) {
        this.name = name;
        this.age = age;
        this.happy = happy;
        this.birth = birth;
        this.maps = maps;
        this.list = list;
        this.dog = dog;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }

    public Boolean getHappy() {
        return happy;
    }

    public void setHappy(Boolean happy) {
        this.happy = happy;
    }

    public Date getBirth() {
        return birth;
    }

    public void setBirth(Date birth) {
        this.birth = birth;
    }

    public Map<String, Object> getMaps() {
        return maps;
    }

    public void setMaps(Map<String, Object> maps) {
        this.maps = maps;
    }

    public List<Object> getList() {
        return list;
    }

    public void setList(List<Object> list) {
        this.list = list;
    }

    public Dog getDog() {
        return dog;
    }

    public void setDog(Dog dog) {
        this.dog = dog;
    }

    @Override
    public String toString() {
        return "Person{" +
                "name='" + name + '\'' +
                ", age=" + age +
                ", happy=" + happy +
                ", birth=" + birth +
                ", maps=" + maps +
                ", list=" + list +
                ", dog=" + dog +
                '}';
    }
}

        我们来使用yaml配置的方式进行注入,大家写的时候注意区别和优势,我们编写一个yaml配置!

person:
  name: xiaoming
  age: 18
  happy: true
  birth: 2000/10/01
  maps: {k1: v1,k2: v2}
  list:
    -code
    -music
    -run
  dog:
    name: 旺财
    age: 3

        IDEA 提示,springboot配置注解处理器没有找到,让我们看文档,我们可以查看文档,找到一个依赖!

        点击打开,跳转链接Configuration Metadata

        按照文档提供在pom.xml中导入依赖包,就不会报错了

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

        测试

import com.yuan.pojo.Person;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

@SpringBootTest
class Springboot02ConfigApplicationTests {

    @Autowired
    private Person person;

    @Test
    void contextLoads() {
        System.out.println(person);
    }
}

加载指定的配置文件

        @PropertySource :加载指定的配置文件;

        @ConfigurationProperties:默认从全局配置文件中获取值;

        我们去在resources目录下新建一个person.properties文件

name=xiaoming

        新建Person2,指定配置文件

@Component
@PropertySource(value = "classpath:person.properties")
public class Person2 {
    @Value("${name}")
    private String name;
    //...
}

配置文件占位符

        配置文件还可以编写占位符生成随机数

person:
  name: xiaoming${random.uuid}   # 随机uuid
  age: ${random.int} # 随机int
  happy: true
  birth: 1995/11/20
  map: {k1: v1, k2: v2}
  hello: a
  list:
    - code
    - music
  dog:
    name: ${person.hello:other}_小黄 # other代表默认值
    age: 1

        打印结果如下

Person{name='zyye360040f-0b1b-4562-b555-fa409901a021', age=218521969, happy=true, birth=Mon Nov 20 00:00:00 CST 1995, map={k1=v1, k2=v2}, list=[code, music], dog=Dog{name='a_小黄', age=1}}

    小结:

  • ${person.hello:other} 的用法是:当hello没有赋值的时候,直接赋予other值

回顾properties配置

        【注意】properties配置文件在写中文的时候,会有乱码 , 我们需要去IDEA中设置编码格式为UTF-8;

        新建一个配置文件user.properties

name=xiaoming
age=18
sex=男

        新建一个实体类User,并给属性注入值

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Component;

@Component
@PropertySource(value = "classpath:application.properties")
public class User {
    /**
     * 从配置文件中取值
     */
    @Value("${name}")
    private String name;
    /**
     * #{SPEL} Spring表达式
     */
    @Value("#{9*2}")
    private int age;
    /**
     * 字面量
     */
    @Value("男")
    private String sex;

    @Override
    public String toString() {
        return "User{" +
                "name='" + name + '\'' +
                ", age=" + age +
                ", sex='" + sex + '\'' +
                '}';
    }
}

        测试验证

import com.yuan.pojo.User;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

@SpringBootTest
class Springboot01IdeaApplicationTests {

    @Autowired
    private User user;

    @Test
    void contextLoads() {
        System.out.println(user);
    }
}

 

对比小结

        @Value这个使用起来并不友好!我们需要为每个属性单独注解赋值,比较麻烦;我们来看个功能对比图

@ConfigurationProperties@Value
功能批量注入配置文件中的属性一个个指定
松散绑定支持不支持
SPEL不支持支持
JSR303数据校验支持不支持
复杂类型封装支持不支持

        1. @ConfigurationProperties只需要写一次即可 , @Value则需要每个字段都添加

        2. 松散绑定:这个什么意思呢? 比如我的yml中写的last-name,这个和lastName是一样的,后面跟着的字母默认是大写的。这就是松散绑定。可以测试一下(注意set方法必须修改)

    yaml

person:
  last-name: xiaoming${random.uuid}   # 随机uuid
  age: ${random.int} # 随机int
# ....

//...
@Component
@ConfigurationProperties(prefix="person")
public class Person {
    private String lastName;
    //...
    public void setLastName(String lastName) {
        this.lastName = lastName;
    }
    //...
}

        这种也是可以正常输出的

        3.JSR303数据校验 , 这个就是我们可以在字段是增加一层过滤器验证 , 可以保证数据的合法性

    导入jar包

        <dependency>
            <groupId>javax.validation</groupId>
            <artifactId>validation-api</artifactId>
        </dependency>

        <dependency>
            <groupId>org.hibernate.validator</groupId>
            <artifactId>hibernate-validator</artifactId>
        </dependency>

        实体类校验

//...
@Component
@ConfigurationProperties(prefix = "person")
@Validated //数据校验
public class Person {
    @NotBlank(message = "姓名不可为空")
    private String name;
    private int age;
    //...
}

        application.yaml配置类

person:
  age: 18

        测试验证,报错如下

        拓展:

部分代码:
@NotNull(message="名字不能为空")
private String userName;
@Max(value=120,message="年龄最大不能查过120")
private int age;
@Email(message="邮箱格式错误")
private String email;
-----------------------
空检查
@Null       验证对象是否为null
@NotNull    验证对象是否不为null, 无法查检长度为0的字符串
@NotBlank   检查约束字符串是不是Null还有被Trim的长度是否大于0,只对字符串,且会去掉前后空格.
@NotEmpty   检查约束元素是否为NULL或者是EMPTY.
    
Booelan检查
@AssertTrue     验证 Boolean 对象是否为 true  
@AssertFalse    验证 Boolean 对象是否为 false  
    
长度检查
@Size(min=, max=) 验证对象(Array,Collection,Map,String)长度是否在给定的范围之内  
@Length(min=, max=) string is between min and max included.

日期检查
@Past       验证 Date 和 Calendar 对象是否在当前时间之前  
@Future     验证 Date 和 Calendar 对象是否在当前时间之后  
@Pattern    验证 String 对象是否符合正则表达式的规则

        4.复杂类型封装,yml中可以封装对象 , 使用value就不支持

结论

  • 配置yml和配置properties都可以获取到值 , 强烈推荐 yaml;
  • 如果我们在某个业务中,只需要获取配置文件中的某个值,可以使用一下 @value;
  • 如果说,我们专门编写了一个JavaBean来和配置文件进行一一映射,就直接使用@configurationProperties

<think>嗯,用户之前已经问过关于Java项目中的YAML配置文件,特别是MyBatis-Plus的配置。现在用户又询问关于MyBatis的YAML配置,可能需要区分MyBatis和MyBatis-Plus的不同配置方式。用户可能在实际项目中遇到了配置问题,或者在迁移项目到Spring Boot时不清楚如何正确配置MyBatis。 首先,需要确认用户是否混淆了MyBatis和MyBatis-Plus,因为两者的配置项有所不同。之前的回答中提到了MyBatis-Plus的配置,所以这次需要明确MyBatis的原生配置。可能用户的项目中没有使用MyBatis-Plus,而是纯MyBatis,所以需要详细说明原生配置的参数。 接下来,用户可能需要了解如何在Spring Boot中通过YAML配置MyBatis,包括数据源、Mapper扫描路径、别名处理等。此外,用户可能还需要知道如何配置XML映射文件的位置,以及日志实现等高级选项。 另外,用户可能对多环境配置和如何结合其他框架(如分页插件)感兴趣,但可能没有明确提到。需要判断是否需要补充这些内容,但根据当前问题,可能集中在MyBatis本身的配置上。 需要注意YAML的语法,比如缩进和结构,确保示例正确。同时,提醒用户配置的优先级和调试方法,比如使用@Value注入,或者检查日志输出,来确认配置是否生效。 可能用户的实际问题在于配置过程中遇到了错误,比如Mapper无法找到,或者XML文件未被加载,需要强调mapper-locations和type-aliases-package的重要性。此外,MyBatis的日志配置可能帮助用户调试SQL语句,这也是常见的问题点。 最后,是否需要提供额外的配置示例,比如事务管理或插件配置,但根据用户当前的问题,可能保持基础配置为主,避免信息过载。同时,如果用户需要更多细节,可以邀请他们进一步提问。</think>以下是为**原生MyBatis**(非MyBatis-Plus)在Spring Boot项目中常用的YAML配置说明,涵盖核心参数及其作用: --- ### 一、基础配置模板 ```yaml mybatis: # 映射文件路径(重点!) mapper-locations: classpath:mapper/*.xml # 实体类别名包路径(自动注册短类名作为别名) type-aliases-package: com.example.entity # 全局配置文件路径(可选) config-location: classpath:mybatis-config.xml ``` --- ### 二、详细配置项解析 #### 1. 核心参数 ```yaml mybatis: # XML映射文件路径(支持Ant风格通配符) mapper-locations: classpath*:/mappers/**/*.xml # 实体类扫描包(自动为@Alias注解类注册别名) type-aliases-package: com.example.model # 类型处理器包(自定义类型转换) type-handlers-package: com.example.handler # 是否开启驼峰命名映射 configuration.map-underscore-to-camel-case: true ``` #### 2. 日志与调试 ```yaml mybatis: configuration: # 显示实际执行的SQL(开发环境建议开启) log-impl: org.apache.ibatis.logging.stdout.StdOutImpl # 开启延迟加载 lazy-loading-enabled: true # 日志输出SQL执行时间(需配合p6spy等工具) log-slow-queries: 5000 ``` #### 3. XML映射配置示例 ```yaml mybatis: mapper-locations: - classpath:mapper/UserMapper.xml - classpath:mapper/OrderMapper.xml type-aliases-package: - com.example.entity - com.example.dto ``` --- ### 三、完整配置示例 ```yaml # application.yml mybatis: mapper-locations: classpath*:/mybatis-mappers/**/*.xml type-aliases-package: com.example.domain configuration: cache-enabled: false # 关闭二级缓存 default-fetch-size: 100 # 默认获取数量 default-statement-timeout: 30 # SQL超时时间(秒) map-underscore-to-camel-case: true jdbc-type-for-null: 'null' # 处理空类型 # 数据源配置(必须配合使用) spring: datasource: url: jdbc:mysql://localhost:3306/db1 username: root password: 123456 driver-class-name: com.mysql.cj.jdbc.Driver ``` --- ### 四、配置验证技巧 1. **检查XML加载**: 启动时查看控制台日志,确认输出类似 `Parsed mapper file: [file:/path/to/xxx.xml]` 2. **别名测试**: 在Mapper XML中使用短类名测试: ```xml <select id="selectUser" resultType="User"> <!-- 依赖type-aliases-package --> SELECT * FROM users </select> ``` 3. **SQL日志验证**: 执行查询后检查控制台是否输出完整SQL及参数 --- ### 常见问题排查 - **报错`Invalid bound statement`**: 检查`mapper-locations`路径是否包含XML文件,或Mapper接口是否添加`@Mapper`注解 - **字段映射失败**: 确认`map-underscore-to-camel-case`是否开启,或手动指定`resultMap` 如果需要补充MyBatis-Plus配置或具体场景的配置方法,可以继续说明需求! ✨
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值