JdbcConfig.java
Configuration是配置文件
PropertySource 引入配置文件
value读取配置文件内容
package cn.itcast.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}") private String seturl; @Value("${jdbc.driverClassName}") private String driver; @Value("${jdbc.username}") private String username; @Value("${jdbc.password}") private String password; @Bean public DataSource dataSource(){ DruidDataSource druidDataSource = new DruidDataSource(); druidDataSource.setDriverClassName(seturl); druidDataSource.setUrl(driver); druidDataSource.setUsername(username); druidDataSource.setPassword(password); return druidDataSource; } }
jdbc.properties
jdbc.driverClassName=com.mysql.jdbc.Driver jdbc.url=jdbc:mysql://127.0.0.1:3306/leyou jdbc.username=root jdbc.password=
原文地址:https://www.cnblogs.com/wuxiang12580/p/10584121.html
时间: 2024-11-10 15:22:21