1. 程式人生 > >springboot+mybatis配置多資料來源

springboot+mybatis配置多資料來源

1、首先你自己實現了單資料來源。

2、然後在application.properties中配置第二資料來源

#第一資料來源
spring.datasource.db_medical.url=jdbc:mysql://192.168.1.70:3306/db_medical
spring.datasource.db_medical.username=root
spring.datasource.db_medical.password=123456
spring.datasource.db_medical.driver-class-name=com.mysql.jdbc.Driver

#第二資料來源
spring.datasource.kdys.url=jdbc:mysql://192.168.1.70:3306/kdys spring.datasource.kdys.username=root spring.datasource.kdys.password=123456 spring.datasource.kdys.driver-class-name=com.mysql.jdbc.Driver

3、新建datasource包,建立DataSource1Config(主資料來源)

package com.gdysh.account.common.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.autoconfigure.jdbc.DataSourceBuilder; import org.springframework.boot.context.properties.ConfigurationProperties; 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; /** * Created by summer on 2016/11/25. */ @Configuration //mapper檔案路徑 @MapperScan(basePackages = "com.gdysh.account.mapper.db_medical", sqlSessionTemplateRef = "db_medicalSqlSessionTemplate") public class DataSource1Config { @Bean(name = "db_medicalDataSource") @ConfigurationProperties(prefix = "spring.datasource.db_medical") @Primary //這個註解是說明此是預設資料來源,必須設定預設資料來源。(即主資料來源) public DataSource testDataSource() { return DataSourceBuilder.create().build(); } @Bean(name = "db_medicalSqlSessionFactory") @Primary public SqlSessionFactory testSqlSessionFactory(@Qualifier("db_medicalDataSource") DataSource dataSource) throws Exception { SqlSessionFactoryBean bean = new SqlSessionFactoryBean(); bean.setDataSource(dataSource); //xml路徑 bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath*:com/yilife/mapper/db_medical/*.xml")); return bean.getObject(); } @Bean(name = "db_medicalTransactionManager") @Primary public DataSourceTransactionManager testTransactionManager(@Qualifier("db_medicalDataSource") DataSource dataSource) { return new DataSourceTransactionManager(dataSource); } @Bean(name = "db_medicalSqlSessionTemplate") @Primary public SqlSessionTemplate testSqlSessionTemplate(@Qualifier("db_medicalSqlSessionFactory") SqlSessionFactory sqlSessionFactory) throws Exception { return new SqlSessionTemplate(sqlSessionFactory); } }

4、然後在同個包建立第二資料來源DataSource2Config

package com.gdysh.account.common.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.autoconfigure.jdbc.DataSourceBuilder;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;

import javax.sql.DataSource;

@Configuration
@MapperScan(basePackages = "com.gdysh.account.mapper.kdys", sqlSessionTemplateRef  = "kdysSqlSessionTemplate")
public class DataSource2Config {

    @Bean(name = "kdysDataSource")
    @ConfigurationProperties(prefix = "spring.datasource.kdys")

    public DataSource kdysDataSource() {
        return DataSourceBuilder.create().build();
    }

    @Bean(name = "kdysSqlSessionFactory")

    public SqlSessionFactory kdysSqlSessionFactory(@Qualifier("kdysDataSource") DataSource dataSource) throws Exception {
        SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
        bean.setDataSource(dataSource);
        bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath*:com/gdysh/account/mapper/kdys/*.xml"));
        return bean.getObject();
    }

    @Bean(name = "kdysTransactionManager")

    public DataSourceTransactionManager kdysTransactionManager(@Qualifier("kdysDataSource") DataSource dataSource) {
        return new DataSourceTransactionManager(dataSource);
    }

    @Bean(name = "kdysSqlSessionTemplate")

    public SqlSessionTemplate kdysSqlSessionTemplate(@Qualifier("kdysSqlSessionFactory") SqlSessionFactory sqlSessionFactory) throws Exception {
        return new SqlSessionTemplate(sqlSessionFactory);
    }

}

5、然後你把這2個類相關的路徑和名稱改成自己的即可。其實第二資料來源相關的東西(實體類等等)仿照著第一資料來源的寫就可以了。這裡需要注意的是,2個數據源對應的dao層不能在同個包中,否則出錯,以及非主資料來源不能有@Primary 註解