當前位置: 首頁>>代碼示例>>Java>>正文


Java AbstractRoutingDataSource類代碼示例

本文整理匯總了Java中org.springframework.jdbc.datasource.lookup.AbstractRoutingDataSource的典型用法代碼示例。如果您正苦於以下問題:Java AbstractRoutingDataSource類的具體用法?Java AbstractRoutingDataSource怎麽用?Java AbstractRoutingDataSource使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。


AbstractRoutingDataSource類屬於org.springframework.jdbc.datasource.lookup包,在下文中一共展示了AbstractRoutingDataSource類的11個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。

示例1: roundRobinDataSouceProxy

import org.springframework.jdbc.datasource.lookup.AbstractRoutingDataSource; //導入依賴的package包/類
/**
 * 有多少個數據源就要配置多少個bean
 * @return
 */
@Bean
public AbstractRoutingDataSource roundRobinDataSouceProxy() {
    int size = Integer.parseInt(dataSourceSize);
    DynamicDataSource proxy = new DynamicDataSource(size);
    Map<Object, Object> targetDataSources = new HashMap<>();
    // 寫
    targetDataSources.put(DataSourceType.write.getType(),dataSource);
    //多個讀數據庫時
    for (int i = 0; i < size; i++) {
        targetDataSources.put(i, readDataSources.get(i));
    }
    proxy.setDefaultTargetDataSource(dataSource);
    proxy.setTargetDataSources(targetDataSources);
    return proxy;
}
 
開發者ID:jinping125,項目名稱:read-write-sever,代碼行數:20,代碼來源:MybatisConfig.java

示例2: roundRobinDataSouceProxy

import org.springframework.jdbc.datasource.lookup.AbstractRoutingDataSource; //導入依賴的package包/類
/** 
 * 有多少個數據源就要配置多少個bean 
 * @return 
 */  
@Bean  
public AbstractRoutingDataSource roundRobinDataSouceProxy() { 
    int size = Integer.parseInt(dataSourceSize);  
    MyAbstractRoutingDataSource proxy = new MyAbstractRoutingDataSource(size);  
    Map<Object, Object> targetDataSources = new HashMap<Object, Object>();  
    // DataSource writeDataSource = SpringContextHolder.getBean("writeDataSource");  
    // 寫  
    targetDataSources.put(DataSourceEnums.write.getType(),dataSource);  
    // targetDataSources.put(DataSourceType.read.getType(),readDataSource);  
    //多個讀數據庫時  
    for (int i = 0; i < size; i++) {  
        targetDataSources.put(i, readDataSources.get(i));  
    }  
    proxy.setDefaultTargetDataSource(dataSource);  
    proxy.setTargetDataSources(targetDataSources);  
    return proxy;  
}
 
開發者ID:duanyaxin,項目名稱:springboot-smart,代碼行數:22,代碼來源:Application.java

示例3: roundRobinDataSouceProxy

import org.springframework.jdbc.datasource.lookup.AbstractRoutingDataSource; //導入依賴的package包/類
/**
 * 有多少個數據源就要配置多少個bean
 * @return
 */
@Bean
public AbstractRoutingDataSource roundRobinDataSouceProxy() {

    int size = Integer.parseInt(dataSourceSize);

    MyAbstractRoutingDataSource proxy = new MyAbstractRoutingDataSource(size);

    Map<Object, Object> targetDataSources = new HashMap<Object, Object>();

    // DataSource writeDataSource = SpringContextHolder.getBean("writeDataSource");
    //設置寫庫
    targetDataSources.put(DataSourceType.write.getType(),dataSource);

    // targetDataSources.put(DataSourceType.read.getType(),readDataSource);
    //設置多讀庫
    for (int i = 0; i < size; i++) {
        targetDataSources.put(i, readDataSources.get(i));
    }

    proxy.setDefaultTargetDataSource(dataSource);
    proxy.setTargetDataSources(targetDataSources);
    return proxy;
}
 
開發者ID:MIYAOW,項目名稱:MI-S,代碼行數:28,代碼來源:MybatisPlusConfig.java

示例4: routingDataSource

import org.springframework.jdbc.datasource.lookup.AbstractRoutingDataSource; //導入依賴的package包/類
/**
 * 注入AbstractRoutingDataSource
 * @param writeDataSource
 * @param read1DataSource
 * @param read2DataSource
 * @return
 * @throws Exception
 */
@Bean
public AbstractRoutingDataSource routingDataSource(
        @Qualifier("writeDataSource") DataSource writeDataSource,
        @Qualifier("read1DataSource") DataSource read1DataSource,
        @Qualifier("read2DataSource") DataSource read2DataSource
) throws Exception {
    DynamicDataSource dataSource = new DynamicDataSource();
    Map<Object, Object> targetDataSources = new HashMap();
    targetDataSources.put(WRITE_DATASOURCE_KEY, writeDataSource);
    targetDataSources.put(READ1_DATASOURCE_KEY, read1DataSource);
    targetDataSources.put(READ2_DATASOURCE_KEY, read2DataSource);
    dataSource.setTargetDataSources(targetDataSources);
    dataSource.setDefaultTargetDataSource(writeDataSource);
    return dataSource;
}
 
開發者ID:Lengchuan,項目名稱:SpringBoot-Study,代碼行數:24,代碼來源:DataSourceConfig.java

示例5: sqlSessionFactory

import org.springframework.jdbc.datasource.lookup.AbstractRoutingDataSource; //導入依賴的package包/類
/**
 * 根據數據源創建SqlSessionFactory
 */
@Bean
public SqlSessionFactory sqlSessionFactory(AbstractRoutingDataSource routingDataSource) throws Exception {
    PathMatchingResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
    SqlSessionFactoryBean factoryBean = new SqlSessionFactoryBean();
    factoryBean.setDataSource(routingDataSource);// 指定數據源(這個必須有,否則報錯)
    // 下邊兩句僅僅用於*.xml文件,如果整個持久層操作不需要使用到xml文件的話(隻用注解就可以搞定),則不加
    factoryBean.setTypeAliasesPackage("com.tangcheng.datasources.aop.model");// 指定基包
    factoryBean.setMapperLocations(resolver.getResources("classpath:mapper/**/*.xml"));//
    return factoryBean.getObject();
}
 
開發者ID:helloworldtang,項目名稱:springboot-multi-datasource,代碼行數:14,代碼來源:MyBatisConfig.java

示例6: afterPropertiesSet

import org.springframework.jdbc.datasource.lookup.AbstractRoutingDataSource; //導入依賴的package包/類
@SuppressWarnings("unchecked")
@Override
public void afterPropertiesSet() {
    super.afterPropertiesSet();
    // 由於父類的resolvedDataSources屬性是私有的子類獲取不到,需要使用反射獲取
    Field field = ReflectionUtils.findField(AbstractRoutingDataSource.class, "resolvedDataSources");
    field.setAccessible(true); // 設置可訪問
    try {
        Map<Object, DataSource> resolvedDataSources = (Map<Object, DataSource>) field.get(this);
        // 讀庫的數據量等於數據源總數減去寫庫的數量
        this.slaveCount = resolvedDataSources.size() - 1;
        for (Map.Entry<Object, DataSource> entry : resolvedDataSources.entrySet()) {
            if (DynamicDataSourceHolder.MASTER.equals(entry.getKey())) {
                continue;
            }
            slaveDataSources.add(entry.getKey());
        }
    } catch (Exception e) {
        LOGGER.error("afterPropertiesSet error! ", e);
    }
}
 
開發者ID:6089555,項目名稱:spring-boot-starter-dynamic-datasource,代碼行數:22,代碼來源:DynamicDataSource.java

示例7: roundRobinDataSouceProxy

import org.springframework.jdbc.datasource.lookup.AbstractRoutingDataSource; //導入依賴的package包/類
/**
 * 有多少個數據源就要配置多少個bean
 * @return
 */
@Bean(name = "roundRobinDataSouceProxy")
public AbstractRoutingDataSource roundRobinDataSouceProxy() {
	int size = Integer.parseInt(dataSourceSize);
	MyAbstractRoutingDataSource proxy = new MyAbstractRoutingDataSource(size);
	Map<Object, Object> targetDataSources = new HashMap<Object, Object>();
	DataSource writeDataSource = SpringContextHolder.getBean("writeDataSource");
	// 寫
	targetDataSources.put(DataSourceType.write.getType(), SpringContextHolder.getBean("writeDataSource"));
	
	for (int i = 0; i < size; i++) {
		targetDataSources.put(i, SpringContextHolder.getBean("readDataSource" + (i + 1)));
	}
	proxy.setDefaultTargetDataSource(writeDataSource);
	proxy.setTargetDataSources(targetDataSources);
	return proxy;
}
 
開發者ID:ggj2010,項目名稱:javabase,代碼行數:21,代碼來源:MybatisConfiguration.java

示例8: getTargetDataSources

import org.springframework.jdbc.datasource.lookup.AbstractRoutingDataSource; //導入依賴的package包/類
@SuppressWarnings("unchecked")
public static Map<Object, Object> getTargetDataSources() {

	try {
		Field field = AbstractRoutingDataSource.class
				.getDeclaredField("targetDataSources");
		field.setAccessible(true);
		return (Map<Object, Object>) field.get(SpringContextUtils
				.getBean("dataSource"));

	} catch (NoSuchFieldException | SecurityException
			| IllegalArgumentException | IllegalAccessException e) {
		e.printStackTrace();
		return null;
	}

}
 
開發者ID:joaquinaimar,項目名稱:wizard,代碼行數:18,代碼來源:DynamicDataSource.java

示例9: routingDataSource

import org.springframework.jdbc.datasource.lookup.AbstractRoutingDataSource; //導入依賴的package包/類
/**
 * 注入AbstractRoutingDataSource
 * @param readDruidDataSource
 * @param writeDruidDataSource
 * @return
 * @throws Exception
 */
@Bean
public AbstractRoutingDataSource routingDataSource(
        @Qualifier(READ_DATASOURCE_KEY) DataSource readDruidDataSource,
        @Qualifier(WRITE_DATASOURCE_KEY) DataSource writeDruidDataSource
) throws Exception {
    DynamicDataSource dataSource = new DynamicDataSource();

    Map<Object, Object> targetDataSources = new HashMap();
    targetDataSources.put(WRITE_DATASOURCE_KEY, writeDruidDataSource);
    targetDataSources.put(READ_DATASOURCE_KEY, readDruidDataSource);
    dataSource.setTargetDataSources(targetDataSources);
    dataSource.setDefaultTargetDataSource(writeDruidDataSource);
    return dataSource;
}
 
開發者ID:Lengchuan,項目名稱:SpringBoot-Study,代碼行數:22,代碼來源:DataSourceConfig.java

示例10: transactionManager

import org.springframework.jdbc.datasource.lookup.AbstractRoutingDataSource; //導入依賴的package包/類
@Bean
public PlatformTransactionManager transactionManager(AbstractRoutingDataSource dataSource) throws Exception {
    return new DataSourceTransactionManager(dataSource);
}
 
開發者ID:helloworldtang,項目名稱:springboot-multi-datasource,代碼行數:5,代碼來源:TxConfig.java

示例11: roundRobinDataSouceProxy

import org.springframework.jdbc.datasource.lookup.AbstractRoutingDataSource; //導入依賴的package包/類
/**
 * See {@literal http://blog.csdn.net/dream_broken/article/details/72851329}
 */
@Bean(name="roundRobinDataSouceProxy")
public AbstractRoutingDataSource roundRobinDataSouceProxy() {

    Map<Object, Object> targetDataSources = new HashMap<>();
    //把所有數據庫都放在targetDataSources中,注意key值要和determineCurrentLookupKey()中代碼寫的一至,
    //否則切換數據源時找不到正確的數據源
    targetDataSources.put(DataSourceContextHolder.DataSourceType.write.getType(), writeDataSource);
    targetDataSources.put(DataSourceContextHolder.DataSourceType.read.getType()+"1", readDataSource01);

    final int readSize = Integer.parseInt(readDataSourceSize);

    //路由類,尋找對應的數據源
    AbstractRoutingDataSource proxy = new AbstractRoutingDataSource(){
        private AtomicInteger count = new AtomicInteger(0);
        /**
         * 這是AbstractRoutingDataSource類中的一個抽象方法,
         * 而它的返回值是你所要用的數據源dataSource的key值,有了這個key值,
         * targetDataSources就從中取出對應的DataSource,如果找不到,就用配置默認的數據源。
         */
        @Override
        protected Object determineCurrentLookupKey() {
            String typeKey = DataSourceContextHolder.getReadOrWrite();

            if(typeKey == null){
                throw new NullPointerException("數據庫路由時,決定使用哪個數據庫源類型不能為空...");
            }

            if (typeKey.equals(DataSourceContextHolder.DataSourceType.write.getType())){
                return DataSourceContextHolder.DataSourceType.write.getType();
            }

            //讀庫, 簡單負載均衡
            int number = count.getAndAdd(1);
            int lookupKey = number % readSize;
            return DataSourceContextHolder.DataSourceType.read.getType()+(lookupKey+1);
        }
    };

    proxy.setDefaultTargetDataSource(writeDataSource);//默認庫
    proxy.setTargetDataSources(targetDataSources);
    return proxy;
}
 
開發者ID:finefuture,項目名稱:data-migration,代碼行數:46,代碼來源:SqlSessionConfiguration.java


注:本文中的org.springframework.jdbc.datasource.lookup.AbstractRoutingDataSource類示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。