哈哈,遇到一个很久都没有解决的问题,今天灵光一动花了不到半个小时就解决了,好开森~~记录一下
对了,我连接的数据库是mysql+sqlserver~~如果双数据库源都是mysql也可以用以下方式哦~~
先摆上代码~~
1、配置文件:application.yml
spring:
datasource:
primary:
jdbc-url: jdbc:mysql://ip:3306/datasource
username: root
password: 123456
driver-class-name: com.mysql.jdbc.Driver
secondary:
jdbc-url: jdbc:sqlserver://ip:1433;DatabaseName=datasource
username: sa
password: 123456
driver-class-name: com.microsoft.sqlserver.jdbc.SQLServerDriver
jpa:
primary:
database: mysql
generate-ddl: true
show-sql: true
hibernate:
ddl-auto: update
database-platform: org.hibernate.dialect.MySQL5InnoDBDialect
secondary:
database: sql_server
show-sql: false
hibernate:
dialect: org.hibernate.dialect.SQLServer2008Dialect
2、展示我的包结构,建的有一点点乱,总之就是每个数据库要有对应的实体包、dao包,希望能给你一点启发~~
配置文件包
3、以下是主要的类啦~~
3.1 启动类要加上这个两个注解~~~
3.2 DataSourceConfig~~
@Configuration
public class DataSourceConfig {
@Primary
@Bean(name = "primaryDataSource")
@Qualifier("primaryDataSource")
@ConfigurationProperties(prefix = "spring.datasource.primary")
public DataSource primaryDatasource() {
return DataSourceBuilder.create().build();
}
@Bean(name = "secondaryDataSource")
@Qualifier("secondaryDataSource")
@ConfigurationProperties(prefix = "spring.datasource.secondary")
public DataSource secondaryDataSource() {
return DataSourceBuilder.create().build();
}
}
3.3 PrimaryConfig~~~
@Configuration
@EnableTransactionManagement
@EnableJpaRepositories(
entityManagerFactoryRef = "entityManagerFactoryPrimary",//配置连接工厂 entityManagerFactory
transactionManagerRef = "transactionManagerPrimary", //配置 事物管理器 transactionManager
basePackages = {"com.ann.demo.primaryRepository"}//设置持久层所在位置
)
public class PrimaryConfig {
@Autowired
private JpaProperties jpaProperties;
@Autowired
@Qualifier("primaryDataSource")
private DataSource primaryDataSource;// 自动注入配置好的数据源
@Value("${spring.jpa.primary.hibernate.database-platform}")
private String primaryDialect;// 获取对应的数据库方言
@Bean(name = "entityManagerFactoryPrimary")
@Primary
public LocalContainerEntityManagerFactoryBean entityManagerFactoryPrimary(EntityManagerFactoryBuilder builder) {
return builder
//设置数据源
.dataSource(primaryDataSource)
//设置数据源属性
.properties(getVendorProperties(primaryDataSource))
//设置实体类所在位置.扫描所有带有 @Entity 注解的类
.packages("com.ann.demo.normalEntity")
// Spring会将EntityManagerFactory注入到Repository之中.有了 EntityManagerFactory之后,
// Repository就能用它来创建 EntityManager 了,然后 EntityManager 就可以针对数据库执行操作
.persistenceUnit("primaryPersistenceUnit")
.build();
}
private Map<String, String> getVendorProperties(DataSource dataSource) {
Map<String,String> map = new HashMap<>();
map.put("hibernate.dialect",primaryDialect);// 设置对应的数据库方言
//jpaProperties.getHibernate().getNaming().setPhysicalStrategy("org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl");
jpaProperties.setProperties(map);
return jpaProperties.getProperties();
}
/**
* 配置事物管理器
*
* @param builder
* @return
*/
@Bean(name = "transactionManagerPrimary")
@Primary
PlatformTransactionManager transactionManagerPrimary(EntityManagerFactoryBuilder builder) {
return new JpaTransactionManager(entityManagerFactoryPrimary(builder).getObject());
}
}
3.4 SecondaryConfig~~
@Configuration
@EnableTransactionManagement
@EnableJpaRepositories(
entityManagerFactoryRef="entityManagerFactorySecondary",
transactionManagerRef="transactionManagerSecondary",
basePackages= { "com.ann.demo.repository" })
public class SecondaryConfig {
@Autowired
private JpaProperties jpaProperties;
@Autowired
@Qualifier("secondaryDataSource")
private DataSource secondaryDataSource;
@Value("${spring.jpa.secondary.hibernate.dialect}")
private String secondaryDialect;
@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)
.properties(getVendorProperties(secondaryDataSource))
.packages("com.ann.demo.entity")
.persistenceUnit("secondaryPersistenceUnit")
.build();
}
private Map<String, String> getVendorProperties(DataSource dataSource) {
Map<String,String> map = new HashMap<>();
map.put("hibernate.dialect",secondaryDialect);
jpaProperties.setProperties(map);
return jpaProperties.getProperties();
}
@Bean(name = "transactionManagerSecondary")
PlatformTransactionManager transactionManagerSecondary(EntityManagerFactoryBuilder builder) {
return new JpaTransactionManager(entityManagerFactorySecondary(builder).getObject());
}
}
4、其中遇到的问题Failed to get driver instance for jdbcUrl
我的版本是Springboot2.0,所以按照规定配置文件改成jdbcUrl,改了之后在Springboot内置的tomcat启动是成功的!但是我部署到外部的tomcat,会一直报这个错,检查是否有sqlserver跟mysql的驱动包,发现都有啊!今天突然一想,是不是没有配置驱动包!加了这个之后,O了~~~
好开森,所以凡事还是要多尝试~~~~~
喜欢的点个赞 再走呗~~嘻嘻嘻嘻嘻~~~