懒羊羊
2023-08-30 1ac2bc1590406d9babec036e154d8d08f34a6aa1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
package cn.stylefeng.guns.base.db.context;
 
import cn.stylefeng.guns.base.db.collector.SqlSessionFactoryCreator;
import cn.stylefeng.guns.base.db.entity.DatabaseInfo;
import cn.stylefeng.guns.base.db.factory.DruidFactory;
import cn.stylefeng.roses.core.config.properties.DruidProperties;
import cn.stylefeng.roses.core.util.SpringContextHolder;
import org.apache.ibatis.session.SqlSessionFactory;
 
import javax.sql.DataSource;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
 
import static cn.stylefeng.guns.base.db.context.DataSourceContext.MASTER_DATASOURCE_NAME;
 
/**
 * mybatis的sqlSessionFactory的上下文容器(单例)
 *
 * @author fengshuonan
 * @date 2019-06-12-13:37
 */
public class SqlSessionFactoryContext {
 
    private static Map<Object, SqlSessionFactory> sqlSessionFactories = new ConcurrentHashMap<>();
 
    /**
     * 添加sqlSessionFactory
     *
     * @author fengshuonan
     * @Date 2019-06-12 15:28
     */
    public static void addSqlSessionFactory(String name, SqlSessionFactory sqlSessionFactory) {
        sqlSessionFactories.put(name, sqlSessionFactory);
    }
 
    /**
     * 添加sqlSessionFactory
     *
     * @author fengshuonan
     * @Date 2019-06-12 15:28
     */
    public static void addSqlSessionFactory(String name, DatabaseInfo databaseInfo) {
 
        //创建properties
        DruidProperties druidProperties = DruidFactory.createDruidProperties(databaseInfo);
 
        //创建dataSource
        DataSource dataSource = DataSourceContext.createDataSource(name, druidProperties);
        DataSourceContext.addDataSource(name, dataSource);
 
        //创建sqlSessionFactory
        SqlSessionFactoryCreator sqlSessionFactoryCreator = SpringContextHolder.getBean(SqlSessionFactoryCreator.class);
        SqlSessionFactory sqlSessionFactory = sqlSessionFactoryCreator.createSqlSessionFactory(dataSource);
        SqlSessionFactoryContext.addSqlSessionFactory(name, sqlSessionFactory);
 
        sqlSessionFactories.put(name, sqlSessionFactory);
    }
 
    /**
     * 获取所有的sqlSessionFactory
     *
     * @author fengshuonan
     * @Date 2019-06-12 13:49
     */
    public static Map<Object, SqlSessionFactory> getSqlSessionFactorys() {
        return sqlSessionFactories;
    }
 
    /**
     * 初始化数据库中的数据源的SqlSessionFactory
     *
     * @author fengshuonan
     * @Date 2019-06-15 19:51
     */
    public static void initBaseSqlSessionFactory(SqlSessionFactoryCreator sqlSessionFactoryCreator) {
 
        //获取数据库的数据源
        Map<String, DataSource> dataSources = DataSourceContext.getDataSources();
 
        //创建数据库中数据源的sqlSessionFactory
        for (Map.Entry<String, DataSource> entry : dataSources.entrySet()) {
            String dbName = entry.getKey();
            DataSource dataSource = entry.getValue();
 
            //如果是主数据源,跳过,否则会冲突
            if (MASTER_DATASOURCE_NAME.equals(dbName)) {
                continue;
            } else {
                SqlSessionFactory sqlSessionFactory = sqlSessionFactoryCreator.createSqlSessionFactory(dataSource);
                SqlSessionFactoryContext.addSqlSessionFactory(dbName, sqlSessionFactory);
            }
        }
 
    }
 
}