1. 测试版本:

id 'org.springframework.boot' version '2.4.5'
implementation 'org.mybatis.spring.boot:mybatis-spring-boot-starter:2.1.4'

2. 在配置文件application.properties中配置两个数据源

spring.datasource.yw.jdbc-url=jdbc:postgresql://localhost:xxxx/xxxx
spring.datasource.yw.driver-class-name=org.postgresql.Driver
spring.datasource.yw.username=xxxx
spring.datasource.yw.password=xxxx

spring.datasource.db2.jdbc-url=jdbc:postgresql://localhost:xxxx/xxxx
spring.datasource.db2.driver-class-name=org.postgresql.Driver
spring.datasource.db2.username=xxxx
spring.datasource.db2.password=xxxx

3. 在Spring Boot入口文件中禁止自动数据库配置

package com.jingxuanqu;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.security.servlet.SecurityAutoConfiguration;

import  org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
import  org.springframework.boot.autoconfigure.jdbc.DataSourceTransactionManagerAutoConfiguration;
import  org.springframework.boot.autoconfigure.jdbc.JdbcTemplateAutoConfiguration;

@SpringBootApplication(
    exclude = {
        SecurityAutoConfiguration.class,
        DataSourceAutoConfiguration.class,
        DataSourceTransactionManagerAutoConfiguration.class,
        JdbcTemplateAutoConfiguration.class
    },
    scanBasePackages = "com.jingxuanqu"
)
public class JxApplication {

        public static void main(String[] args) {
                SpringApplication.run(JxApplication.class, args);
        }

}

4. 增加Mybatis的数据库连接配置文件

以下为一个数据库连接的IoC配置文件,另一个数据库类似。

package com.jingxuanqu;

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
@MapperScan(basePackages = "com.jingxuanqu.goal.mapper", sqlSessionTemplateRef = "jxSqlSessionTemplate")
public class JxDataSourceConfig {


    @Bean(name = "jxDataSource")
    @Primary
    @ConfigurationProperties(prefix = "spring.datasource.jingxuan")
    public DataSource jxDataSource() {
        DataSourceBuilder dataSourceBuilder = DataSourceBuilder.create();
        return dataSourceBuilder.build();
    }

    @Bean("jxSqlSessionFactory")
    @Primary
    public SqlSessionFactory dataSourceFactory(@Qualifier("jxDataSource") DataSource dataSource) throws Exception {
        SqlSessionFactoryBean factoryBean = new SqlSessionFactoryBean();
        factoryBean.setDataSource(jxDataSource());
        return factoryBean.getObject();
    }

    @Bean("jxSqlSessionTemplate")
    @Primary
    public SqlSessionTemplate sqlSessionTemplate(@Qualifier("jxSqlSessionFactory") SqlSessionFactory sessionFactory) {
        return new SqlSessionTemplate(sessionFactory);
    }

    @Bean(name = "jxTransactionManager")
    public DataSourceTransactionManager fawkesTransactionManager(@Qualifier("jxDataSource") DataSource dataSource) {
        return new DataSourceTransactionManager(dataSource);
    }

}

5. 由于MapperScan中指定了package,故其他文件(如Mapper)不需要修改

package com.jingxuanqu.goal.mapper;

import java.util.List;

import com.jingxuanqu.goal.domain.Topic;

import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

@Mapper
public interface TopicMapper {

    @Select("SELECT * FROM topic WHERE id = #{id}")
    Topic getById(@Param("id") Integer id);

}