(2)多个springboot项目合并的问题总结

一,解决项目合并中的多个数据源问题

今天使用spring boot配置两个数据源,出现了问题。

上网搜了一下,结果是使用datasource.url的锅。

spring.datasource.url 数据库的 JDBC URL。

spring.datasource.jdbc-url 用来创建连接的 JDBC URL。

官方文档的解释是:

因为连接池的实际类型没有被公开,所以在您的自定义数据源的元数据中没有生成密钥,而且在IDE中没有完成(因为DataSource接口没有暴露属性)。另外,如果您碰巧在类路径上有Hikari,那么这个基本设置就不起作用了,因为Hikari没有url属性(但是确实有一个jdbcUrl属性)。在这种情况下,您必须重写您的配置如下:

spring.datasource.db1.jdbc-url=jdbc:mysql://193.9.71.47:3306/big_data_platform?useUnicode=true&characterEncoding=utf-8&useSSL=false
spring.datasource.db1.username=root
spring.datasource.db1.password=Grand_403?
spring.datasource.db1.driver-class-name=com.mysql.jdbc.Driver

spring.datasource.db2.jdbc-url=jdbc:mysql://192.168.95.2:3306/app_database?serverTimezone=UTC&useUnicode=true&characterEncoding=utf-8&useSSL=false
spring.datasource.db2.username=root
spring.datasource.db2.password=admin
spring.datasource.db2.driver-class-name=com.mysql.jdbc.Driver

所以如果数据库中只有一个数据源的时候就使用:url

两个以上的数据源时就使用:jdbc-url

二.多数据源的配置实现

我配置了两个数据源,一个名字叫db1数据源,一个名字叫db2数据源,如果你想配置更多的数据源,继续加就行了。

这里我只使用两个数据源,所以要添加两个配置类:

(1)db1数据源的配置: DataSourceConfig.java 

@MapperScan的作用是指定db1这个数据源在项目中的mapper的位置
nameGenerator的作用是如果不同的包下如果出现同名的类则以全路径注册,就不会报错
package com.grand;

import javax.sql.DataSource;
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.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;

//表示这个类为一个配置类
@Configuration
//配置Mybatis的接口类放的地方
@MapperScan(value={"com.grand.p1upgrade.mapper","com.grand.quickreaction.mapper","com.grand.synchronization.db1.mapper","com.grand.springboottemplate.app.mapper","com.grand.springboottemplate.environment.mapper"},sqlSessionFactoryRef = "db1SqlSessionFactory",nameGenerator = UniqueNameGenerator.class)
public class DataSourceConfig {
    // 将这个对象放入Spring容器中
    @Bean(name = "db1")
    // 读取application.properties中的配置参数映射成为一个对象
    // prefix表示参数的前缀
    @ConfigurationProperties(prefix = "spring.datasource.db1")
    // 表示这个数据源是默认数据源
    @Primary
    public DataSource testDataSource() {
        return DataSourceBuilder.create().build();
    }
 
    @Bean(name = "db1SqlSessionFactory")
    // 表示这个数据源是默认数据源
    @Primary
    public SqlSessionFactory testSqlSessionFactory(@Qualifier("db1") DataSource dataSource)
            throws Exception {
        SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
        bean.setDataSource((dataSource));
        // 设置 MyBatis 所需的 xml文件,这里没有使用到 xml,所以注释掉
        // bean.setMapperLocations(
        // new
        // PathMatchingResourcePatternResolver().getResources("classpath:mybatis/mapper/db01/*.xml"));
        return bean.getObject();
    }
 
 
    @Bean(name = "db1TransactionManager")
    // 表示这个数据源是默认数据源
    @Primary
    public DataSourceTransactionManager testTransactionManager(@Qualifier("db1") DataSource dataSource) {
        return new DataSourceTransactionManager(dataSource);
    }
 
    @Bean(name = "db1SqlSessionTemplate")
    // 表示这个数据源是默认数据源
    @Primary
    public SqlSessionTemplate testSqlSessionTemplate(
            @Qualifier("db1SqlSessionFactory") SqlSessionFactory sqlSessionFactory) throws Exception {
        return new SqlSessionTemplate(sqlSessionFactory);
    }

}

(2)db1数据源的配置: DataSource2Config.java 

@MapperScan的作用是指定db2这个数据源在项目中的mapper的位置
nameGenerator的作用是如果不同的包下如果出现同名的类则以全路径注册,就不会报错
package com.grand;

import javax.sql.DataSource;
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.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;

//表示这个类为一个配置类
@Configuration
//配置Mybatis的接口类放的地方
@MapperScan(basePackages = "com.grand.synchronization.db2.mapper", sqlSessionFactoryRef = "db2SqlSessionFactory",nameGenerator = UniqueNameGenerator.class)
public class DataSource2Config {

    // 将这个对象放入Spring容器中
    @Bean(name = "db2")
    // 读取application.properties中的配置参数映射成为一个对象
    // prefix表示参数的前缀
    @ConfigurationProperties(prefix = "spring.datasource.db2")
    public DataSource testDataSource() {
        return DataSourceBuilder.create().build();
    }
 
    @Bean(name = "db2SqlSessionFactory")
    public SqlSessionFactory testSqlSessionFactory(@Qualifier("db2") DataSource dataSource)
            throws Exception {
        SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
        bean.setDataSource((dataSource));
        // 设置 MyBatis 所需的 xml文件,这里没有使用到 xml,所以注释掉
        // bean.setMapperLocations(
        // new
        // PathMatchingResourcePatternResolver().getResources("classpath:mybatis/mapper/db01/*.xml"));
        return bean.getObject();
    }
 
 
    @Bean(name = "db2TransactionManager")
    public DataSourceTransactionManager testTransactionManager(@Qualifier("db2") DataSource dataSource) {
        return new DataSourceTransactionManager(dataSource);
    }
 
    @Bean(name = "db2SqlSessionTemplate")
    public SqlSessionTemplate testSqlSessionTemplate(
            @Qualifier("db2SqlSessionFactory") SqlSessionFactory sqlSessionFactory) throws Exception {
        return new SqlSessionTemplate(sqlSessionFactory);
    }
}

(3)测试结果

原文地址:https://www.cnblogs.com/KdeS/p/13324052.html