liurunyu
2023-11-04 df37487916ebc1ff8d8e0f8b088c5b6af1e582ff
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
package com.dy.common.multiDataSource;
 
import com.baomidou.mybatisplus.extension.spring.MybatisSqlSessionFactoryBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.Resource;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
 
@Configuration
public class MultiDataSourceConfig {
 
    @Autowired
    private javax.sql.DataSource multiDataSource;
 
    @Bean(name = "sqlSessionFactory")
    public MybatisSqlSessionFactoryBean sqlSessionFactory() throws Exception {
        MybatisSqlSessionFactoryBean bean = new MybatisSqlSessionFactoryBean();
        bean.setTransactionFactory(new MultiDataSourceTransactionFactory());
        bean.setDataSource(multiDataSource);
 
        /**
         * 刘润玉 2023-10-11
         * 因为BasePo不必要了,所以MetaObjectHandler也不需要了,进而MultiDataSourceConfig
         * 中的相关代码也不需要了。
        //mybatisplus 全局配置
        GlobalConfig globalConfig  = new GlobalConfig();
        //配置填充器
        globalConfig.setMetaObjectHandler(new MetaObjectHandler());
        bean.setGlobalConfig(globalConfig);
         */
 
        //设置我们的xml文件路径
        Resource[] resources = new PathMatchingResourcePatternResolver().getResources(
                "classpath*:mapper/*.xml") ;
        bean.setMapperLocations(resources);
        return bean ;
 
    }
}