赞
踩
spring:
datasource:
db1: # 1.0 Datasource
url: jdbc:mysql://127.0.0.1:3306/test1?useSSL=false&serverTimezone=GMT%2b8&characterEncoding=utf8&connectTimeout=1000&socketTimeout=3000&autoReconnect=true&cachePrepStmts=true&useServerPrepStmts=true
username: root
password: 123456
driverClassName: com.mysql.cj.jdbc.Driver
db2: # 2.0 Datasource
url: jdbc:mysql://127.0.0.1:3306/test2?useSSL=false&serverTimezone=GMT%2b8&characterEncoding=utf8&connectTimeout=1000&socketTimeout=3000&autoReconnect=true&cachePrepStmts=true&useServerPrepStmts=true
username: root
password: 123456
@ConfigurationProperties(prefix = "spring.datasource.db1")
@Component
@Data
public class Db1Properties {
private String url;
private String username;
private String password;
private String driverClassName;
}
@ConfigurationProperties(prefix = "spring.datasource.db2")
@Component
@Data
public class Db2Properties {
private String url;
private String username;
private String password;
private String driverClassName;
}
@Configuration
@Slf4j
public class DataSourceConfig {
@Autowired
private Db1Properties db1Properties;
@Autowired
private Db2Properties db2Properties;
@Bean(name = "db1DataSource")
@Qualifier("db1DataSource")
@Primary
public DataSource db1DataSource() {
log.info("db1DataSource 初始化 .....");
HikariDataSource dataSource = new HikariDataSource();
dataSource.setJdbcUrl(db1Properties.getUrl());
dataSource.setUsername(db1Properties.getUsername());
dataSource.setPassword(db1Properties.getPassword());
dataSource.setDriverClassName(db1Properties.getDriverClassName());
return dataSource;
}
@Bean(name = "db2DataSource")
@Qualifier("db2DataSource")
public DataSource db2DataSource() {
log.info("db2DataSource 初始化 .....");
HikariDataSource dataSource = new HikariDataSource();
dataSource.setJdbcUrl(db2Properties.getUrl());
dataSource.setUsername(db2Properties.getUsername());
dataSource.setPassword(db2Properties.getPassword());
dataSource.setDriverClassName(db2Properties.getDriverClassName());
return dataSource;
}
}
以下两个是需要修改的配置信息
@Configuration
@EnableTransactionManagement
@EnableJpaRepositories(
entityManagerFactoryRef = "db1EntityManagerFactory",
transactionManagerRef = "db1TransactionManager",
basePackages = {"com.ljm.db1todb2.db1.repository"}) // 指定该数据源操作的DAO接口包
public class Db1Config {
@Autowired
@Qualifier("db1DataSource")
private DataSource db1DataSource;
@Primary
@Bean(name = "db1EntityManagerFactory")
public LocalContainerEntityManagerFactoryBean db1EntityManagerFactory(EntityManagerFactoryBuilder builder) {
return builder
.dataSource(db1DataSource)
.packages("com.ljm.db1todb2.db1.model") //设置实体类所在位置
.persistenceUnit("db1PersistenceUnit")
.build();
}
@Bean(name = "db1TransactionManager")
public PlatformTransactionManager db1TransactionManager(EntityManagerFactoryBuilder builder) {
return new JpaTransactionManager(db1EntityManagerFactory(builder).getObject());
}
}
@Configuration
@EnableTransactionManagement
@EnableJpaRepositories(
entityManagerFactoryRef = "db2EntityManagerFactory",
transactionManagerRef = "db2TransactionManager",
// 指定该数据源操作的DAO接口包
basePackages = {"com.ljm.db1todb2.db2.repository"})
public class Db2Config {
@Autowired
@Qualifier("db2DataSource")
private DataSource db2DataSource;
@Bean(name = "db2EntityManagerFactory")
public LocalContainerEntityManagerFactoryBean db2EntityManagerFactory(EntityManagerFactoryBuilder builder) {
return builder
.dataSource(db2DataSource)
//设置实体类所在位置
.packages("com.ljm.db1todb2.db2.model")
.persistenceUnit("db2PersistenceUnit")
.build();
}
@Bean(name = "db2TransactionManager")
public PlatformTransactionManager db2TransactionManager(EntityManagerFactoryBuilder builder) {
return new JpaTransactionManager(db2EntityManagerFactory(builder).getObject());
}
}
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。