liurunyu
2024-10-12 3cf13ad3251d86fa4025e4a248f600c6b56c7e8f
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
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;
 
/**
 * SpringBoot容器启动时,针对数据源,第二步启动本类:
 * 把第一步形成各数据源定义,注入本类对象multiDataSource属性中,
 * 本类生成SQL会话工厂对象,工厂对象持有事务工厂对象,和各数据源(数据库)表的定义(Mapper.xml)
 */
@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 ;
 
    }
}