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 ; } }