最近在整理一个SpringBoot+有多数据源的Demo,方言有SQLite和Mysql,SpringBoot使用的版本是<version>1.5.10.RELEASE</version>
,想升级2.0以上的版本,于是出现了一系列版本冲突问题,小问题就不贴了,只贴处理最久的一个Jpa多数据源配置问题,在网上记录了该问题解决方法的鲜有几人。
<version>1.5.10.RELEASE</version>
原代码
DataSourceConfiguration 类
package com.xxxx.core.sqlite.config;
import javax.sql.DataSource;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.jdbc.DataSourceBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.sqlite.SQLiteDataSource;
/**
* 配置sqlite数据库的DataSource
*
*/
@Configuration
public class DataSourceConfiguration {
@Value("${sqlite.dbName}")
private String dbName;
@Bean(destroyMethod = "", name = "EmbeddeddataSource")
public DataSource dataSource() {
DataSourceBuilder dataSourceBuilder = DataSourceBuilder.create();
dataSourceBuilder.driverClassName("org.sqlite.JDBC");
dataSourceBuilder.url("jdbc:sqlite:" + dbName);
dataSourceBuilder.type(SQLiteDataSource.class);
return dataSourceBuilder.build();
}
}
JpaConfiguration 类
package com.xxxx.core.sqlite.config;
import java.util.Map;
import javax.annotation.Resource;
import javax.persistence.EntityManagerFactory;
import javax.sql.DataSource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
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.annotation.EnableTransactionManagement;
@Configuration
@EnableJpaRepositories(basePackages = "com.xxxx.core.repository", transactionManagerRef = "jpaTransactionManager", entityManagerFactoryRef = "localContainerEntityManagerFactoryBean")
@EnableTransactionManagement
public class JpaConfiguration {
@Resource
private JpaProperties jpaProperties;
@Autowired
@Bean
public JpaTransactionManager jpaTransactionManager(@Qualifier(value = "EmbeddeddataSource") DataSource dataSource, EntityManagerFactory entityManagerFactory) {
JpaTransactionManager jpaTransactionManager = new JpaTransactionManager();
jpaTransactionManager.setEntityManagerFactory(entityManagerFactory);
jpaTransactionManager.setDataSource(dataSource);
return jpaTransactionManager;
}
@Autowired
@Bean
LocalContainerEntityManagerFactoryBean localContainerEntityManagerFactoryBean(@Qualifier(value = "EmbeddeddataSource") DataSource dataSource, EntityManagerFactoryBuilder builder) {
return builder.dataSource(dataSource).packages("com.xxxx.core.model").properties(getVendorProperties(dataSource)).build();
}
private Map<String, String> getVendorProperties(DataSource dataSource) {
return jpaProperties.getHibernateProperties(dataSource);
}
}
ModelMetaData 类
package com.xxxx.core.sqlite.config;
import java.util.HashMap;
import java.util.Map;
import javax.persistence.EntityManagerFactory;
import org.hibernate.SessionFactory;
import org.hibernate.metadata.ClassMetadata;
import org.hibernate.persister.entity.AbstractEntityPersister;
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
/**
* 构建一个存放表名和model实体class的对应关系,如member_entity:MemberEntity.class
*
*/
@Configuration
@AutoConfigureAfter(JpaConfiguration.class)
public class ModelMetaData {
@Bean(name = "metaMap")
public Map<String, Class> metaMap(EntityManagerFactory factory) throws ClassNotFoundException {
if (factory.unwrap(SessionFactory.class) == null) {
throw new NullPointerException("factory is not a hibernate factory");
}
SessionFactory sessionFactory = factory.unwrap(SessionFactory.class);
Map<String, ClassMetadata> metaMap = sessionFactory.getAllClassMetadata();
Map<String, Class> map = new HashMap<>(metaMap.size());
for (String key : metaMap.keySet()) {
AbstractEntityPersister classMetadata = (AbstractEntityPersister) metaMap.get