package com.aida.seller.config; import com.baomidou.mybatisplus.core.config.GlobalConfig; import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler; import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor; import com.baomidou.mybatisplus.extension.spring.MybatisSqlSessionFactoryBean; import org.apache.ibatis.session.SqlSessionFactory; import org.mybatis.spring.SqlSessionTemplate; import org.mybatis.spring.annotation.MapperScan; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.boot.jdbc.DataSourceBuilder; import org.springframework.context.ApplicationContext; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Primary; import javax.sql.DataSource; @Configuration @MapperScan(basePackages = "com.aida.seller.module.*.mapper", sqlSessionFactoryRef = "primarySqlSessionFactory") public class PrimaryDataSourceConfig { @Autowired private MybatisPlusInterceptor mybatisPlusInterceptor; @Autowired private MetaObjectHandler myMetaObjectHandler; @Primary @Bean(name = "primaryDataSource") @ConfigurationProperties(prefix = "spring.datasource.primary") public DataSource dataSource() { return DataSourceBuilder.create().build(); } @Primary @Bean(name = "primarySqlSessionFactory") public SqlSessionFactory sqlSessionFactory( @Qualifier("primaryDataSource") DataSource dataSource, ApplicationContext applicationContext) throws Exception { MybatisSqlSessionFactoryBean bean = new MybatisSqlSessionFactoryBean(); bean.setDataSource(dataSource); bean.setMapperLocations(applicationContext.getResources("classpath*:/mapper/**/*.xml")); GlobalConfig globalConfig = new GlobalConfig(); globalConfig.setBanner(false); globalConfig.setMetaObjectHandler(myMetaObjectHandler); bean.setGlobalConfig(globalConfig); bean.setPlugins(mybatisPlusInterceptor); return bean.getObject(); } @Primary @Bean(name = "primarySqlSessionTemplate") public SqlSessionTemplate sqlSessionTemplate( @Qualifier("primarySqlSessionFactory") SqlSessionFactory sqlSessionFactory) { return new SqlSessionTemplate(sqlSessionFactory); } }