前言

Spring Boot项目使用Mybatis,既要从上游系统同步数据,又要操作本系统的数据库,所以需要引入双数据源,配置Mybatis

步骤

一、配置双数据源,连接数据库

1、禁用Spring Boot数据源的自动装配,在启动类@SpringBootApplication注解添加exclude = {DataSourceAutoConfiguration.class}

@SpringBootApplication(scanBasePackages = {"com.linkus"}, exclude = {DataSourceAutoConfiguration.class, MybatisAutoConfiguration.class})public class Application {public static void main(String[] args) {SpringApplication.run(Application.class, args);}

2、application.properties配置文件添加两个数据源配置

ora.datasource.driverClassName=oracle.jdbc.OracleDriverora.datasource.url=jdbc:oracle:thin:@ip:port:dbora.datasource.username=xxxora.datasource.password=xxxspring.datasource.driverClassName=org.postgresql.Driverspring.datasource.url=jdbc:postgresql://ip:port/dbspring.datasource.username=xxxspring.datasource.password=xxx

3、添加oracle数据源配置类PrimaryDataSourceConfig,下面的pg数据源配置类DmpDataSourceConfig,添加对应数据源的bean
PrimaryDataSourceConfig

@Primary@Bean(name = "primaryDataSource")@ConfigurationProperties(prefix = "ora.datasource")public DataSource oraDataSource() {return DruidDataSourceBuilder.create().build();}

DmpDataSourceConfig

@Bean(name = "dmpDataSource")@ConfigurationProperties(prefix = "spring.datasource")public DataSource dataSource() {return DruidDataSourceBuilder.create().build();}

两个数据源添加完成

二、配置两个Mybatis

1、禁用Myabtis自动装配,启动类@SpringBootApplication注解添加exclude = {DataSourceAutoConfiguration.class, MybatisAutoConfiguration.class}

2、在application.properties配置文件添加两个Mybatis的配置和对应的配置类

主数据源的mybatis配置

# mybatis mapper.xml路径ora.datasource.extend.mybatisMapperLocations=classpath*:mapper/crm/*.xml

主数据源的配置类

@Configuration@ConfigurationProperties(prefix = "ora.datasource.extend")public class PrimaryMybatisProperties {private String mybatisMapperLocations;public String getMybatisMapperLocations() {return mybatisMapperLocations;}public void setMybatisMapperLocations(String mybatisMapperLocations) {this.mybatisMapperLocations = mybatisMapperLocations;}}

数据源2的配置

# mybatis mapper.xml路径spring.datasource.extend.mybatisMapperLocations=classpath*:mapper/dmp/*.xml

数据源2的配置类

@Configuration@ConfigurationProperties(prefix = "spring.datasource.extend")public class DmpMybatisProperties {private String mybatisMapperLocations;public String getMybatisMapperLocations() {return mybatisMapperLocations;}public void setMybatisMapperLocations(String mybatisMapperLocations) {this.mybatisMapperLocations = mybatisMapperLocations;}}

3、配置两个Mybatis的SqlSessionFactory

@Configuration
@MapperScan(basePackages = {“com.linkus.abp.mapper.crm”}, sqlSessionFactoryRef = “primarySqlSessionFactory”)
@Slf4j
public class PrimaryDataSourceConfig {

@Autowiredprivate PrimaryMybatisProperties properties;@Primary@Bean(name = "primaryDataSource")@ConfigurationProperties(prefix = "ora.datasource")public DataSource oraDataSource() {return DruidDataSourceBuilder.create().build();}@Bean(name = "primarySqlSessionFactory")@Primarypublic SqlSessionFactory sqlSessionFactory(@Qualifier("primaryDataSource") DataSource dataSource) {try {SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean();sqlSessionFactoryBean.setDataSource(dataSource);sqlSessionFactoryBean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources(this.properties.getMybatisMapperLocations()));log.info("config primarySqlSessionFactory success.");return sqlSessionFactoryBean.getObject();} catch (Exception e) {log.error("config primarySqlSessionFactory failed.", e);e.printStackTrace();}return null;}@Bean(name = "primarySqlSessionTemplate")@Primarypublic SqlSessionTemplate sqlSessionTemplate(@Qualifier("primarySqlSessionFactory") SqlSessionFactory sqlSessionFactory) {return new SqlSessionTemplate(sqlSessionFactory);}@Bean(name = "primaryTransactionManager")@Primarypublic DataSourceTransactionManager transactionManager(@Qualifier("primaryDataSource") DataSource dataSource) {return new DataSourceTransactionManager(dataSource);}

}

@Configuration@MapperScan(basePackages = {"com.linkus.abp.mapper.crm"}, sqlSessionFactoryRef = "primarySqlSessionFactory")@Slf4jpublic class PrimaryDataSourceConfig {@Autowiredprivate PrimaryMybatisProperties properties;@Primary@Bean(name = "primaryDataSource")@ConfigurationProperties(prefix = "ora.datasource")public DataSource oraDataSource() {return DruidDataSourceBuilder.create().build();}@Bean(name = "primarySqlSessionFactory")@Primarypublic SqlSessionFactory sqlSessionFactory(@Qualifier("primaryDataSource") DataSource dataSource) {try {SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean();sqlSessionFactoryBean.setDataSource(dataSource);sqlSessionFactoryBean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources(this.properties.getMybatisMapperLocations()));log.info("config primarySqlSessionFactory success.");return sqlSessionFactoryBean.getObject();} catch (Exception e) {log.error("config primarySqlSessionFactory failed.", e);e.printStackTrace();}return null;}@Bean(name = "primarySqlSessionTemplate")@Primarypublic SqlSessionTemplate sqlSessionTemplate(@Qualifier("primarySqlSessionFactory") SqlSessionFactory sqlSessionFactory) {return new SqlSessionTemplate(sqlSessionFactory);}@Bean(name = "primaryTransactionManager")@Primarypublic DataSourceTransactionManager transactionManager(@Qualifier("primaryDataSource") DataSource dataSource) {return new DataSourceTransactionManager(dataSource);}}
@Configuration@MapperScan(basePackages = {"com.linkus.abp.mapper.dmp"}, sqlSessionFactoryRef = "dmpSqlSessionFactory")@Slf4jpublic class DmpDataSourceConfig {@Autowiredprivate DmpMybatisProperties property;@Bean(name = "dmpDataSource")@ConfigurationProperties(prefix = "spring.datasource")public DataSource dataSource() {return DruidDataSourceBuilder.create().build();}@Bean(name = "dmpSqlSessionFactory")public SqlSessionFactory sqlSessionFactory(@Qualifier("dmpDataSource") DataSource dataSource) {try {SqlSessionFactoryBean sessionFactory = new SqlSessionFactoryBean();sessionFactory.setDataSource(dataSource);sessionFactory.setMapperLocations(new PathMatchingResourcePatternResolver().getResources(this.property.getMybatisMapperLocations()));log.info("config dmpSqlSessionFactory success.");return sessionFactory.getObject();} catch (Exception e) {log.error("config dmpSqlSessionFactory failed.", e);e.printStackTrace();}return null;}@Bean(name = "dmpSqlSessionTemplate")public SqlSessionTemplate sqlSessionTemplate(@Qualifier("dmpSqlSessionFactory") SqlSessionFactory sqlSessionFactory) {return new SqlSessionTemplate(sqlSessionFactory);}@Bean(name = "dmpTransactionManager")public DataSourceTransactionManager transactionManager(@Qualifier("dmpDataSource") DataSource dataSource) {return new DataSourceTransactionManager(dataSource);}}@Configuration@MapperScan(basePackages = {"com.linkus.abp.mapper.dmp"}, sqlSessionFactoryRef = "dmpSqlSessionFactory")@Slf4jpublic class DmpDataSourceConfig {@Autowiredprivate DmpMybatisProperties property;@Bean(name = "dmpDataSource")@ConfigurationProperties(prefix = "spring.datasource")public DataSource dataSource() {return DruidDataSourceBuilder.create().build();}@Bean(name = "dmpSqlSessionFactory")public SqlSessionFactory sqlSessionFactory(@Qualifier("dmpDataSource") DataSource dataSource) {try {SqlSessionFactoryBean sessionFactory = new SqlSessionFactoryBean();sessionFactory.setDataSource(dataSource);sessionFactory.setMapperLocations(new PathMatchingResourcePatternResolver().getResources(this.property.getMybatisMapperLocations()));log.info("config dmpSqlSessionFactory success.");return sessionFactory.getObject();} catch (Exception e) {log.error("config dmpSqlSessionFactory failed.", e);e.printStackTrace();}return null;}@Bean(name = "dmpSqlSessionTemplate")public SqlSessionTemplate sqlSessionTemplate(@Qualifier("dmpSqlSessionFactory") SqlSessionFactory sqlSessionFactory) {return new SqlSessionTemplate(sqlSessionFactory);}@Bean(name = "dmpTransactionManager")public DataSourceTransactionManager transactionManager(@Qualifier("dmpDataSource") DataSource dataSource) {return new DataSourceTransactionManager(dataSource);}}@Configuration@MapperScan(basePackages = {"com.linkus.abp.mapper.dmp"}, sqlSessionFactoryRef = "dmpSqlSessionFactory")@Slf4jpublic class DmpDataSourceConfig {@Autowiredprivate DmpMybatisProperties property;@Bean(name = "dmpDataSource")@ConfigurationProperties(prefix = "spring.datasource")public DataSource dataSource() {return DruidDataSourceBuilder.create().build();}@Bean(name = "dmpSqlSessionFactory")public SqlSessionFactory sqlSessionFactory(@Qualifier("dmpDataSource") DataSource dataSource) {try {SqlSessionFactoryBean sessionFactory = new SqlSessionFactoryBean();sessionFactory.setDataSource(dataSource);sessionFactory.setMapperLocations(new PathMatchingResourcePatternResolver().getResources(this.property.getMybatisMapperLocations()));log.info("config dmpSqlSessionFactory success.");return sessionFactory.getObject();} catch (Exception e) {log.error("config dmpSqlSessionFactory failed.", e);e.printStackTrace();}return null;}@Bean(name = "dmpSqlSessionTemplate")public SqlSessionTemplate sqlSessionTemplate(@Qualifier("dmpSqlSessionFactory") SqlSessionFactory sqlSessionFactory) {return new SqlSessionTemplate(sqlSessionFactory);}@Bean(name = "dmpTransactionManager")public DataSourceTransactionManager transactionManager(@Qualifier("dmpDataSource") DataSource dataSource) {return new DataSourceTransactionManager(dataSource);}}

两个Mybatis配置完可以工作了,如果需要分页还要配置两个Mybatis的分页