黑马SpringBoot2:Java代码方式配置

resources\jdbc.properties
jdbc.driverClassName=com.mysql.jdbc.Driver 
jdbc.url=jdbc:mysql://127.0.0.1:3306/springboot_test 
jdbc.username=root 
jdbc.password=root
config\JdbcConfig.java
package com.itheima.config;

import com.alibaba.druid.pool.DruidDataSource;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;

import javax.sql.DataSource;

@Configuration
@PropertySource("classpath:jdbc.properties")
public class JdbcConfig {
    @Value("${jdbc.url}")
    String url;
    @Value("${jdbc.driverClassName}")
    String driverClassName;
    @Value("${jdbc.username}")
    String username;
    @Value("${jdbc.password}")
    String password;
    @Bean
    public DataSource dataSource(){
        DruidDataSource dataSource = new DruidDataSource();
        dataSource.setDriverClassName(driverClassName);
        dataSource.setUrl(url);
        dataSource.setUsername(username);
        dataSource.setPassword(password);
        return dataSource;
    }
}
posted @ 2020-07-09 15:13  xidianzxm  阅读(156)  评论(0编辑  收藏  举报