Configuration class of master data source: MySQL is used for the database.
package com.juqent.cps.system.config;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.SqlSessionTemplate;
import org.mybatis.spring.annotation.MapperScan;
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.*;
import javax.sql.DataSource;
@Configuration
//this is what you need to scan using the master data source mapper the path to the interface file
@MapperScan(basePackages = "com.juqent.cps.**.mapper", sqlSessionTemplateRef = "sqlSessionTemplatePrimary")
public class PrimaryDataSourceConfig {
@Primary
@Bean(name = "primaryDataSource")
//@ConfigurationProperties(prefix = "spring.datasource.dynamic.druid.datasource.master")
public DataSource dataSource() {
return DataSourceBuilder.create().driverClassName("com.mysql.cj.jdbc.Driver")
.url("jdbc:mysql://192.168.0.157:3306/data?useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=true&serverTimezone=GMT%2B8&allowMultiQueries=true")
.username("root")
.password("123456").build();
}
@Primary
@Bean(name = "sqlSessionFactoryPrimary")
public SqlSessionFactory sqlSessionFactory(@Qualifier("primaryDataSource") DataSource dataSource, ApplicationContext applicationContext) throws Exception {
SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean();
sqlSessionFactoryBean.setDataSource(dataSource);
//applicationContext.getResources( "classpath*:**/mapper/*/*.xml")this path is what you want to map XML files, i.e mapper corresponding to the interface ...Mapper.xml the path to the file
sqlSessionFactoryBean.setMapperLocations(applicationContext.getResources( "classpath*:**/mapper/*/*.xml"));
return sqlSessionFactoryBean.getObject();
}
@Primary
@Bean(name = "sqlSessionTemplatePrimary")
public SqlSessionTemplate sqlSessionTemplate(@Qualifier("sqlSessionFactoryPrimary") SqlSessionFactory sqlSessionFactory) throws Exception {
return new SqlSessionTemplate(sqlSessionFactory);
}
}
The configuration file for the second data source: The database used is SQLServer.
package com.juqent.cps.system.config;
import com.juqent.cps.base.syn.SynchronizationMaterialTypeMapper;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.SqlSessionTemplate;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.jdbc.DataSourceBuilder;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import javax.sql.DataSource;
@Configuration
//this mapper i did not include the second data source in the path to prevent the main data source from scanning it mapper catalog placement mapper under the directory, i placed it syn under the directory.
@MapperScan(basePackages = "com.juqent.cps.base.**.syn", sqlSessionTemplateRef = "sqlSessionTemplateSecondary")
public class SecondaryDataSourceConfig {
@Bean(name = "secondaryDataSource")
//@ConfigurationProperties(prefix = "mybatis.datasource.secondary")
public DataSource dataSource() {
return DataSourceBuilder.create().driverClassName("com.microsoft.sqlserver.jdbc.SQLServerDriver")
.url("jdbc:sqlserver://192.168.0.158:1433;databaseName=UFDATA_001_2018")
.username("sa")
.password("SA123sajuqent").build();
}
@Bean(name = "secondarySqlSessionFactory")
public SqlSessionFactory sqlSessionFactory(@Qualifier("secondaryDataSource") DataSource dataSource,
ApplicationContext applicationContext) throws Exception {
SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean();
sqlSessionFactoryBean.setDataSource(dataSource);
//this xml i am on the path mapper an additional layer called syn under the directory of
sqlSessionFactoryBean.setMapperLocations(applicationContext.getResources("classpath*:mapper/base/syn/*Mapper.xml"));
return sqlSessionFactoryBean.getObject();
}
@Bean(name = "sqlSessionTemplateSecondary")
public SqlSessionTemplate sqlSessionTemplate(@Qualifier("secondarySqlSessionFactory") SqlSessionFactory sqlSessionFactory) throws Exception {
return new SqlSessionTemplate(sqlSessionFactory);
}
}
If you don't want to write the database account and password into the code, you can actually use this annotation.
@ConfigurationProperties(prefix = "mybatis.datasource.secondary")
The path below the annotation is the configuration path of your data source in the configuration file.
The directory structure is the path to two data source configuration files.



As for the dependencies in pom, I won't show them one by one. MySQL is relatively simple, but my second data source uses sqlServer, so it is necessary to include the driver dependencies of SqlServer.
<dependency>
<groupId>com.microsoft.sqlserver</groupId>
<artifactId>mssql-jdbc</artifactId>
<version>8.4.1.jre8</version> <! --the version number may need to be adjusted according to the actual situation-- >
</dependency>