Commit 0a618b6d by gechengyang

整合多数据基础版本,待完善。

parent e2d1815b
package com.brilliance.mda.support;
public class mybatis {
}
package com.brilliance.mda.util;
import com.brilliance.mda.runtime.mda.util.MdaUtils;
/**
* 数据源切换工具
*/
public class DataSourceUtil {
/**
* 默认数据源
*/
public static final String DEFAULT_DS = "datasource";
/**
* 数据源属于一个公共的资源
* 采用可以保证在多线程情况下线程隔离
*/
private static final ThreadLocal<String> contextHolder = new ThreadLocal<>();
/**
* 设置数据源名
* @param dbType
*/
public static void setDB(String dbType) {
contextHolder.set(dbType);
}
/**
* 获取数据源名
* @return
*/
public static String getDB() {
String dbName= (contextHolder.get()) ;
if(MdaUtils.isEmpty(dbName))
return DEFAULT_DS;
return dbName;
}
/**
* 清除数据源名
*/
public static void clearDB() {
contextHolder.remove();
}
}
package com.ceb.gjjs.mda.config;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.SqlSessionTemplate;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.jdbc.DataSourceBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.transaction.PlatformTransactionManager;
import javax.sql.DataSource;
import java.util.HashMap;
import java.util.Map;
/**
* 数据库配置
*/
@Configuration
public class DataSourceConfig {
/**
* 数据源1
* spring.datasource.db1:application.properteis中对应属性的前缀
* @return
*/
@Bean(name = "dataSource")
@ConfigurationProperties(prefix = "spring.datasource.druid")
// @Bean(name = "db1")
// @ConfigurationProperties(prefix = "spring.datasource.db1")
public DataSource dataSourceOne() {
return DataSourceBuilder.create().build();
}
/**
* 数据源2
* spring.datasource.db2:application.properteis中对应属性的前缀
* @return
*/
//@Bean(name = "db2")
//@ConfigurationProperties(prefix = "spring.datasource.db2")
@Bean(name = "dataSource2")
@ConfigurationProperties(prefix = "spring.datasource2")
public DataSource dataSourceTwo() {
return DataSourceBuilder.create().build();
}
/**
* 动态数据源: 通过AOP在不同数据源之间动态切换
* @return
*/
@Primary
@Bean(name = "dynamicDataSource")
public DataSource dynamicDataSource() {
DynamicDataSource dynamicDataSource = new DynamicDataSource();
// 默认数据源
dynamicDataSource.setDefaultTargetDataSource(dataSourceOne());
// 配置多数据源
Map<Object, Object> dsMap = new HashMap<>();
dsMap.put("dataSource", dataSourceOne());
dsMap.put("dataSource2", dataSourceTwo());
dynamicDataSource.setTargetDataSources(dsMap);
return dynamicDataSource;
}
/**
* 配置多数据源后IOC中存在多个数据源了,事务管理器需要重新配置,不然器不知道选择哪个数据源
* 事务管理器此时管理的数据源将是动态数据源dynamicDataSource
* 配置@Transactional注解
* @return
*/
@Bean
public PlatformTransactionManager transactionManager() {
return new DataSourceTransactionManager(dynamicDataSource());
}
@Bean(name = "sqlSessionFactory1")
@Primary
public SqlSessionFactory getSqlSessionFactory1(@Qualifier("dataSource") DataSource dataSource) throws Exception{
SqlSessionFactoryBean sqlfb = new SqlSessionFactoryBean();
sqlfb.setDataSource(dataSource);
// sqlfb.setConfigLocation(new ClassPathResource("mybatis-config.xml"));
sqlfb.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath:META-INF/orm/mda/*.xml"));
return sqlfb.getObject();
}
@Bean(name = "transactionManager")
@Primary
public DataSourceTransactionManager getTransactionManager1(@Qualifier("dataSource")DataSource dataSource) {
return new DataSourceTransactionManager(dataSource);
}
@Bean(name = "sqlSessionTemplate1")
@Primary
public SqlSessionTemplate getSqlSessionTemplate1(@Qualifier("sqlSessionFactory1")SqlSessionFactory sqlSessionFactory) throws Exception {
return new SqlSessionTemplate(sqlSessionFactory);
}
//-----------------------------------------
@Bean(name = "sqlSessionFactory2")
public SqlSessionFactory getSqlSessionFactory2(@Qualifier("dataSource2") DataSource dataSource) throws Exception{
SqlSessionFactoryBean sqlfb = new SqlSessionFactoryBean();
sqlfb.setDataSource(dataSource);
// sqlfb.setConfigLocation(new ClassPathResource("mybatis-config.xml"));
sqlfb.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath:META-INF/orm/mda/*.xml"));
return sqlfb.getObject();
}
@Bean(name = "transactionManager2")
public DataSourceTransactionManager getTransactionManager2(@Qualifier("dataSource2")DataSource dataSource) {
return new DataSourceTransactionManager(dataSource);
}
@Bean(name = "sqlSessionTemplate2")
public SqlSessionTemplate getSqlSessionTemplate2(@Qualifier("sqlSessionFactory2")SqlSessionFactory sqlSessionFactory) throws Exception {
return new SqlSessionTemplate(sqlSessionFactory);
}
}
package com.ceb.gjjs.mda.config;
import com.brilliance.mda.util.DataSourceUtil;
import org.springframework.jdbc.datasource.lookup.AbstractRoutingDataSource;
/**
* 动态数据源类
*/
public class DynamicDataSource extends AbstractRoutingDataSource {
@Override
protected Object determineCurrentLookupKey() {
return DataSourceUtil.getDB();
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment