版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/bcqd1211/article/details/85835011
application.yml
配置文件
server:
port: 9090
servlet:
session:
timeout: 3600
tomcat:
uri-encoding: utf-8
spring:
jpa:
database: mysql
show-sql: true
hibernate:
ddl-auto: update
database-platform: org.hibernate.dialect.MySQL5InnoDBDialect
datasource:
ds1:
driver-class-name: com.mysql.jdbc.Driver
url: jdbc:mysql://xxxxx:3306/ds1?useUnicode=true&characterEncoding=utf-8&autoReconnect=true
type: com.zaxxer.hikari.HikariDataSource
username: root
password: root
hikari:
#连接池中允许的最大连接数。缺省值:10;推荐的公式:((core_count * 2) + effective_spindle_count)
maximum-pool-size: 5
minimum-idle: 3
pool-name: SpringBootHikariCP
# 一个连接的生命时长(毫秒),超时而且没被使用则被释放(retired),缺省:30分钟,建议设置比数据库超时时长少30秒,参考MySQL wait_timeout参数(show variables like '%timeout%';)
max-lifetime: 1765000
#连接只读数据库时配置为true, 保证安全
read-only: false
ds2:
driver-class-name: com.mysql.jdbc.Driver
url: jdbc:mysql://ssss:3306/ds2?useUnicode=true&characterEncoding=utf-8&autoReconnect=true
type: com.zaxxer.hikari.HikariDataSource
username: root
password: root
hikari:
#连接池中允许的最大连接数。缺省值:10;推荐的公式:((core_count * 2) + effective_spindle_count)
maximum-pool-size: 5
minimum-idle: 3
pool-name: SpringBootHikariCP
# 一个连接的生命时长(毫秒),超时而且没被使用则被释放(retired),缺省:30分钟,建议设置比数据库超时时长少30秒,参考MySQL wait_timeout参数(show variables like '%timeout%';)
max-lifetime: 1765000
#连接只读数据库时配置为true, 保证安全
read-only: false
#management:
# port: 54001
#
# # close security. 关闭身份验证,否则无法查询出数据
# security:
# enabled: false
management:
port: 54001
health:
mail:
enabled: false
info:
app:
name: "@project.name@" #从pom.xml中获取
description: "@project.description@"
version: "@project.version@"
spring-boot-version: "@project.parent.version@"
DataSourceConfig
数据源配置类,声明两个数据源的bean对象
package com.springboot.config;
import com.zaxxer.hikari.HikariDataSource;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.autoconfigure.jdbc.DataSourceProperties;
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;
/**
* @Author: chuan.bai
* @Description
* @Date: Created on 13:52 2018/9/30
* @Modified By:
*/
@Configuration
public class DataSourceConfig {
@Primary
@Bean(name = "ds1SProperties")
@Qualifier("ds1SProperties")
@ConfigurationProperties(prefix = "spring.datasource.ds1")
public DataSourceProperties xxDS1Properties() {//这是是用hikariCP的时候用的
return new DataSourceProperties();
}
@Bean(name = "ds2Properties")
@Qualifier("ds2Properties")
@ConfigurationProperties(prefix = "spring.datasource.ds2")
public DataSourceProperties xxDS2Properties() {
return new DataSourceProperties();
}
@Primary
@Bean(name = "ds1")
@ConfigurationProperties(prefix = "spring.datasource.ds1")
public HikariDataSource dataSourceOrder() {
//return DruidDataSourceBuilder.create().build(); //使用druidCP时打开这个注释,同时注释掉下面一行
return xxDS1Properties().initializeDataSourceBuilder().type(HikariDataSource.class).build();
}
@Bean(name = "ds2")
@ConfigurationProperties(prefix = "spring.datasource.ds2")
public HikariDataSource dataSourceAuth() {
return xxDS2Properties().initializeDataSourceBuilder().type(HikariDataSource.class).build();
}
}
Ds1Config 主数据源
设置事务配置,启用SpringDataJpa的配置参数
package com.springboot.config;
import org.springframework.beans.factory.annotation.Qualifier;
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.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;
import javax.annotation.Resource;
import javax.persistence.EntityManager;
import javax.sql.DataSource;
import java.util.Map;
@Configuration
@EnableTransactionManagement
@EnableJpaRepositories(
entityManagerFactoryRef="entityManagerFactoryPrimary",
transactionManagerRef="transactionManagerPrimary",
basePackages= { "com.springboot.repository.hardware" }) //设置Repository所在位置
public class Ds1Config {
/**
* 注入ds1数据源
*/
@Resource
@Qualifier("ds1")
private DataSource ds1Source;
@Primary
@Bean(name = "entityManagerPrimary")
public EntityManager entityManager(EntityManagerFactoryBuilder builder) {
return entityManagerFactoryPrimary(builder).getObject().createEntityManager();
}
@Resource
private JpaProperties jpaProperties;
private Map<String, Object> getVendorProperties() {
return jpaProperties.getHibernateProperties(new HibernateSettings());
}
/**
* 设置实体类所在位置
*/
@Primary
@Bean(name = "entityManagerFactoryPrimary")
public LocalContainerEntityManagerFactoryBean entityManagerFactoryPrimary(EntityManagerFactoryBuilder builder) {
return builder
.dataSource(ds1Source)
.packages("com.springboot.model.hardware")
.persistenceUnit("primaryPersistenceUnit")
.properties(getVendorProperties())
.build();
}
@Primary
@Bean(name = "transactionManagerPrimary")
public PlatformTransactionManager transactionManagerPrimary(EntityManagerFactoryBuilder builder) {
return new JpaTransactionManager(entityManagerFactoryPrimary(builder).getObject());
}
}
Ds2Config
package com.springboot.config;
import org.springframework.beans.factory.annotation.Qualifier;
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;
import javax.annotation.Resource;
import javax.persistence.EntityManager;
import javax.sql.DataSource;
import java.util.Map;
@Configuration
@EnableTransactionManagement
@EnableJpaRepositories(
entityManagerFactoryRef="entityManagerFactorySecondary",
transactionManagerRef="transactionManagerSecondary",
basePackages= {"com.springboot.repository.cook"}) //设置Repository所在位置
public class Ds2Config {
@Resource
@Qualifier("ds2")
private DataSource ds2Source;
@Bean(name = "entityManagerSecondary")
public EntityManager entityManager(EntityManagerFactoryBuilder builder) {
return entityManagerFactorySecondary(builder).getObject().createEntityManager();
}
@Resource
private JpaProperties jpaProperties;
private Map<String, Object> getVendorProperties() {
return jpaProperties.getHibernateProperties(new HibernateSettings());
}
@Bean(name = "entityManagerFactorySecondary")
public LocalContainerEntityManagerFactoryBean entityManagerFactorySecondary(EntityManagerFactoryBuilder builder) {
return builder
.dataSource(ds2Source)
.packages("com.springboot.model.cook")
.persistenceUnit("secondaryPersistenceUnit")
.properties(getVendorProperties())
.build();
}
@Bean(name = "transactionManagerSecondary")
PlatformTransactionManager transactionManagerSecondary(EntityManagerFactoryBuilder builder) {
return new JpaTransactionManager(entityManagerFactorySecondary(builder).getObject());
}
}