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