yml
spring:
datasource: # 数据库链接
otc:
jdbc-url: jdbc:mysql://x.x.x.x:3306/db1_v1?useUnicode=true&characterEncoding=utf8&useSSL=false&allowPublicKeyRetrieval=true&serverTimezone=GMT%2B8
username: root
password: ENC(0BB79lsvpyt/Vj4fEVfITHOMGu7YuLf0) #数据库名、用户名和密码改为自己的
driver-class-name: com.mysql.cj.jdbc.Driver
mapper-locations: classpath*:mapper/otcmapper/*.xml
db2:
jdbc-url: jdbc:sqlserver://x.x.x.x:1433;DatabaseName=db2_v1
username: root
password: 123456 #数据库名、用户名和密码改为自己的
driver-class-name: com.microsoft.sqlserver.jdbc.SQLServerDriver
mapper-locations: classpath*:mapper/plmmapper/*.xml
配置类
启动类添加注解参数,排除自动配置数据源
@SpringBootApplication(exclude = DataSourceAutoConfiguration.class)
配置第一个数据源
其余的数据源按照以下规则再写配置类即可
import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.core.MybatisConfiguration;
import com.baomidou.mybatisplus.core.config.GlobalConfig;
import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;
import com.baomidou.mybatisplus.extension.spring.MybatisSqlSessionFactoryBean;
import org.apache.ibatis.plugin.Interceptor;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionTemplate;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.jdbc.DataSourceBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.core.io.support.ResourcePatternResolver;
import javax.sql.DataSource;
@Configuration
@MapperScan(basePackages = {"com.batch.otc.otc.mapper"}, sqlSessionFactoryRef = "otcSqlSessionFactory")
public class OtcDataSourceConfig {
@Value("${spring.datasource.otc.mapper-locations}")
private String otcMapperLocation;
@Primary // 表示这个数据源是默认数据源, 这个注解必须要加,因为不加的话spring将分不清楚那个为主数据源(默认数据源)
@Bean("otcDataSource")
@ConfigurationProperties(prefix = "spring.datasource.otc") //读取application.yml中的配置参数映射成为一个对象
public DataSource primaryDataSource(){
return DataSourceBuilder.create().build();
}
@Bean("otcSqlSessionFactory")
public SqlSessionFactory sqlSessionFactory(@Qualifier("otcDataSource") DataSource dataSource) throws Exception {
MybatisSqlSessionFactoryBean sqlSessionFactory = new MybatisSqlSessionFactoryBean();
MybatisConfiguration mybatisConfiguration=new MybatisConfiguration();
// mybatisConfiguration.setLogImpl(org.apache.ibatis.logging.stdout.StdOutImpl.class);//日志打印
mybatisConfiguration.setMapUnderscoreToCamelCase(true);// 驼峰
mybatisConfiguration.setObjectWrapperFactory(new MybatisMapWrapperFactory());// map结果也转驼峰
sqlSessionFactory.setConfiguration(mybatisConfiguration);
sqlSessionFactory.setDataSource(dataSource); //数据源
ResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
sqlSessionFactory.setMapperLocations(resolver.getResources(otcMapperLocation));
sqlSessionFactory.setTypeAliasesPackage("com.batch.otc.otc.entity");
sqlSessionFactory.setGlobalConfig(globalConfig());
sqlSessionFactory.setPlugins(new Interceptor[]{mybatisPlusInterceptor()});
return sqlSessionFactory.getObject();
}
@Bean("otcSqlSessionTemplate")
public SqlSessionTemplate sqlSessionTemplate(@Qualifier("otcSqlSessionFactory") SqlSessionFactory sqlSessionFactory) {
return new SqlSessionTemplate(sqlSessionFactory);
}
public MybatisPlusInterceptor mybatisPlusInterceptor() {
MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL));
return interceptor;
}
public GlobalConfig globalConfig() {
GlobalConfig globalConfig = new GlobalConfig();
GlobalConfig.DbConfig dbConfig = new GlobalConfig.DbConfig();
dbConfig.setLogicDeleteValue("Y");
dbConfig.setLogicNotDeleteValue("N");
globalConfig.setDbConfig(dbConfig);
return globalConfig;
}
}