1. 程式人生 > >SpringBoot分表分庫配置多資料來源

SpringBoot分表分庫配置多資料來源





1、背景的介紹

有時候我們需要做分庫分表,那麼肯定存在多資料來源,Spring Boot和Mybatis的多資料來源是如何整合的呢?比如說我們現在做了一個浪跡天涯管理的後臺系統,商品資訊是存在itemCenter資料來源中的,而與使用者相關的資訊是存在account資料來源中,專案結構如下:


2、Spring Boot和Mybatis的整合

2.1 新增相關的pom檔案

這個我不在這裡詳細的貼程式碼,稍後請檢視具體的原始碼。

2.2 在環境配置檔案中新增資料庫的配置


因為我們這裡有四個環境,分別是dev、sit、ust、prod。所以我們需要在每個環境下配置jdbc.properties。並且我們在jdbc.properties配置檔案中配置了2個數據源,分別是itemcenter和account。


2.3 資料來源的讀取和事物的配置以及Mybatis的配置

因為這些都是通用的,所以我們放在了common這個檔案下統一配置了,如下圖所示:


(1). 現在我們具體的看下dataSource.xml的配置:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd"
> <aop:aspectj-autoproxy proxy-target-class="false"/> <bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSource" /> </bean> <tx:annotation-driven transaction-manager="txManager" /> <bean id="itemcenterDataSource" class="com.alibaba.druid.pool.DruidDataSource" destroy-method="close"> <property name="url" value="${itemcenter.jdbc.url}" /> <property name="username" value="${itemcenter.jdbc.username}" /> <property name="password" value="${itemcenter.jdbc.password}" /> <property name="driverClassName" value="${itemcenter.jdbc.driver}" /> </bean> <bean id="accountDataSource" class="com.alibaba.druid.pool.DruidDataSource" destroy-method="close"> <property name="url" value="${account.jdbc.url}" /> <property name="username" value="${account.jdbc.username}" /> <property name="password" value="${account.jdbc.password}" /> <property name="driverClassName" value="${account.jdbc.driver}" /> </bean> <bean id ="dataSource" class= "com.niepengfei.langjitianya.config.DynamicDataSource" > <property name ="targetDataSources"> <map key-type ="java.lang.String"> <entry value-ref ="itemcenterDataSource" key= "itemcenter_dataSource"/> <entry value-ref ="accountDataSource" key= "account_dataSource"/> </map> </property> </bean> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="configLocation" value="classpath:config/common/mybatisConfig.xml"/> <property name="dataSource" ref="dataSource"/> </bean> </beans>

(2). 現在我們具體的看下mybatisConfig.xml的配置:

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>

        <!-- 配置分頁外掛 -->
    <plugins>
        <plugin interceptor="com.github.pagehelper.PageHelper">
            <!-- 設定資料庫型別 Oracle,Mysql,MariaDB,SQLite,Hsqldb,PostgreSQL六種資料庫-->        
            <property name="dialect" value="mysql"/>
        </plugin>
    </plugins>

</configuration>

dataSource.xml是讀取資料來源,且mybatisConfig.xml是在dataSource.xml被載入。

2.4 開發dao

我們把dao和相應的xml檔案放在一起,約定俗成。這樣省去了配置dao和xml的對應關係。


2.5 啟動類的配置


2.6 我們的資料來源的切換是在service層,這裡我使用Spring AOP的思想,利用切面在Service層切換資料來源。

(1).首先定義資料來源的列舉


(2)、然後分別為每個資料來源定義個註解,例如AccountDatasource和ItemCenterDataSource,程式碼如下:


(3).定義DynamicDataSource,該類繼承AbstractRoutingDataSource,重寫determineCurrentLookupKey:


(4)、定義資料來源切面:


(5)、在Service層加上相應的資料來源註解


以上就是全部過程。



相關推薦

SpringBoot分庫配置資料來源

1、背景的介紹有時候我們需要做分庫分表,那麼肯定存在多資料來源,Spring Boot和Mybatis的多資料來源是如何整合的呢?比如

springboot使用mybatis如何配置資料來源

本篇部落格提供一個工程想連線多個數據庫進行業務查詢,在原先的單個數據庫上進行配置實現多個數據庫的使用,對於springboot以及mybatis不在此進行展開介紹,只需要把程式碼按照步驟一步步貼上進你的專案,調整一下就能實現; 簡單介紹單個數據源配置 pom檔案匯入依賴 <

springboot配置資料來源之Spring Date JPA

多資料來源在專案開發中是經常遇到的,如果同一個專案的不同模組使用的是不同資料庫,就需要多資料來源的處理。現在先寫之前使用JPA的時候遇到多資料來源的配置,後續可能再來個關於mybatis的多資料來源配置。 現在有這樣的需求,專案中有兩個模組,分別是flow與imap,flow需要使用預設資料

springboot整合Mybatis配置資料來源

springboot配置多資料來源有好幾種方式 1.application.properties配置 ## 埠 server.port=8080 # 資料庫訪問配置 spring.datasource.type=com.alibaba.druid.pool.DruidDataSource spri

springboot配置資料來源(MongoDB主從)

相信看過上一篇文章的小夥伴已經知道了, 這章要講的就是MongoDB主從配置。 在這邊文章中,你將要學到的是在專案中配置主從資料庫,並且相容其他資料庫喲。。這些都是博主專案中需要並且比較重要的知識哦~ 好了,廢話不多說,直接進主題。 1.pom依賴 <depende

springboot配置資料來源(不同DB)

springBoot整合Mysql+MongoDB 因為在專案中需要用到兩個不同的資料來源。但是又不存在於一個DB中。讓我很是苦惱,不得已只能整合多資料來源。 博主文筆不好,只能講乾貨了。。 目標: 使用springBoot整合mys

springboot+MybatisPlus配置資料來源+aop切面

DataSource.java package com.ocean.dataSourceConfig; import java.lang.annotation.*; /** * Created with IDEA * author:QinWei * Date:2018/12/11 *

手把手教你用springboot配置資料來源

<project xmlns="http://maven.apache.org/POM/4.0.0"     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"     xsi:schemaLocation="http://maven.apache

SpringBoot配置資料來源(druid)

分析 spring本身是支援多資料來源動態切換的,AbstractRoutingDataSource這個抽象類就是spring提供的一個數據源路由的一個入口,該抽象類暴露了一個determineCurrentLookupKey()的方法,該方法返回值是Object,該返回值作為key

SpringBoot學習筆記---配置資料來源(Mybatis )

介紹 為什麼配置多資料來源,因為在實際業務需求中 資料的來源可能不是一個數據庫中可進行配置兩套資料來源就可以避免跨庫查詢 pom檔案需要引入的依賴 <dependency> <groupId>org.mybatis.spring.boot</gr

springboot+jpa配置資料來源

功能情況: 實現系統對多資料來源的操作。 實現系統對多資料來源的分散式事務管理,包括事務的提交和回滾。 1、建立資料庫配置檔案 #預設資料庫,必須配置,且字首必須為spring.datasource.primary spring.datasource.prima

springboot+mybatis配置資料來源(MySQL+SQLServer)

1.應用場景        什麼時候才用的到配置多資料來源??當我們用不同資料庫存放不同資料的時候。這個時候我們整個工程並不是只用一個數據庫,所以,要配置多個,在具體的場景應用具體的資料來源。 2.檔案結構 3.主要思路 假設現在有兩類人,需要登入,在不同的登入頁

springboot v2.0.3版本資料來源配置

本篇分享的是springboot多資料來源配置,在從springboot v1.5版本升級到v2.0.3時,發現之前寫的多資料來源的方式不可用了,捕獲錯誤資訊如: 異常:jdbcUrl is required with driverClassName. 先來說下之前的多資料來源配置如: 1

SpringBoot配置資料來源(MySQL+SQLServer)

SpringBoot 版本 1.5.13.RELEASE,以下maven依賴中包含jpa、web、mysql驅動、sqlserver驅動、測試模組。(按需新增) <parent> <groupId>org.springframework.

SpringBoot整合MybatisPlus配置資料來源

首先建立SpringBoot專案,匯入web模組; 匯入依賴: <!--aop--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId

springboot配置資料來源並整合Druid

1.application.properties配置檔案 spring.datasource.type = com.alibaba.druid.pool.DruidDataSource #----DS1---- spring.datasource.primary.u

springboot配置資料來源,註解操作資料庫

最近新搭建了一個專案,需要去不同的資料庫中查詢資料,需要多個數據源,在網上搜索了下,基本上實現都很複雜,下面我自己實現了一個很簡單的配置方法。 1、原來我們都是在application.yml檔案中配置資料來源,現在不需要在application.yml檔案中配置了。   &n

springboot+hibernate(jpa)配置資料來源

 個人在配置完spring boot多資料來源後報錯!仔細看了報錯的資訊提示建立從資料來源bean失敗,最後還是因為自己不仔細造成的,從資料庫少建立一張表。廢話不說直接貼程式碼! pom.xml 檔案就不上了,正常的依賴包。 application.properties 配置資訊: #

springboot 配置資料來源

1.首先在建立應用物件時引入autoConfig @ComponentScan @EnableAutoConfiguration public class Application extends SpringBootServletInitializer { public

SpringBoot+Mybatis+ Druid+PageHelper 實現資料來源

前言 本篇文章主要講述的是SpringBoot整合Mybatis、Druid和PageHelper 並實現多資料來源和分頁。其中SpringBoot整合Mybatis這塊,在之前的的一篇文章中已經講述了,這裡就不過多說明了。重點是講述在多資料來源下的如何配置使用Druid和PageHelper 。 Druid