黑马SpringBoot2:Java代码方式配置

黑马SpringBoot2:Java代码方式配置_第1张图片

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;
    }
}

你可能感兴趣的:(黑马SpringBoot2:Java代码方式配置)