springboot中注入yaml配置到实体类(配置类)

本文章主要讲解yaml如何配置到实体类,配置类同理。

思路:

  • 编写实体类
  • 实体类在默认全局配置文件中寻找值(注解)
  • 编写yaml配置文件
  • 在测试类注入实体类

配置同理:

  • 编写配置类
  • 配置类在配置文件寻找值
  • 使用配置类

目录图:
在这里插入图片描述

首先编写两个实体类

@Component
@ConfigurationProperties(prefix = "person")
public class Person {
    private String name;
    private int age;
    private Map score;
    private List hobby;
    private Date birthday;
    private Dog dog;

    public Person() {
    }

    public Person(String name, int age, Map score, List hobby, Date birthday, Dog dog) {
        this.name = name;
        this.age = age;
        this.score = score;
        this.hobby = hobby;
        this.birthday = birthday;
        this.dog = dog;
    }

    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 Map getScore() {
        return score;
    }

    public void setScore(Map score) {
        this.score = score;
    }

    public List getHobby() {
        return hobby;
    }

    public void setHobby(List hobby) {
        this.hobby = hobby;
    }

    public Date getBirthday() {
        return birthday;
    }

    public void setBirthday(Date birthday) {
        this.birthday = birthday;
    }

    public Dog getDog() {
        return dog;
    }

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

    @Override
    public String toString() {
        return "Person{" +
                "name='" + name + '\'' +
                ", age=" + age +
                ", score=" + score +
                ", hobby=" + hobby +
                ", birthday=" + birthday +
                ", dog=" + dog +
                '}';
    }
}
public class Dog {
    private int age;
    private String name;
    private String color;
}

编写yaml配置文件

person:
  name: lina
  age: 15
  score: {k1: v1,k2: v2}
  hobby:
    - basketball
    - swim
  birthday: 2022/06/25
  dog:
    age: 2
    name: 花花
    color: green

测试类中测试以下

@SpringBootTest
class AgainbootApplicationTests {
    @Autowired
    private Person person;

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

}

扩展:springboot底层自动配置采用的配置类也是如此

  • springboot底层只指向spring.factories文件,文件里表明自动配置的类有哪些
    在这里插入图片描述
  • 打开其中一个自动配置类,注解==@EnableConfigurationProperties==指向一个配置类
@Configuration(
   proxyBeanMethods = false
)
@ConditionalOnClass({RedisOperations.class})
@EnableConfigurationProperties({RedisProperties.class})
@Import({LettuceConnectionConfiguration.class, JedisConnectionConfiguration.class})
public class RedisAutoConfiguration {
   public RedisAutoConfiguration() {
   }
...
}
  • 打开配置类RedisProperties.class,可以看到里面指向”spring.redis“开头的配置。和我们用的一样~

@ConfigurationProperties(
   prefix = "spring.redis"
)
public class RedisProperties {
   private int database = 0;
   private String url;
   private String host = "localhost";
   private String password;
   private int port = 6379;
   private boolean ssl;
   private Duration timeout;
   private String clientName;
   private RedisProperties.Sentinel sentinel;
   private RedisProperties.Cluster cluster;
   private final RedisProperties.Jedis jedis = new RedisProperties.Jedis();
   private final RedisProperties.Lettuce lettuce = new RedisProperties.Lettuce();
...
}
  • 我们就可以在application.yaml配置它的方法了。
    在这里插入图片描述
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Spring Boot使用MyBatis需要进行以下配置: 1. 添加MyBatis和MyBatis-Spring的依赖 ```xml <dependencies> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>2.2.0</version> </dependency> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>3.5.7</version> </dependency> </dependencies> ``` 2. 配置数据库连接信息 在`application.properties`或`application.yml`配置数据库连接信息: ```yaml spring: datasource: driver-class-name: com.mysql.cj.jdbc.Driver url: jdbc:mysql://localhost:3306/test?serverTimezone=Asia/Shanghai&useSSL=false&characterEncoding=UTF-8 username: root password: root ``` 3. 配置MyBatis 在`application.properties`或`application.yml`配置MyBatis相关信息: ```yaml mybatis: mapper-locations: classpath:mapper/*.xml type-aliases-package: com.example.model ``` 其,`mapper-locations`指定mapper文件的位置,`type-aliases-package`指定实体类的包名。 4. 编写Mapper接口和Mapper XML文件 在Mapper接口定义SQL语句,同时在对应的Mapper XML文件编写对应的SQL语句。 例如,定义一个UserMapper接口: ```java public interface UserMapper { List<User> findAll(); } ``` 对应的UserMapper.xml文件: ```xml <mapper namespace="com.example.mapper.UserMapper"> <select id="findAll" resultType="com.example.model.User"> select * from user </select> </mapper> ``` 5. 在Service层注入Mapper接口 ```java @Service public class UserServiceImpl implements UserService { @Autowired private UserMapper userMapper; @Override public List<User> findAll() { return userMapper.findAll(); } } ``` 这样就完成了Spring Boot和MyBatis的整合。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值