JPA的多mysql数据源配置
- 配置文件
配置文件 application.yml 增加备用库的设置如下:
- 配置文件
spring:
datasource:
# driver-class-name: com.mysql.jdbc.Driver 老版
primary:
driver-class-name: com.mysql.cj.jdbc.Driver
username: root
password: 12345678
# 一定是jdbc-url 要不然报错 数据源连接配置2.x和1.x的配置项是有区别的:2.x使用jdbc-url,
# 而1.x版本使用 url。
jdbc-url: jdbc:mysql://x.x.x.x:13306/your_primary_sql_name?charterEncoding=utf-8&useSSl=false&serverTimezone=Asia/Shanghai
secondary:
driver-class-name: com.mysql.cj.jdbc.Driver
username: root
password: 12345678
jdbc-url: jdbc:mysql://y.y.y.y:13306/you_secondary_sql_name?charterEncoding=utf-8&useSSl=false&serverTimezone=Asia/Shanghai
注意:如果你在配置的时候发生了这个报错java.lang.IllegalArgumentException: jdbcUrl is required with driverClassName. 就是url配置项错误。2.x版本下,url配置项名称应该为jdbc-url。
- 创建一个多数据源的配置类加载上述数据源配置
比如我新建一个配置类:DataSourceConfig,内容如下:
- 创建一个多数据源的配置类加载上述数据源配置
@Configuration
public class DataSourceConfig {
// @Primary注解指定了主数据源,就是当我们不特别指定哪个数据源的时候,就会使用这个Bean
@Primary
@Bean
@ConfigurationProperties(prefix = "spring.datasource.primary")
public DataSource primaryDataSource() {
return DataSourceBuilder.create().build();
}
@Bean
@ConfigurationProperties(prefix = "spring.datasource.secondary")
public DataSource secondaryDataSource() {
return DataSourceBuilder.create().build();
}
}
- 数据源primary配置类
新建数据源primary配置类:JpaConfigPrimary,内容如下:
- 数据源primary配置类
@Configuration
/* 使用 @EnableTransactionManagement 注解来进行 JPA 的配置,该注解中主要配置了三个属性:
basePackages 用来指定 Repository 所在的位置(可以配置多个包)。
entityManagerFactoryRef 用来指定实体类管理工厂 Bean 的名称。
transactionManagerRef 用来指定事物管理器的引用名称,这里的引用名称就是 JpaConfigOne 类中注册的 Bean 的名称(默认的 Bean 名称为方法名)
*/
@EnableTransactionManagement
@EnableJpaRepositories(entityManagerFactoryRef="entityManagerFactoryPrimary",
transactionManagerRef="transactionManagerPrimary",
basePackages = {"com.tony.miaosuan.repository"})
public class JpaConfigPrimary {
@Autowired
@Qualifier("primaryDataSource")
private DataSource primaryDataSource;
@Autowired
private JpaProperties jpaProperties;
@Autowired
private HibernateProperties hibernateProperties;
private Map<String, Object> getVendorProperties() {
return hibernateProperties.determineHibernateProperties(jpaProperties.getProperties(), new HibernateSettings());
}
@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)
.packages("com.tony.miaosuan.model") //设置实体类所在位置
.persistenceUnit("primaryPersistenceUnit")
.properties(getVendorProperties())
.build();
}
@Primary
@Bean(name = "transactionManagerPrimary")
public PlatformTransactionManager transactionManagerPrimary(EntityManagerFactoryBuilder builder) {
return new JpaTransactionManager(entityManagerFactoryPrimary(builder).getObject());
}
}
- 数据源secondary配置类
新建数据源secondary配置类:JpaConfigSecondary,内容如下:
- 数据源secondary配置类
@Configuration
@EnableTransactionManagement
@EnableJpaRepositories(entityManagerFactoryRef="entityManagerFactorySecondary",
transactionManagerRef="transactionManagerSecondary",
basePackages = {"com.tony.miaosuan.repository2"})
public class JpaConfigSencondary {
@Autowired
@Qualifier("secondaryDataSource")
private DataSource secondaryDataSource;
@Autowired
private JpaProperties jpaProperties;
@Autowired
private HibernateProperties hibernateProperties;
private Map<String, Object> getVendorProperties() {
return hibernateProperties.determineHibernateProperties(jpaProperties.getProperties(), new HibernateSettings());
}
@Bean(name = "entityManagerSecondary")
public EntityManager entityManager(EntityManagerFactoryBuilder builder) {
return entityManagerFactorySecondary(builder).getObject().createEntityManager();
}
@Bean(name = "entityManagerFactorySecondary")
public LocalContainerEntityManagerFactoryBean entityManagerFactorySecondary (EntityManagerFactoryBuilder builder) {
return builder
.dataSource(secondaryDataSource)
.packages("com.tony.miaosuan.model2") //设置实体类所在位置
.persistenceUnit("secondaryPersistenceUnit")
.properties(getVendorProperties())
.build();
}
@Bean(name = "transactionManagerSecondary")
PlatformTransactionManager transactionManagerSecondary(EntityManagerFactoryBuilder builder) {
return new JpaTransactionManager(entityManagerFactorySecondary(builder).getObject());
}
}