SpringBoot中雙數據源的配置詳解


前言申明

  springBoot配置雙數據源主要是配置類的書寫和配置文件的書寫,本文采用yml的形式進行配置,mapper和pojo兩個個數據庫都共用一個路徑,dao層分開書寫,各自配置文件中直接配置各自掃描路徑即可。本文只講解如何配置雙數據源,如需配置單數據源的童鞋,可參看該鏈接:https://www.cnblogs.com/zblwyj/p/10668803.html

一、導入jar包

<dependency>
  <groupId>org.mybatis.spring.boot</groupId>
  <artifactId>mybatis-spring-boot-starter</artifactId>
  <version>2.0.0</version>
</dependency

<dependency>
  <groupId>com.alibaba</groupId>
  <artifactId>druid</artifactId>
  <version>1.1.9</version>
</dependency>
<!-- sqlite3驅動包 -->
<dependency>
  <groupId>org.xerial</groupId>
  <artifactId>sqlite-jdbc</artifactId>
</dependency>

二、書寫配置文件View Code

# Tomcat
server:
    tomcat:
        uri-encoding: UTF-8
        max-threads: 1000
        min-spare-threads: 30
    port: 8088
#spring
spring:
    # 指定靜態資源的路徑
    resources:
        static-locations: classpath:/static/,classpath:/views/,file:${web.upload},file:${web.ueditorUpload}
# 數據源一
    datasource:
           master:
             jdbc-url: jdbc:sqlite:D:/java/xy.db
#        jdbc-url: jdbc:sqlite:/usr/java/myfile/xy.db
             driver-class-name: org.sqlite.JDBC
             username: 
             password:
#數據源二
           other:
             driver-class-name: org.sqlite.JDBC
# 方式一:  引用外部文件
             jdbc-url: jdbc:sqlite:D:/system.db
             username: 
             password:
      
                 
# Mybatis配置
#mybatis:
#    mapperLocations: classpath:mapper/**/*.xml
#    configLocation: classpath:/mybatis.xml
    
# sql打印
logging:
    level: debug
    level.com.xuanyin: debug
#    path: logs/
#    file: admin.log
View Code

三、配置主數據源

 

package com.xuanyin.database;
  
import org.apache.ibatis.plugin.Interceptor;
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;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;

import com.github.pagehelper.PageInterceptor;

import java.util.Properties;

import javax.sql.DataSource;
 
/**
 * 主數據源的配置
 * @author Administrator
 *
 */
@Configuration
@Primary
@MapperScan(basePackages ="com.xuanyin.dao", sqlSessionFactoryRef = "masterSqlSessionFactory")
public class MybatisDbMasterConfig {
  
    @Bean(name = "masterDataSource")
    @ConfigurationProperties(prefix = "spring.datasource.master")
    public DataSource dataSource() {
        return DataSourceBuilder.create().build();
    }
  
    @Bean(name = "masterSqlSessionFactory")
    public SqlSessionFactory sqlSessionFactory(@Qualifier("masterDataSource") DataSource dataSource) throws Exception {
        SqlSessionFactoryBean factoryBean = new SqlSessionFactoryBean();
        factoryBean.setDataSource(dataSource);
        factoryBean.setTypeAliasesPackage("com.xuanyin.pojo");
        factoryBean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath:mapper/*.xml"));
        
        //添加PageHelper插件
        Interceptor interceptor = new PageInterceptor();
        Properties properties = new Properties();
        //數據庫
        properties.setProperty("helperDialect", "sqlite");
        //是否將參數offset作為PageNum使用
        properties.setProperty("offsetAsPageNum", "true");
        //是否進行count查詢
        properties.setProperty("rowBoundsWithCount", "true");
        //是否分頁合理化
        properties.setProperty("reasonable", "false");
        interceptor.setProperties(properties);
        factoryBean.setPlugins(new Interceptor[] {interceptor});
        
        
        return factoryBean.getObject();
    }
  
    @Bean(name = "masterTransactionManager")
    public DataSourceTransactionManager transactionManager(@Qualifier("masterDataSource") DataSource dataSource) {
        return new DataSourceTransactionManager(dataSource);
    }
  
    @Bean(name = "masterSqlSessionTemplate")
    public SqlSessionTemplate testSqlSessionTemplate(
            @Qualifier("masterSqlSessionFactory") SqlSessionFactory sqlSessionFactory) throws Exception {
        return new SqlSessionTemplate(sqlSessionFactory);
    }
}

 

四、從數據源的配置

package com.xuanyin.database;
  
import org.apache.ibatis.plugin.Interceptor;
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;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;

import com.github.pagehelper.PageInterceptor;

import java.util.Properties;

import javax.sql.DataSource;
 /**
  * 從數據源的配置
  * @author Administrator
  *
  */
@Configuration
@MapperScan(basePackages = "com.xuanyin.system.dao", sqlSessionFactoryRef = "otherSqlSessionFactory")
public class MybatisDbOtherConfig {
  
    @Bean(name = "otherDataSource")
    @ConfigurationProperties(prefix = "spring.datasource.other")
    public DataSource dataSource() {
        return DataSourceBuilder.create().build();
    }
  
    @Bean(name = "otherTransactionManager")
    public DataSourceTransactionManager transactionManager(@Qualifier("otherDataSource") DataSource dataSource) {
        return new DataSourceTransactionManager(dataSource);
  
    }
  
    @Bean(name = "otherSqlSessionFactory")
    public SqlSessionFactory basicSqlSessionFactory(@Qualifier("otherDataSource") DataSource basicDataSource) throws Exception {
        SqlSessionFactoryBean factoryBean = new SqlSessionFactoryBean();
        factoryBean.setDataSource(basicDataSource);
        factoryBean.setMapperLocations(
                new PathMatchingResourcePatternResolver().getResources("classpath:mapper/*.xml"));
        
        //添加PageHelper插件
        Interceptor interceptor = new PageInterceptor();
        Properties properties = new Properties();
        //數據庫
        properties.setProperty("helperDialect", "sqlite");
        //是否將參數offset作為PageNum使用
        properties.setProperty("offsetAsPageNum", "true");
        //是否進行count查詢
        properties.setProperty("rowBoundsWithCount", "true");
        //是否分頁合理化
        properties.setProperty("reasonable", "false");
        interceptor.setProperties(properties);
        factoryBean.setPlugins(new Interceptor[] {interceptor});
        return factoryBean.getObject();
    }
  
    @Bean(name = "otherSqlSessionTemplate")
    public SqlSessionTemplate testSqlSessionTemplate(
            @Qualifier("otherSqlSessionFactory") SqlSessionFactory sqlSessionFactory) throws Exception {
        return new SqlSessionTemplate(sqlSessionFactory);
    }
}

 

五、調用方法

由於2個數據源dao層位置分開存放,故此,調用時不用做任何操作,直接正常書寫即可。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM