1、添加MAVEN依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency><dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency><dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
<version>1.1.17</version>
</dependency>
2、配置文件:
server:
port: 8090
spring:
application:
name: DataSourceTest
datasource:
type: com.alibaba.druid.pool.DruidDataSource
druid:
one:
url: jdbc:mysql://localhost:3306/sourcetest?useUnicode=true&characterEncoding=utf-8&serverTimezone=GMT%2b8&useSSL=false
username: root
password: 123456
driver-class-name: com.mysql.cj.jdbc.Driver
db-type: mysql
initial-size: 5
max-active: 500
min-idle: 5
max-wait: 60000
max-pool-prepared-statement-per-connection-size: 50
max-open-prepared-statements: 50
validation-query: SELECT 1 FROM DUAL
test-on-borrow: false
test-on-return: false
test-while-idle: true
time-between-eviction-runs-millis: 60000
min-evictable-idle-time-millis: 300000
filters: stat,wall,slf4j
two:
url: jdbc:mysql://localhost:3306/sourcetest1?useUnicode=true&characterEncoding=utf-8&serverTimezone=GMT%2b8&useSSL=false
username: root
password: 123456
driver-class-name: com.mysql.cj.jdbc.Driver
db-type: mysql
initial-size: 5
max-active: 500
min-idle: 5
max-wait: 60000
max-pool-prepared-statement-per-connection-size: 50
max-open-prepared-statements: 50
validation-query: SELECT 1 FROM DUAL
test-on-borrow: false
test-on-return: false
test-while-idle: true
time-between-eviction-runs-millis: 60000
min-evictable-idle-time-millis: 300000
filters: stat,wall,slf4j
3、数据源连接类
import javax.sql.DataSource;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.jdbc.core.JdbcTemplate;
import com.alibaba.druid.pool.DruidDataSource;
@Configuration
public class DataSourceConfig {
@Bean(name = "dataSource")
@ConfigurationProperties(prefix = "spring.datasource.druid.one")
@Primary //设置主数据源
public DataSource dataSource(){
return new DruidDataSource();
}
@Bean(name = "dataSourceTwo")
@ConfigurationProperties(prefix = "spring.datasource.druid.two")
public DataSource DataSourceOne(){
return new DruidDataSource();
}
@Bean(name = "jdbcTemplate")
public JdbcTemplate jdbcTemplate(@Qualifier("dataSource") DataSource dataSource){
return new JdbcTemplate(dataSource);
}
@Bean(name = "jdbcTemplate2")
public JdbcTemplate jdbcTemplate2(@Qualifier("dataSourceTwo") DataSource dataSource){
return new JdbcTemplate(dataSource);
}
}
4、DAO使用方式:
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;
@Repository
public class Dao {
@Autowired
@Qualifier("jdbcTemplate")
JdbcTemplate jdbcTemplate;
@Autowired
@Qualifier("jdbcTemplate2")
JdbcTemplate jdbcTemplate1;
public String getUserName(String ID){
String sql = "select name from test where id = ?";
return jdbcTemplate.queryForObject(sql, String.class, new Object[]{ID});
}
public String getUserName1(String ID){
String sql = "select name from test where id = ?";
return jdbcTemplate1.queryForObject(sql, String.class, new Object[]{ID});
}
}
以上就是Druid+多数据源+JDBC使用方式,在实际项目中,有很多同学在生产环境需要加密数据库密码,那么就需要做一点小改动了:
1、首先通过Druid自带的加密方式加密密码:
一、进入Druid jar包所在文件夹路径,通过命令加密:
加密命令为:
java -cp druid-1.1.17.jar com.alibaba.druid.filter.config.ConfigTools password
将password替换为你的数据库密码即可。
结果如图:
其中password为加密后的字符串,privateKey为私钥,publicKey为公钥。
二、 修改配置文件:
将配置文件中密码替换为加密后的字符串,并将公钥添加进去,添加密码回调类,修改后的完整配置文件为:
server:
port: 8090
spring:
application:
name: DataSourceTest
datasource:
type: com.alibaba.druid.pool.DruidDataSource
publicKey: MFwwDQYJKoZIhvcNAQEBBQADSwAwSAJBALI4Yfqx/ihVlW0FsmwC1I1sc9t/5OV2ge4YdKF+ZmDnpk0c8JglEdwdx+1/qNHg4MP5Kvgu26LOt0E1zR6PQS0CAwEAAQ==
druid:
one:
url: jdbc:mysql://localhost:3306/sourcetest?useUnicode=true&characterEncoding=utf-8&serverTimezone=GMT%2b8&useSSL=false
username: root
password: ktwLokn7m57F7WNotRjJkYDYatGdP485mPe8iIAaK2k3cgMMbK5twzDxyCYBEGEs6v2+YE+r0qwunCUdl9bEfw==
driver-class-name: com.mysql.cj.jdbc.Driver
db-type: mysql
initial-size: 5
max-active: 500
min-idle: 5
max-wait: 60000
max-pool-prepared-statement-per-connection-size: 50
max-open-prepared-statements: 50
validation-query: SELECT 1 FROM DUAL
test-on-borrow: false
test-on-return: false
test-while-idle: true
time-between-eviction-runs-millis: 60000
min-evictable-idle-time-millis: 300000
filters: stat,wall,slf4j
passwordCallbackClassName: com.yanqi.utils.DbPasswordCallback
connection-properties: config.decrypt=true;publicKey=${spring.datasource.publicKey};password=${spring.datasource.druid.one.password}
filter:
config:
enabled: true
two:
url: jdbc:mysql://localhost:3306/sourcetest1?useUnicode=true&characterEncoding=utf-8&serverTimezone=GMT%2b8&useSSL=false
username: root
password: ktwLokn7m57F7WNotRjJkYDYatGdP485mPe8iIAaK2k3cgMMbK5twzDxyCYBEGEs6v2+YE+r0qwunCUdl9bEfw==
driver-class-name: com.mysql.cj.jdbc.Driver
db-type: mysql
initial-size: 5
max-active: 500
min-idle: 5
max-wait: 60000
max-pool-prepared-statement-per-connection-size: 50
max-open-prepared-statements: 50
validation-query: SELECT 1 FROM DUAL
test-on-borrow: false
test-on-return: false
test-while-idle: true
time-between-eviction-runs-millis: 60000
min-evictable-idle-time-millis: 300000
filters: stat,wall,slf4j
passwordCallbackClassName: com.yanqi.utils.DbPasswordCallback
connection-properties: config.decrypt=true;publicKey=${spring.datasource.publicKey};password=${spring.datasource.druid.two.password}
filter:
config:
enabled: true
其中passwordCallbackClassName为密码回调类,需要自己手动实现密码解密:
import java.util.Properties;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.alibaba.druid.filter.config.ConfigTools;
import com.alibaba.druid.util.DruidPasswordCallback;
/**
* Druid多数据源 密码回调
* @author yanqi
* @Date 2019年9月18日
*/
public class DbPasswordCallback extends DruidPasswordCallback{
private static final Logger logger = LoggerFactory.getLogger(DbPasswordCallback.class);
@Override
public void setProperties(Properties properties){
super.setProperties(properties);
//获取application.yml 里面配置的密码和公钥
String password = (String) properties.get("password");
String publickey = (String) properties.get("publicKey");
try {
String dbpassword = ConfigTools.decrypt(publickey, password);
setPassword(dbpassword.toCharArray());
} catch (Exception e) {
logger.error("Druid ConfigTools.decrypt", e);
}
}
}
以上就是整个数据源配置加实现方式,附上demo,有问题的小伙伴可以私信交流~