springboot + mybatis配置多个数据源最简单的方法

springboot + mybatis配置多个数据源最简单的方法

配置文件

application.properties

spring.datasource.driver-class-name=org.postgresql.Driver
spring.datasource.jdbcUrl=jdbc:postgresql://localhost:5432/test
spring.datasource.username=test
spring.datasource.password=test
spring.datasource.type=com.alibaba.druid.pool.DruidDataSource

spring.datasource.oracle.driver-class-name=oracle.jdbc.driver.OracleDriver
#
#格式一:jdbc:oracle:thin:@//:/
#格式二:jdbc:oracle:thin:@:: 
#格式三:jdbc:oracle:thin:@ 
#
spring.datasource.oracle.jdbcUrl=jdbc:oracle:thin:@//localhost:1521/orcl.dev.com
spring.datasource.oracle.username=test
spring.datasource.oracle.password=test
spring.datasource.oracle.type=com.alibaba.druid.pool.DruidDataSource

主数据源

package com.cims.dcar.datasource;

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.core.io.support.PathMatchingResourcePatternResolver;

@Configuration
@MapperScan(basePackages = "com.dao.pg", sqlSessionFactoryRef = "primarySqlSessionFactory")
public class PrimaryDataSourceConfig {
	
    @Bean(name = "primaryDataSource")
    @Primary
    @ConfigurationProperties(prefix = "spring.datasource")
    public DataSource getDateSource() {
    	DataSource dataSource = DataSourceBuilder.create().build();
        return dataSource;
    }

    @Bean(name = "primarySqlSessionFactory")
    @Primary
    public SqlSessionFactory primarySqlSessionFactory(
    		@Qualifier("primaryDataSource") DataSource datasource) throws Exception {
        SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
        bean.setDataSource(datasource);
        bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath:mapping/*.xml"));
        return bean.getObject();
    }

    @Bean("primarySqlSessionTemplate")
    @Primary
    public SqlSessionTemplate primarySqlSessionTemplate(
    		@Qualifier("primarySqlSessionFactory") SqlSessionFactory sessionFactory) {
        return new SqlSessionTemplate(sessionFactory);
    }
}

数据源二

package com.cims.dcar.datasource;

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.core.io.support.PathMatchingResourcePatternResolver;

@Configuration
@MapperScan(basePackages = "com.dao.oracle", sqlSessionFactoryRef = "oracleSqlSessionFactory")
public class OracleDataSourceConfig {

    @Bean(name = "oracleDataSource")
    @ConfigurationProperties(prefix = "spring.datasource.oracle")
    public DataSource getDateSource() {
        DataSource dataSource = DataSourceBuilder.create().build();
        return dataSource;
    }

    @Bean(name = "oracleSqlSessionFactory")
    public SqlSessionFactory oracleSqlSessionFactory(@Qualifier("oracleDataSource") DataSource datasource) throws Exception {
        SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
        bean.setDataSource(datasource);
        bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath*:mapping/oracle/*.xml"));
        return bean.getObject();
    }

    @Bean("oracleSqlSessionTemplate")
    public SqlSessionTemplate oracleSqlSessionTemplate(@Qualifier("oracleSqlSessionFactory") SqlSessionFactory sessionFactory) {
        return new SqlSessionTemplate(sessionFactory);
    }
}

多加个依赖

@ConfigurationProperties注解需要

<dependency>
     <groupId>org.springframework.bootgroupId>
     <artifactId>spring-boot-configuration-processorartifactId>
     <optional>trueoptional>
dependency>

需要注意的问题

1、 java.lang.IllegalArgumentException: jdbcUrl is required with driverClassName.
spring.datasource.url后缀url需改为jdbcUrl(spring.datasource.jdbcUrl)

2、两个数据源的dao包要分开,包不能是同一个包或包含关系,比如(@MapperScan(basePackages = “com.dao”) ,@MapperScan(basePackages = “com.dao.oracle”))本地运行可能不会出现问题,但部署docker运行则会出现Invalid bound statement (not found):错误

你可能感兴趣的:(Java)