SpringBoot+JPA+Druid多数据源配置完全版+有坑提⽰
## 简介
在下是刚毕业的⼩萌新,现在在⼀家股票资讯公司做Java开发,⼿头上的项⽬(crud项⽬)本来是只连Mysql,现新增了功能需要连Postgre,于是哼哧哼哧开始了JPA的多数据源配置。经历⼀番轰轰烈烈的搜索,了好⼏个多数据源的配置教程,就开始了我的模仿表演。没想到⼀步⼀个坑,经过⼏周的摸索,总算是爬了出来。吾⽇三省吾⾝,于是决定记下这坑爹的经历。
## 多数据配置
开门见⼭,先贴出配置代码,之后再说说⾥⾯的坑
1. **项⽬结构**
common:⼀些⼯具类、通⽤dto
dao:数据访问层,查询⽅法
domain:实体
service:业务实现
web:接⼝定义
2. **application-dev.properties**
```
# datasource配置
# 主数据源
druid连接池配置详解spring.datasource.druid.primary.url=jdbc:mysql://xxxx:3306/xxxx?characterEncoding=utf8&useSSL=true&serverTimezone=UTC
spring.datasource.druid.primary.username=root
spring.datasource.druid.primary.password=root
spring.datasource.druid.primary.sql.jdbc.Driver
# 第⼆数据源
spring.datasource.druid.secondary.url=jdbc:mysql://xxxx:5432/postgres?
useUnicode=true&characterEncoding=utf8¤tSchema=db40
spring.datasource.druid.secondary.username=root
spring.datasource.druid.secondary.password=root
spring.datasource.druid.secondary.driver-class-name=com.postgresql.Driver
# 监控
spring.datasource.druid.use-globall-data-source-stat=true
spring.datasource.abled=true
spring.datasource.druid.stat-view-servlet.allow=
spring.datasource.druid.stat-view-servlet.login-username=admin
spring.datasource.druid.stat-view-servlet.login-password=admin
spring.datasource.set-enable=false
spring.datasource.druid.stat-view-servlet.url-pattern=/druid/*
# 主数据源连接池配置
spring.datasource.druid.primary.initial-size=2
spring.datasource.druid.primary.max-active=30
spring.datasource.druid.primary.min-idle=2
spring.datasource.druid.primary.max-wait=60000
spring.datasource.druid.primary.validation-query=select 'a'
spring.datasource.st-on-borrow=false
spring.datasource.st-on-return=false
spring.datasource.st-while-idle=true
spring.datasource.druid.primary.time-between-eviction-runs-millis=60000
spring.datasource.druid.primary.min-evictable-idle-time-millis=300000
spring.datasource.druid.primary.filters=stat
spring.datasource.tionProperties=Sql=true;druid.stat.slowSqlMillis=5000 spring.datasource.druid.lusions=*.js,*.gif,*.jpg,*.bmp,*.png,*.css,*.ico,/druid/*
spring.datasource.druid.primary.pool-prepared-statements=true
spring.datasource.druid.primary.max-pool-prepared-statement-per-connection-size=20
# 第⼆数据源连接池配置
spring.datasource.druid.secondary.initial-size=2
spring.datasource.druid.secondary.max-active=30
spring.datasource.druid.secondary.min-idle=2
spring.datasource.druid.secondary.max-wait=60000
spring.datasource.druid.secondary.validation-query=select 'a'
spring.datasource.st-on-borrow=false
spring.datasource.st-on-return=false
spring.datasource.st-while-idle=true
spring.datasource.druid.secondary.time-between-eviction-runs-millis=60000
spring.datasource.druid.secondary.min-evictable-idle-time-millis=300000
spring.datasource.druid.secondary.filters=stat
spring.datasource.tionProperties=Sql=true;druid.stat.slowSqlMillis=5000 spring.datasource.druid.secondary.useGlobalDataSourceStat=true
spring.datasource.druid.lusions=*.js,*.gif,*.jpg,*.bmp,*.png,*.css,*.ico,/druid/*
spring.datasource.druid.secondary.pool-prepared-statements=true
spring.datasource.druid.secondary.max-pool-prepared-statement-per-connection-size=20
# JPA配置
spring.jpa.show-sql=true
spring.jpa.properties.hibernate.fomat_sql=true
spring.jpa.open-in-view=true
ate-ddl=true
spring.jpa.hibernate.ddl-auto=update
spring.jpa.hibernate.primary-dialect=org.hibernate.dialect.MySQL5Dialect
spring.jpa.hibernate.secondary-dialect=org.hibernate.dialect.PostgreSQL9Dialect
```
3. **DruidDataSourceConfig.java**
```
# Druid配置
@Configuration
public class DruidDataSourceConfig {
@Primary
@Qualifier("primaryDataSource")
@Bean(name = "primaryDataSource")
@ConfigurationProperties(prefix = "spring.datasource.druid.primary")
public DataSource primaryDataSource() {
ate().build();
@Qualifier("secondaryDataSource")
@Bean(name = "secondaryDataSource")
@ConfigurationProperties(prefix = "spring.datasource.druid.secondary")
public DataSource secondaryDataSource() {
ate().build();
}
```
4. **PrimaryConfig.java**
```
# 主数据源配置-mysql
@Configuration
@EnableTransactionManagement
@EnableJpaRepositories(
entityManagerFactoryRef="entityManagerFactoryPrimary",
transactionManagerRef="transactionManagerPrimary",
basePackages= { "sql.*" }) //设置Repository所在位置public class PrimaryConfig {
@Autowired
@Qualifier("primaryDataSource")
private DataSource primaryDataSource;
@Autowired
private JpaProperties jpaPreperties;
@Value("${spring.jpa.hibernate.primary-dialect}")
private String primaryDialect;
@Primary
@Bean(name = "entityManagerPrimary")
public EntityManager entityManager(EntityManagerFactoryBuilder builder) {
return entityManagerFactoryPrimary(builder).getObject().createEntityManager();
@Primary
@Bean(name = "entityManagerFactoryPrimary")
public LocalContainerEntityManagerFactoryBean entityManagerFactoryPrimary(EntityManagerFactoryBuilder builder) { return builder
.dataSource(primaryDataSource)
.properties(getVendorProperties(primaryDataSource))
.packages("sql.*") //设置实体类所在位置
.persistenceUnit("primaryPersistenceUnit")
.build();
}
private Map<String, String> getVendorProperties(DataSource dataSource) {
# ⼿动设置命名策略(可选)
Map<String,String> map=new HashMap<>(2);
map.put("hibernate.dialect",primaryDialect);
jpaProperties.setProperies(map);
HibernateProperties(dataSource);
}
@Primary
@Bean(name = "transactionManagerPrimary")
public PlatformTransactionManager transactionManagerPrimary(EntityManagerFactoryBuilder builder) {
return new JpaTransactionManager(entityManagerFactoryPrimary(builder).getObject());
}
}
```
5. **SecondaryConfig.java**
```
# 第⼆数据源配置-pg
@Configuration
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论