All Downloads are FREE. Search and download functionalities are using the official Maven repository.

matrix.boot.jdbc.beans.MoreDataSource Maven / Gradle / Ivy

The newest version!
package matrix.boot.jdbc.beans;

import matrix.boot.common.exception.ServiceException;
import org.springframework.util.CollectionUtils;

import javax.sql.DataSource;
import java.util.Collection;
import java.util.LinkedHashMap;

/**
 * 数据源集合
 *
 * @author wangcheng
 * 2021/8/16
 **/
public class MoreDataSource {

    /**
     * 数据源集合
     */
    private final LinkedHashMap dataSourceMap;

    public MoreDataSource(LinkedHashMap dataSourceMap) {
        if (CollectionUtils.isEmpty(dataSourceMap)) {
            throw new ServiceException("datasource list is null");
        }
        this.dataSourceMap = dataSourceMap;
    }

    /**
     * 获取数据源字典项
     *
     * @return 数据源字典项
     */
    public LinkedHashMap getDataSourceMap() {
        return this.dataSourceMap;
    }

    /**
     * 获取数据源
     *
     * @param key 键值
     * @return 数据源
     */
    public DataSource getDataSource(String key) {
        return dataSourceMap.get(key);
    }

    /**
     * 获取数据源列表
     *
     * @return 数据源列表
     */
    public Collection getDataSourceList() {
        return this.dataSourceMap.values();
    }

    /**
     * 获取主数据源key
     *
     * @return 主数据源key
     */
    public String getMasterDataSourceKey() {
        return dataSourceMap.entrySet().iterator().next().getKey();
    }

    /**
     * 获取主数据源
     *
     * @return 主数据源
     */
    public DataSource getMasterDataSource() {
        return dataSourceMap.entrySet().iterator().next().getValue();
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy