张晓波
2023-09-19 164694c47c35d6654df69b533e8dbf8b5423efc5
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
package com.thhy.general.config.mysql;
 
import com.thhy.general.common.enums.DataSources;
import com.zaxxer.hikari.HikariDataSource;
import org.apache.ibatis.logging.slf4j.Slf4jImpl;
import org.apache.ibatis.logging.stdout.StdOutImpl;
import org.apache.ibatis.plugin.Interceptor;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.transaction.TransactionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.jdbc.DataSourceBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.transaction.TransactionManager;
import org.springframework.transaction.annotation.EnableTransactionManagement;
 
import javax.sql.DataSource;
import java.util.HashMap;
import java.util.Map;
 
 
@Configuration
@ConditionalOnProperty(prefix = "spring.datasource.hikari",value = "minIdle",havingValue = "10")
@EnableTransactionManagement
public class MysqlConfig {
 
    @Autowired
    private MysqlPrimary mysqlPrimary;
 
    @Autowired
    private MysqlSecondary mysqlSecondary;
 
    @Autowired
    private HikariConfig hikariConfig;
 
    @Autowired
    private SwtichInterceptor swtichInterceptor;
 
    @Bean
    @Primary
    public DataSource dataSourcePrimary() {
        HikariDataSource hikariDataSource = new HikariDataSource();
        hikariDataSource.setMaxLifetime(hikariConfig.getMaxLifeTime());
        hikariDataSource.setMaximumPoolSize(hikariConfig.getMaxPoolSize());
        hikariDataSource.setMinimumIdle(hikariConfig.getMinIdle());
        hikariDataSource.setIdleTimeout(hikariConfig.getIdleTimeOut());
        DataSource dataSource = DataSourceBuilder.create().url(mysqlPrimary.getUrl())
                .driverClassName(mysqlPrimary.getDriverClassName())
                .username(mysqlPrimary.getUsername())
                .password(mysqlPrimary.getPassword()).build();
        hikariDataSource.setDataSource(dataSource);
        return hikariDataSource;
    }
 
    @Bean
    public DataSource dataSourceSecondary() {
        HikariDataSource hikariDataSource = new HikariDataSource();
        hikariDataSource.setMaxLifetime(hikariConfig.getMaxLifeTime());
        hikariDataSource.setMaximumPoolSize(hikariConfig.getMaxPoolSize());
        hikariDataSource.setMinimumIdle(hikariConfig.getMinIdle());
        hikariDataSource.setIdleTimeout(hikariConfig.getIdleTimeOut());
        DataSource dataSource = DataSourceBuilder.create().url(mysqlSecondary.getUrl())
                .driverClassName(mysqlSecondary.getDriverClassName())
                .username(mysqlSecondary.getUsername())
                .password(mysqlSecondary.getPassword()).build();
        hikariDataSource.setDataSource(dataSource);
        return hikariDataSource;
    }
 
 
 
    @Bean
    public DynamicDataSource proxyDataSource(@Qualifier("dataSourcePrimary") DataSource dataSourcePrimary,
                                      @Qualifier("dataSourceSecondary") DataSource dataSourceSecondary) {
        Map<Object, Object> mappedDataSource = new HashMap<>();
        mappedDataSource.put(DataSources.MASTER.name(), dataSourcePrimary);
        mappedDataSource.put(DataSources.SLAVE.name(), dataSourceSecondary);
        DynamicDataSource proxy = new DynamicDataSource();
        proxy.setDefaultTargetDataSource(dataSourcePrimary);
        proxy.setTargetDataSources(mappedDataSource);
        proxy.afterPropertiesSet();
        return proxy;
    }
 
    /*@Bean
    public SqlSessionFactory sqlSessionFactory(DataSource dataSource) throws Exception{
        SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean();
        sqlSessionFactoryBean.setDataSource(dataSource);
        sqlSessionFactoryBean.setTypeAliasesPackage("com.thhy.*.modules.*.*.mapper");
        sqlSessionFactoryBean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath*:mapping/*.xml"));
        SqlSessionFactory sqlSessionFactory = sqlSessionFactoryBean.getObject();
 
        return sqlSessionFactory;
    }*/
 
    @Bean
    public SqlSessionFactory SqlSessionFactory(DynamicDataSource dynamicDataSource)
            throws Exception {
        final SqlSessionFactoryBean sessionFactory = new SqlSessionFactoryBean();
        sessionFactory.setPlugins(new Interceptor[]{swtichInterceptor});
        sessionFactory.setDataSource(dynamicDataSource);
        //sessionFactory.setTransactionFactory();
        org.apache.ibatis.session.Configuration configuration = new org.apache.ibatis.session.Configuration();
        configuration.setLogImpl(StdOutImpl.class);
        configuration.setMapUnderscoreToCamelCase(true);
        sessionFactory.setConfiguration(configuration);
 
        sessionFactory.setMapperLocations(new PathMatchingResourcePatternResolver()
                .getResources("classpath*:mapping/*.xml"));
        return sessionFactory.getObject();
    }
 
    @Bean
    public TransactionManager TransactionManager(DynamicDataSource dynamicDataSource) {
        return new DataSourceTransactionManager(dynamicDataSource);
    }
 
    /*@Bean(name = "priSqlSessionFactory")
    @Primary
    public SqlSessionFactory masterSqlSessionFactory(@Qualifier("dataSourcePrimary") DataSource dataSourcePrimary)
            throws Exception {
        final SqlSessionFactoryBean sessionFactory = new SqlSessionFactoryBean();
        sessionFactory.setPlugins(new Interceptor[]{swtichInterceptor});
        sessionFactory.setDataSource(dataSourcePrimary);
        sessionFactory.setMapperLocations(new PathMatchingResourcePatternResolver()
                .getResources("classpath*:mapping/*.xml"));
        return sessionFactory.getObject();
    }
 
 
    @Bean(name = "secSqlSessionFactory")
    public SqlSessionFactory clusterSqlSessionFactory(@Qualifier("dataSourceSecondary") DataSource dataSourceSecondary)
            throws Exception {
        final SqlSessionFactoryBean sessionFactory = new SqlSessionFactoryBean();
        sessionFactory.setPlugins(new Interceptor[]{swtichInterceptor});
        sessionFactory.setDataSource(dataSourceSecondary);
 
        sessionFactory.setMapperLocations(new PathMatchingResourcePatternResolver()
                .getResources("classpath*:mapping/*.xml"));
        return sessionFactory.getObject();
    }*/
 
 
}