1.application.properties数据配置
首先现在配置文件中定义三个数据库相关信息
# 数据库1
targetLibraryMain.datasource.url = jdbc:kingbase8://127.0.0.1:54321/DATA_ONE?useUnicode=true&characterEncoding=utf8&serverTimezone=GMT%2B8&allowMultiQueries=true
targetLibraryMain.datasource.driver-class-name = com.kingbase8.Driver
targetLibraryMain.datasource.username = DATA_ONE
targetLibraryMain.datasource.password = 1234567# 数据库2
resourceCenterMain.datasource.url = jdbc:kingbase8://127.0.0.1:54321/DATA_TWO?useUnicode=true&characterEncoding=utf8&serverTimezone=GMT%2B8&allowMultiQueries=true
resourceCenterMain.datasource.driver-class-name = com.kingbase8.Driver
resourceCenterMain.datasource.username = DATA_TWO
resourceCenterMain.datasource.password = 12345678# 数据库3
superviseBusinessMain.datasource.url = jdbc:kingbase8://127.0.0.1:54321/DATA_THREE?useUnicode=true&characterEncoding=utf8&serverTimezone=GMT%2B8&allowMultiQueries=true
superviseBusinessMain.datasource.driver-class-name = com.kingbase8.Driver
superviseBusinessMain.datasource.username = DATA_THREE
superviseBusinessMain.datasource.password = 123456789
2.创建数据库配置类
package xx.config.dataSourceCfg;import xx.utils.DataSrcUtil;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
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.jdbc.datasource.DataSourceTransactionManager;import javax.sql.DataSource;@Configuration
// 扫描 Mapper 接口并容器管理
@MapperScan(basePackages = TargetLibraryMainDataSourceCfg.PACKAGE, sqlSessionFactoryRef = "targetLibraryMainSqlSessionFactory")
public class TargetLibraryMainDataSourceCfg {static final String PACKAGE = "xx.mapper.targetLibraryMain";static final String MAPPER_LOCATION = "classpath:xx/mapper/targetLibraryMain/*.xml";@Value("${targetLibraryMain.datasource.url}")private String url;@Value("${targetLibraryMain.datasource.username}")private String user;@Value("${targetLibraryMain.datasource.password}")private String password;@Value("${targetLibraryMain.datasource.driver-class-name}")private String driverClass;@Bean(name = "targetLibraryMainDataSource")@Primarypublic DataSource targetLibraryMainDataSource() {return DataSrcUtil.getDataSource(driverClass, url, user, password);}@Bean(name = "targetLibraryMainTransactionManager")@Primarypublic DataSourceTransactionManager targetLibraryMainTransactionManager() {DataSourceTransactionManager transactionManager = new DataSourceTransactionManager(this.targetLibraryMainDataSource());return transactionManager;}@Bean(name = "targetLibraryMainSqlSessionFactory")@Primarypublic SqlSessionFactory targetLibraryMainSqlSessionFactory(@Qualifier("targetLibraryMainDataSource") DataSource targetLibraryMainDataSource) throws Exception {final SqlSessionFactoryBean sessionFactory = new SqlSessionFactoryBean();sessionFactory.setDataSource(targetLibraryMainDataSource);sessionFactory.setMapperLocations(new PathMatchingResourcePatternResolver().getResources(TargetLibraryMainDataSourceCfg.MAPPER_LOCATION));// 实体类文件存放地址sessionFactory.setTypeAliasesPackage("xx.pojo");return sessionFactory.getObject();}
}
参考以上文件再创建两个用来对应其他两个数据库信息
3.Application主启动类配置
// 手动配置数据源(exclude = {DataSourceAutoConfiguration.class})
@SpringBootApplication(exclude = {DataSourceAutoConfiguration.class})
public class Application {public static void main(String[] args) {SpringApplication.run(Application.class, args);}
}
然后就可以测试使用啦~