使用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);
}
}