前言:
在上一章中,我们已经搭建好项目,现在来讲一下如何配置数据源。
由于在有的项目中,用的数据源可能会涉及多个,且是不同类型的,我们接下来就讲解多数据源的配置。
情景描述:
现有项目需要访问不同的数据库,有Mysql、Oracle、Sql server数据库
就以该情景为例,讲讲该如何配置。
步骤:
1、在resources文件夹下新建persistence-multiple-db.properties资源文件,如下图:
(备注:具体的值自己配置)
# Mysql数据库
app.datasource.mysql.url=jdbc:mysql://192.168.1.100/XXX数据库名称
app.datasource.mysql.username=username
app.datasource.mysql.password=password
app.datasource.mysql.driver-class-name=com.mysql.jdbc.Driver
app.datasource.mysql.maximum-pool-size=10 # Oracle数据库
app.datasource.oracle.url=jdbc:oracle:thin:@192.168.1.100:1521/XXX数据库名称
app.datasource.oracle.username=username
app.datasource.oracle.password=password
app.datasource.oracle.driver-class-name=oracle.jdbc.driver.OracleDriver
app.datasource.oracle.maximum-pool-size=30 # Sql server数据库
app.datasource.sqlserver.url=jdbc:sqlserver://192.168.1.100;databaseName=XXX数据库名称
app.datasource.sqlserver.username=username
app.datasource.sqlserver.password=password
app.datasource.sqlserver.driver-class-name=com.microsoft.sqlserver.jdbc.SQLServerDriver
app.datasource.sqlserver.maximum-pool-size=30
2、在config文件夹中新建MysqlDatabaseConfig.java、OracleDatabaseConfig.java、SqlserverDatabaseConfig.java
在domain文件夹中新建mysql、oracle、sqlserver文件夹
在repository文件夹中新建mysql、oracle、sqlserver文件夹
(备注:命名可以自定义,我这里是为了便于区分)
接下来就依次配置这三个文件,以MysqlDatabaseConfig.java为例说明一下,另外两个同理,但是要注意@Primary(优先考虑,优先考虑被注解的对象注入)注解,另外两个不要添加该注解
package com.hyl.springdemo.config; import com.ulisesbocchio.jasyptspringboot.annotation.EnableEncryptableProperties;
import com.zaxxer.hikari.HikariDataSource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.jdbc.DataSourceBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.context.annotation.PropertySource;
import org.springframework.core.env.Environment;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
import org.springframework.orm.jpa.JpaTransactionManager;
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
import org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter;
import org.springframework.transaction.PlatformTransactionManager; import javax.sql.DataSource;
import java.util.HashMap; @Configuration
@EnableEncryptableProperties
@PropertySource({ "classpath:persistence-multiple-db.properties" })
@EnableJpaRepositories(
basePackages = "com.hyl.springdemo.repository.mysql",
entityManagerFactoryRef = "mysqlEntityManager",
transactionManagerRef = "mysqlTransactionManager"
)
public class MysqlDatabaseConfig { @Autowired
private Environment env; @Bean
@Primary
public DataSource mysqlDataSource() { HikariDataSource dataSource = DataSourceBuilder.create().type(HikariDataSource.class).build();
dataSource.setDriverClassName(env.getProperty("app.datasource.mysql.driver-class-name"));
dataSource.setJdbcUrl(env.getProperty("app.datasource.mysql.url"));
dataSource.setUsername(env.getProperty("app.datasource.mysql.username"));
dataSource.setPassword(env.getProperty("app.datasource.mysql.password"));
dataSource.setMaximumPoolSize(Integer.parseInt(env.getProperty("app.datasource.mysql.maximum-pool-size"))); return dataSource;
} @Bean
@Primary
public LocalContainerEntityManagerFactoryBean mysqlEntityManager() {
LocalContainerEntityManagerFactoryBean em
= new LocalContainerEntityManagerFactoryBean();
em.setDataSource(mysqlDataSource());
em.setPackagesToScan(
new String[] { "com.hyl.springdemo.domain.mysql" }); HibernateJpaVendorAdapter vendorAdapter
= new HibernateJpaVendorAdapter();
em.setJpaVendorAdapter(vendorAdapter);
em.setPersistenceUnitName("MysqlPersistenceUnit");
HashMap<String, Object> properties = new HashMap<>();
properties.put("hibernate.hbm2ddl.auto",
env.getProperty("hibernate.hbm2ddl.auto"));
properties.put("hibernate.dialect",
"org.hibernate.dialect.MySQL5Dialect");
em.setJpaPropertyMap(properties); return em;
} @Bean
@Primary
public PlatformTransactionManager mysqlTransactionManager() { JpaTransactionManager transactionManager
= new JpaTransactionManager();
transactionManager.setEntityManagerFactory(
mysqlEntityManager().getObject());
return transactionManager;
}
}
3、新建Repository,访问数据库(关于该部分,在后续的文章中再进行介绍)
4、新建Service层,访问Repository
5、在Controller层中进行调用
6、运行项目,在网页中查看
此篇博文主要是为了介绍配置多数据源,涉及到的其它内容在后续的博文中会逐一介绍
具体源码地址:https://github.com/AnneHan/springboot-demo
欢迎satr or fork