SpringBoot学习-利用yml/yaml配置,及SpringBoot测试的使用

使用yml进行数据库相关配置

准配创建yml的配置文件,名称必须为application.yml

jdbc:
  driver: com.mysql.jdbc.Driver
  url: jdbc:mysql://localhost:3306/test?characterEncoding=utf-8
  username: root
  password: 123456

使用配置文件,通过创建一个包装类来分装这些属性,前提属性必须和配置文件的属性名一致,

@ConfigurationProperties(prefix = "jdbc")
@Data
@Component
public class JdbcProperties {
    private String username;
    private String password;
    private String url;
    private String driver;
}

SpringBoot中的单元测试

1.在测试的类的上面添加@RunWith(SpringRunner.class)
2.指定启动类@SpringBootTest(classes = Application.class)
3.使用@Test标记测试方法

@RunWith(SpringRunner.class)
@SpringBootTest(classes = Application.class)
public class MyTest {
    @Autowired
    private JdbcProperties jdbcProperties;

    @Test
    public void test(){
        System.out.println(jdbcProperties);
    }
}
发布了47 篇原创文章 · 获赞 6 · 访问量 2219

猜你喜欢

转载自blog.csdn.net/weixin_44467251/article/details/101103825