目录
1 新建SpringBoot项目
1.1 导入pom依赖文件
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.3.3.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.zjw</groupId>
<artifactId>springbootjdbc</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>springbootjdbc</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!--Thymeleaf-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<!--jdbc-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<!--mysql-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.15</version>
</dependency>
<!--druid-->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.1.12</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
2 通过@PropertySource
注解读取配置文件
2.1 新建jdbc.properties文件
在resources下新建jdbc.properties文件
jdbc.driverClassName=com.mysql.cj.jdbc.Driver
jdbc.url=jdbc:mysql://192.168.234.128/sb?useSSL=false&serverTimeZone=Shanghai/Asia
jdbc.username=root
jdbc.password=a@123456
2.2 新建配置类,加载配置文件
package com.zjw.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;
/**
* 数据源的JDBC配置类
*/
@Configuration
@PropertySource("classpath:/jdbc.properties") //加载指定的Properties配置文件
public class JdbcConfiguration {
@Value("${jdbc.driverClassName}")
private String driverClassName;
@Value("${jdbc.url}")
private String url;
@Value("${jdbc.username}")
private String username;
@Value("${jdbc.password}")
private String password;
/**
* 实例化Druid
* @return
*/
@Bean
public DataSource getDataSource(){
DruidDataSource source = new DruidDataSource();
source.setDriverClassName(this.driverClassName);
source.setUrl(this.url);
source.setUsername(this.username);
source.setPassword(this.password);
return source;
}
}
2.3 编写Controller测试类
debug观察dataSource的属性
package com.zjw.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import javax.annotation.Resource;
import javax.sql.DataSource;
@Controller
public class UsersController {
@Resource
private DataSource dataSource;
@GetMapping("/showInfo")
public String showInfo(){
return "OK";
}
}
3 通过@ConfigurationProperties注解读取配置信息
由于@ConfigurationProperties
是SpringBoot的注解不能读取其他配置文件,只能读取SpringBoot的application的位置文件,我们把数据库的连接信息放到application.properties文件中
3.1 application.properties文件添加数据库信息
jdbc.driverClassName=com.mysql.cj.jdbc.Driver
jdbc.url=jdbc:mysql://192.168.234.128/sb?useSSL=false&serverTimeZone=Shanghai/Asia
jdbc.username=root
jdbc.password=a@123456
3.2 新建JdbcProperties类,加载数据的信息
@ConfigurationProperties(prefix = "jdbc")
prefix属性用来指定配置文件的前缀,同时要为属性指定getter/setter方法
package com.zjw.config;
import org.springframework.boot.context.properties.ConfigurationProperties;
/**
* JDBC配置信息属性类
*/
@ConfigurationProperties(prefix = "jdbc") //是SpringBoot的注解不能读取其他配置文件,只能读取SpringBoot的application的位置文件
public class JdbcProperties {
private String driverClassName;
private String url;
private String username;
private String password;
public String getDriverClassName() {
return driverClassName;
}
public void setDriverClassName(String driverClassName) {
this.driverClassName = driverClassName;
}
public String getUrl() {
return url;
}
public void setUrl(String url) {
this.url = url;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
3.3 修改JdbcConfiguration类
通过@EnableConfigurationProperties(JdbcProperties.class)
加载数据库的配置信息
package com.zjw.config;
import com.alibaba.druid.pool.DruidDataSource;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import javax.annotation.Resource;
import javax.sql.DataSource;
/**
* 数据源的JDBC配置类
*/
@Configuration
@EnableConfigurationProperties(JdbcProperties.class)
public class JdbcConfiguration {
@Resource
private JdbcProperties jdbcProperties;
/**
* 实例化Druid
* @return
*/
@Bean
public DataSource getDataSource(){
DruidDataSource source = new DruidDataSource();
source.setDriverClassName(jdbcProperties.getDriverClassName());
source.setUrl(jdbcProperties.getUrl());
source.setUsername(jdbcProperties.getUsername());
source.setPassword(jdbcProperties.getPassword());
return source;
}
}
也可以通过构造方法注入JdbcProperties
package com.zjw.config;
import com.alibaba.druid.pool.DruidDataSource;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import javax.sql.DataSource;
/**
* 数据源的JDBC配置类
*/
@Configuration
@EnableConfigurationProperties(JdbcProperties.class)
public class JdbcConfiguration {
private JdbcProperties jdbcProperties;
public JdbcConfiguration(JdbcProperties jdbcProperties) {
this.jdbcProperties = jdbcProperties;
}
/**
* 实例化Druid
* @return
*/
@Bean
public DataSource getDataSource(){
DruidDataSource source = new DruidDataSource();
source.setDriverClassName(jdbcProperties.getDriverClassName());
source.setUrl(jdbcProperties.getUrl());
source.setUsername(jdbcProperties.getUsername());
source.setPassword(jdbcProperties.getPassword());
return source;
}
}
3.4 将@ConfigurationProperties
作用在方法上,取消JdbcProperties类
package com.zjw.config;
import com.alibaba.druid.pool.DruidDataSource;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import javax.sql.DataSource;
/**
* 数据源的JDBC配置类
*/
@Configuration
public class JdbcConfiguration {
/**
* 实例化Druid
* @return
*/
@Bean
@ConfigurationProperties(prefix = "jdbc")
public DataSource getDataSource(){
DruidDataSource source = new DruidDataSource();
return source;
}
}