springbootJPA2.2druid多数据源配置

编程

server.port=8100

# 主数据库访问配置

spring.datasource.druid.db-type=com.alibaba.druid.pool.DruidDataSource

spring.datasource.druid.driverClassName=com.mysql.cj.jdbc.Driver

#spring.datasource.druid.url=jdbc:mysql://localhost:3306/db_test1?allowMultiQueries=true&autoReconnect=true&characterEncoding=utf-8&serverTimezone=UTC

spring.datasource.druid.url=jdbc:mysql://17.168.0.210:3306/db_platform_yun?useUnicode=true&characterEncoding=utf-8&zeroDateTimeBehavior=convertToNull&useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=GMT%2B8

spring.datasource.druid.username=root

spring.datasource.druid.password=123456

# 下面为连接池的补充设置,应用到上面所有数据源中

# 初始化大小,最小,最大

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

# 打开PSCache,并且指定每个连接上PSCache的大小

spring.datasource.druid.pool-prepared-statements=true

spring.datasource.druid.max-pool-prepared-statement-per-connection-size=20

# 配置监控统计拦截的filters,去掉后监控界面sql无法统计,"wall"用于防火墙

spring.datasource.druid.filter.commons-log.connection-logger-name=stat,wall,log4j

spring.datasource.druid.filter.stat.log-slow-sql=true

spring.datasource.druid.filter.stat.slow-sql-millis=2000

# 通过connectProperties属性来打开mergeSql功能;慢SQL记录

spring.datasource.druid.connect-properties.=druid.stat.mergeSql=true;druid.stat.slowSqlMillis=5000

# 合并多个DruidDataSource的监控数据

spring.datasource.druid.use-global-data-source-stat=true

#从数据库配置

spring.datasource.druid.salve.db-type=com.alibaba.druid.pool.DruidDataSource

spring.datasource.druid.salve.driverClassName=com.mysql.cj.jdbc.Driver

#spring.datasource.druid.url=jdbc:mysql://localhost:3306/db_hjy_reclaim?allowMultiQueries=true&autoReconnect=true&characterEncoding=utf-8&serverTimezone=UTC

spring.datasource.druid.salve.url=jdbc:mysql://17.168.0.210:3306/db_test2?useUnicode=true&characterEncoding=utf-8&zeroDateTimeBehavior=convertToNull&useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=GMT%2B8

spring.datasource.druid.salve.username=root

spring.datasource.druid.salve.password=123456

## 下面为连接池的补充设置,应用到上面所有数据源中

## 初始化大小,最小,最大

spring.datasource.druid.salve.initial-size=5

spring.datasource.druid.salve.min-idle=5

spring.datasource.druid.salve.max-active=20

# 配置获取连接等待超时的时间

spring.datasource.druid.salve.max-wait=60000

# 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒

spring.datasource.druid.salve.time-between-eviction-runs-millis=60000

# 配置一个连接在池中最小生存的时间,单位是毫秒

spring.datasource.druid.salve.min-evictable-idle-time-millis=300000

spring.datasource.druid.salve.validation-query=SELECT 1 FROM DUAL

spring.datasource.druid.salve.test-while-idle=true

spring.datasource.druid.salve.test-on-borrow=false

spring.datasource.druid.salve.test-on-return=false

# 打开PSCache,并且指定每个连接上PSCache的大小

spring.datasource.druid.salve.pool-prepared-statements=true

spring.datasource.druid.salve.max-pool-prepared-statement-per-connection-size=20

# 配置监控统计拦截的filters,去掉后监控界面sql无法统计,"wall"用于防火墙

spring.datasource.druid.salve.filter.commons-log.connection-logger-name=stat,wall,log4j

spring.datasource.druid.salve.filter.stat.log-slow-sql=true

spring.datasource.druid.salve.filter.stat.slow-sql-millis=2000

# 通过connectProperties属性来打开mergeSql功能;慢SQL记录

spring.datasource.druid.salve.connect-properties.=druid.stat.mergeSql=true;druid.stat.slowSqlMillis=5000

# 合并多个DruidDataSource的监控数据

spring.datasource.druid.salve.use-global-data-source-stat=true

#spring.autoconfigure.exclude=org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration,org.springframework.boot.autoconfigure.jdbc.DataSourceTransactionManagerAutoConfiguration

#spring.jpa.hibernate.naming.physical-strategy=org.hibernate.boot.model.naming.PhysicalNamingStrategyJpaCompliantImpl

#spring.jpa.hibernate.naming.implicit-strategy=org.hibernate.boot.model.naming.PhysicalImplicitStrategyStandardImpl

spring.jpa.hibernate.dd1-auto=update

#spring.jpa.hibernate.database-platform:org.hibernate.dialect.MySQL5InnoDBDialect

spring.jpa.show-sql=true

spring.jpa.properties.hibernate.format_sql=true

logging.level.org.hibernate.type.descriptor.sql.BasicBinder=trace

loging.level.root:trace

logging.level.springframework.web:INFO

logging.level.org.hibernate:ERROR

2.添加配置文件

2.1 DataSourceConfig

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.core.env.Environment;

import com.alibaba.druid.spring.boot.autoconfigure.DruidDataSourceBuilder;

@Configuration

public class DataSourceConfig {

/**

* 主DataSource 配置

*/

@Primary

@ConfigurationProperties(prefix = "spring.datasource.druid")

@Qualifier("primaryDruidDataSource")

@Bean(name = "primaryDruidDataSource")

public DataSource primaryDruidDataSource(Environment environment) {

return DruidDataSourceBuilder.create().build(environment, "spring.datasource.druid.");

}

/**

* 从DataSource 配置

*/

@ConfigurationProperties(prefix = "spring.datasource.druid.salve")

@Bean(name = "slaveDruidDataSource")

public DataSource slaveDruidDataSource(Environment environment) {

return DruidDataSourceBuilder.create().build(environment, "spring.datasource.druid.salve.");

}

}

2.2 JpaConfig

import java.util.Map;

import javax.annotation.Resource;

import javax.persistence.EntityManager;

import javax.sql.DataSource;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.boot.autoconfigure.orm.jpa.HibernateProperties;

import org.springframework.boot.autoconfigure.orm.jpa.HibernateSettings;

import org.springframework.boot.autoconfigure.orm.jpa.JpaProperties;

//import org.springframework.boot.autoconfigure.orm.jpa.JpaProperties;

import org.springframework.boot.orm.jpa.EntityManagerFactoryBuilder;

import org.springframework.context.annotation.Bean;

import org.springframework.context.annotation.Configuration;

import org.springframework.context.annotation.Primary;

import org.springframework.data.jpa.repository.config.EnableJpaRepositories;

import org.springframework.orm.jpa.JpaTransactionManager;

import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;

import org.springframework.transaction.PlatformTransactionManager;

import org.springframework.transaction.annotation.EnableTransactionManagement;

@Configuration

@EnableTransactionManagement

@EnableJpaRepositories(basePackages={"com.management.hjy.repository"},

entityManagerFactoryRef="entityManagerFactoryPrimary",

transactionManagerRef="transactionManagerPrimary")

public class JpaConfig {

@Resource(name = "primaryDruidDataSource")

private DataSource primaryDruidDataSource;

@Autowired

private HibernateProperties hibernateProperties;

@Autowired

private JpaProperties jpaProperties;

@Primary

@Bean(name = "entityManagerFactoryPrimary")

public LocalContainerEntityManagerFactoryBean entityManagerFactoryPrimary(EntityManagerFactoryBuilder builder) {

return builder

.dataSource(primaryDruidDataSource)

.properties(getVendorProperties())

.packages("com.management.hjy.entity") //设置实体类所在位置

.persistenceUnit("primaryPersistenceUnit")

.build();

}

@Primary

@Bean(name = "entityManagerPrimary")

public EntityManager entityManagerPrimary(EntityManagerFactoryBuilder builder) {

return entityManagerFactoryPrimary(builder).getObject().createEntityManager();

}

private Map<String, Object> getVendorProperties() {

return hibernateProperties.determineHibernateProperties(

jpaProperties.getProperties(), new HibernateSettings()

);

// return jpaProperties.getProperties();

}

@Primary

@Bean(name = "transactionManagerPrimary")

public PlatformTransactionManager transactionManagerPrimary(EntityManagerFactoryBuilder builder) {

return new JpaTransactionManager(entityManagerFactoryPrimary(builder).getObject());

}

}

2.3 JpaConfig1

import java.util.Map;

import javax.annotation.Resource;

import javax.persistence.EntityManager;

import javax.sql.DataSource;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.boot.autoconfigure.orm.jpa.HibernateProperties;

import org.springframework.boot.autoconfigure.orm.jpa.HibernateSettings;

import org.springframework.boot.autoconfigure.orm.jpa.JpaProperties;

import org.springframework.boot.orm.jpa.EntityManagerFactoryBuilder;

import org.springframework.context.annotation.Bean;

import org.springframework.context.annotation.Configuration;

import org.springframework.data.jpa.repository.config.EnableJpaRepositories;

import org.springframework.orm.jpa.JpaTransactionManager;

import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;

import org.springframework.transaction.PlatformTransactionManager;

import org.springframework.transaction.annotation.EnableTransactionManagement;

@Configuration

@EnableTransactionManagement

@EnableJpaRepositories(basePackages="com.management.hjy.repository1", // 数据库访问dao

entityManagerFactoryRef = "entityManagerFactorySlave",

transactionManagerRef = "transactionManagerSlave")

public class JpaConfig1 {

//slaveDruidDataSource

@Resource(name = "slaveDruidDataSource")

private DataSource slaveDruidDataSource;

@Autowired

private HibernateProperties hibernateProperties;

@Autowired

private JpaProperties jpaProperties;

@Bean(name = "entityManagerFactorySlave")

public LocalContainerEntityManagerFactoryBean entityManagerFactorySlave(EntityManagerFactoryBuilder builder) {

LocalContainerEntityManagerFactoryBean slavePersistenceUnit = builder

.dataSource(slaveDruidDataSource)

.properties(getVendorProperties())

.packages("com.management.hjy.entity") //设置实体类所在位置

// .persistenceUnit("slavePersistenceUnit")

.build();

slavePersistenceUnit.getJpaPropertyMap().remove("hibernate.hbm2ddl.auto");

return slavePersistenceUnit;

}

@Bean(name = "entityManagerSlave")

public EntityManager entityManagerSlave(EntityManagerFactoryBuilder builder) {

return entityManagerFactorySlave(builder).getObject().createEntityManager();

}

private Map<String, Object> getVendorProperties() {

return hibernateProperties.determineHibernateProperties(

jpaProperties.getProperties(), new HibernateSettings()

);

// return jpaProperties.getProperties();

}

@Bean(name = "transactionManagerSlave")

public PlatformTransactionManager transactionManagerSlave(EntityManagerFactoryBuilder builder) {

return new JpaTransactionManager(entityManagerFactorySlave(builder).getObject());

}

}

 

以上是 springbootJPA2.2druid多数据源配置 的全部内容, 来源链接: utcz.com/z/515751.html

回到顶部