package com.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 org.springframework.jdbc.core.JdbcTemplate;

import javax.sql.DataSource;

@Configuration
@PropertySource("classpath:jdbc.properties")
public class JdbcConfig {
    // 写一个dataSource
    @Value("${jdbc.url}")
    String jdbcUrl;
    @Value("${jdbc.driver}")
    String driver;
    @Value("${jdbc.username}")
    String userName;
    @Value("${jdbc.password}")
    String password;
    @Bean("getJdbcUrl")
    public String getJdbcUrl() {
        return jdbcUrl;
    }
    @Bean
    public JdbcTemplate backJdbc() {
        DruidDataSource druidDataSource = new DruidDataSource();
        druidDataSource.setDriverClassName(driver);
        druidDataSource.setUrl(jdbcUrl);
        druidDataSource.setUsername(userName);
        druidDataSource.setPassword(password);
        JdbcTemplate jdbcTemplate = new JdbcTemplate();
        jdbcTemplate.setDataSource(druidDataSource);
        return jdbcTemplate;
    }
}
