springboot mysql多数据源 druid加解密连接数据库代码
时间: 2023-08-17 20:06:27 浏览: 143
好的,下面是一个示例代码,使用Spring Boot和Druid连接MySQL多数据源,并且对数据库密码进行了加解密:
1. 添加依赖
在 pom.xml 文件中添加以下依赖:
```
<dependencies>
<!-- Spring Boot Starter -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<!-- Spring Boot Starter JDBC -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<!-- Druid -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.1.23</version>
</dependency>
<!-- Jasypt -->
<dependency>
<groupId>com.github.ulisesbocchio</groupId>
<artifactId>jasypt-spring-boot-starter</artifactId>
<version>3.0.3</version>
</dependency>
</dependencies>
```
2. 配置数据源
在 application.yml 文件中添加以下配置:
```
# 默认数据源
spring.datasource.url=jdbc:mysql://localhost:3306/db1
spring.datasource.username=root
spring.datasource.password=ENC(加密后的密码)
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
# 第二个数据源
datasource2.url=jdbc:mysql://localhost:3306/db2
datasource2.username=root
datasource2.password=ENC(加密后的密码)
datasource2.driver-class-name=com.mysql.jdbc.Driver
```
其中,密码字段使用 Jasypt 进行加密,格式为 `ENC(加密后的密码)`。
3. 配置Druid数据源
在配置文件中添加以下配置:
```
# Druid数据源配置
spring.datasource.type=com.alibaba.druid.pool.DruidDataSource
# 配置第一个数据源
spring.datasource.druid.initial-size=5
spring.datasource.druid.min-idle=5
spring.datasource.druid.max-active=20
spring.datasource.druid.max-wait=60000
spring.datasource.druid.time-between-eviction-runs-millis=60000
spring.datasource.druid.min-evictable-idle-time-millis=300000
spring.datasource.druid.validation-query=SELECT 1 FROM DUAL
spring.datasource.druid.test-while-idle=true
spring.datasource.druid.test-on-borrow=false
spring.datasource.druid.test-on-return=false
spring.datasource.druid.pool-prepared-statements=true
spring.datasource.druid.max-pool-prepared-statement-per-connection-size=20
spring.datasource.druid.filters=stat,wall,log4j
# 配置第二个数据源
datasource2.druid.initial-size=5
datasource2.druid.min-idle=5
datasource2.druid.max-active=20
datasource2.druid.max-wait=60000
datasource2.druid.time-between-eviction-runs-millis=60000
datasource2.druid.min-evictable-idle-time-millis=300000
datasource2.druid.validation-query=SELECT 1 FROM DUAL
datasource2.druid.test-while-idle=true
datasource2.druid.test-on-borrow=false
datasource2.druid.test-on-return=false
datasource2.druid.pool-prepared-statements=true
datasource2.druid.max-pool-prepared-statement-per-connection-size=20
datasource2.druid.filters=stat,wall,log4j
```
4. 配置数据源连接池
在配置类中添加以下代码:
```
@Configuration
public class DataSourceConfig {
@Bean(name = "dataSource")
@Primary
@ConfigurationProperties(prefix = "spring.datasource")
public DruidDataSource dataSource() {
return DruidDataSourceBuilder.create().build();
}
@Bean(name = "dataSource2")
@ConfigurationProperties(prefix = "datasource2")
public DruidDataSource dataSource2() {
return DruidDataSourceBuilder.create().build();
}
@Bean(name = "dataSource1TransactionManager")
@Primary
public DataSourceTransactionManager dataSourceTransactionManager() {
return new DataSourceTransactionManager(dataSource());
}
@Bean(name = "dataSource2TransactionManager")
public DataSourceTransactionManager dataSource2TransactionManager() {
return new DataSourceTransactionManager(dataSource2());
}
@Bean(name = "dataSource1SqlSessionFactory")
@Primary
public SqlSessionFactory dataSourceSqlSessionFactory(@Qualifier("dataSource") DruidDataSource dataSource) throws Exception {
final SqlSessionFactoryBean sessionFactory = new SqlSessionFactoryBean();
sessionFactory.setDataSource(dataSource);
sessionFactory.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath:mapper/*.xml"));
return sessionFactory.getObject();
}
@Bean(name = "dataSource2SqlSessionFactory")
public SqlSessionFactory dataSource2SqlSessionFactory(@Qualifier("dataSource2") DruidDataSource dataSource) throws Exception {
final SqlSessionFactoryBean sessionFactory = new SqlSessionFactoryBean();
sessionFactory.setDataSource(dataSource);
sessionFactory.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath:mapper2/*.xml"));
return sessionFactory.getObject();
}
}
```
其中,`@Primary` 注解表示默认数据源,`@ConfigurationProperties` 注解表示从配置文件中读取配置。
5. 配置MyBatis
在 `application.yml` 文件中添加以下配置:
```
mybatis:
mapper-locations: classpath:mapper/*.xml
type-aliases-package: com.example.entity
```
6. 编写DAO和Mapper
在 `com.example.dao` 包中编写DAO和Mapper,例如:
```
@Repository
public interface UserDAO {
@Select("SELECT * FROM user")
@Results(id = "userResultMap", value = {
@Result(property = "id", column = "id", id = true),
@Result(property = "name", column = "name"),
@Result(property = "email", column = "email"),
@Result(property = "phone", column = "phone"),
@Result(property = "createTime", column = "create_time")
})
List<User> list();
}
@Mapper
public interface UserMapper {
@Select("SELECT * FROM user")
@ResultMap("userResultMap")
List<User> list();
}
```
7. 使用数据源
在Service中使用数据源,例如:
```
@Service
public class UserService {
@Autowired
private UserDAO userDAO;
@Autowired
private UserMapper userMapper;
@Transactional(transactionManager = "dataSource1TransactionManager")
public List<User> list() {
return userDAO.list();
}
@Transactional(transactionManager = "dataSource2TransactionManager")
public List<User> list2() {
return userMapper.list();
}
}
```
其中,`@Transactional` 注解表示开启事务,`transactionManager` 参数指定使用的数据源。
阅读全文