long
2023-09-08 85b11cfb0f3ffc8a2fbdc2f6fbd26770b1346913
提交 | 用户 | 时间
71e81e 1 /**
2  * Copyright 2018-2020 stylefeng & fengshuonan (sn93@qq.com)
3  * <p>
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  * <p>
8  * http://www.apache.org/licenses/LICENSE-2.0
9  * <p>
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 package cn.stylefeng.guns.config.datasource;
17
18 import cn.stylefeng.guns.base.db.collector.SqlSessionFactoryCreator;
19 import cn.stylefeng.guns.base.db.context.DataSourceContext;
20 import cn.stylefeng.guns.base.db.context.SqlSessionFactoryContext;
21 import cn.stylefeng.guns.base.db.exception.DataSourceInitException;
22 import cn.stylefeng.roses.core.config.properties.DruidProperties;
23 import cn.stylefeng.roses.core.mutidatasource.mybatis.OptionalSqlSessionTemplate;
24 import com.baomidou.mybatisplus.extension.toolkit.SqlHelper;
25 import lombok.extern.slf4j.Slf4j;
26 import org.apache.ibatis.session.SqlSessionFactory;
27 import org.springframework.beans.factory.annotation.Qualifier;
28 import org.springframework.context.annotation.Bean;
29 import org.springframework.context.annotation.Configuration;
30 import org.springframework.context.annotation.Import;
31 import org.springframework.context.annotation.Primary;
32
33 import javax.sql.DataSource;
34
35 import static cn.stylefeng.guns.base.db.context.DataSourceContext.MASTER_DATASOURCE_NAME;
36
37 /**
38  * 多数据源配置<br/>
39  * <p>
40  * 注:由于引入多数据源,所以让spring事务的aop要在多数据源切换aop的后面
41  *
42  * @author stylefeng
43  * @Date 2017/5/20 21:58
44  */
45 @Slf4j
46 @Configuration
47 @Import(SqlSessionFactoryCreator.class)
48 public class SqlSessionFactoryConfig {
49
50     /**
51      * 主sqlSessionFactory
52      */
53     @Primary
54     @Bean
55     public SqlSessionFactory sqlSessionFactoryPrimary(@Qualifier("dataSourcePrimary") DataSource dataSource,
56                                                       SqlSessionFactoryCreator sqlSessionFactoryCreator) {
57         return sqlSessionFactoryCreator.createSqlSessionFactory(dataSource);
58     }
59
60     /**
61      * 多数据源sqlSessionTemplate切换模板
62      */
63     @Bean(name = "gunsSqlSessionTemplate")
64     public OptionalSqlSessionTemplate gunsSqlSessionTemplate(@Qualifier("dataSourcePrimary") DataSource dataSourcePrimary,
65                                                              @Qualifier("sqlSessionFactoryPrimary") SqlSessionFactory sqlSessionFactoryPrimary,
66                                                              SqlSessionFactoryCreator sqlSessionFactoryCreator,
67                                                              DruidProperties druidProperties) {
68         //初始化数据源容器
69         try {
70             DataSourceContext.initDataSource(druidProperties, dataSourcePrimary);
71         } catch (Exception e) {
72             log.error("初始化数据源容器错误!", e);
73             throw new DataSourceInitException(DataSourceInitException.ExEnum.INIT_DATA_SOURCE_ERROR);
74         }
75
76         //添加主数据源的SqlSessionFactory
77         SqlSessionFactoryContext.addSqlSessionFactory(MASTER_DATASOURCE_NAME, sqlSessionFactoryPrimary);
78
79         //初始化其他数据源的SqlSessionFactory的容器
80         SqlSessionFactoryContext.initBaseSqlSessionFactory(sqlSessionFactoryCreator);
81
82         //设置SqlHelper为主数据源
83         SqlHelper.FACTORY = sqlSessionFactoryPrimary;
84
85         return new OptionalSqlSessionTemplate(sqlSessionFactoryPrimary, SqlSessionFactoryContext.getSqlSessionFactorys());
86     }
87
88 }