1. 程式人生 > >jeesite專案使用-多資料庫配置【Spring mvc + Mybatis】

jeesite專案使用-多資料庫配置【Spring mvc + Mybatis】

jeesite開源專案使用-多資料庫配置【Spring + Mybatis】

配置DataSource多資料來源
使用場景:
同一個專案涉及多個數據庫,既多資料來源的情況。一般有兩種情況: 1。兩個資料庫沒有關係,各自獨立,只是獨立使用,並不相互使用。【我沒有遇到這情況】 2。兩個資料庫,有相關性的,比如:主從master-slave。【我遇到的情況,oracle資料庫,一個是主資料庫,另一個是備份資料庫只允許做查詢】 【這兩種解決的方法也是有對應的兩種】【推薦使用,我是用這種的╮(╯_╰)╭】

1.採用直接配置多個數據庫。

在spring裡面直接配置兩個資料庫的資訊,兩個資料庫,兩個事務,兩個對映注入。【兩套各自獨立的東東】 所需要修改的配置:【spring-context.xml
<context:component-scanbase-package="net.aazj.service,net.aazj.aop"/><context:component-scanbase-package="net.aazj.aop"/><!-- 引入屬性檔案 --><context:property-placeholderlocation="classpath:config/db.properties"/><!-- 配置資料來源 --><beanname="dataSource"class="com.alibaba.druid.pool.DruidDataSource"
init-method="init"destroy-method="close">
<propertyname="url"value="${jdbc_url}"/><propertyname="username"value="${jdbc_username}"/><propertyname="password"value="${jdbc_password}"/><!-- 初始化連線大小 --><propertyname="initialSize"value="0"/><!-- 連線池最大使用連線數量 --><property
name="maxActive"value="20"/>
<!-- 連線池最大空閒 --><propertyname="maxIdle"value="20"/><!-- 連線池最小空閒 --><propertyname="minIdle"value="0"/><!-- 獲取連線最大等待時間 --><propertyname="maxWait"value="60000"/></bean><beanid="sqlSessionFactory"class="org.mybatis.spring.SqlSessionFactoryBean"><propertyname="dataSource"ref="dataSource"/><propertyname="configLocation"value="classpath:config/mybatis-config.xml"/><propertyname="mapperLocations"value="classpath*:config/mappers/**/*.xml"/></bean><!-- Transaction manager for a single JDBC DataSource --><beanid="transactionManager"class="org.springframework.jdbc.datasource.DataSourceTransactionManager"><propertyname="dataSource"ref="dataSource"/></bean><!-- 使用annotation定義事務 --><tx:annotation-driventransaction-manager="transactionManager"/><beanclass="org.mybatis.spring.mapper.MapperScannerConfigurer"><propertyname="basePackage"value="net.aazj.mapper"/><propertyname="sqlSessionFactoryBeanName"value="sqlSessionFactory"/></bean><!-- Enables the use of the @AspectJ style of Spring AOP --><aop:aspectj-autoproxy/><!-- ===============第二個資料來源的配置=============== --><beanname="dataSource_2"class="com.alibaba.druid.pool.DruidDataSource"init-method="init"destroy-method="close"><propertyname="url"value="${jdbc_url_2}"/><propertyname="username"value="${jdbc_username_2}"/><propertyname="password"value="${jdbc_password_2}"/><!-- 初始化連線大小 --><propertyname="initialSize"value="0"/><!-- 連線池最大使用連線數量 --><propertyname="maxActive"value="20"/><!-- 連線池最大空閒 --><propertyname="maxIdle"value="20"/><!-- 連線池最小空閒 --><propertyname="minIdle"value="0"/><!-- 獲取連線最大等待時間 --><propertyname="maxWait"value="60000"/></bean><beanid="sqlSessionFactory_slave"class="org.mybatis.spring.SqlSessionFactoryBean"><propertyname="dataSource"ref="dataSource_2"/><propertyname="configLocation"value="classpath:config/mybatis-config-2.xml"/><propertyname="mapperLocations"value="classpath*:config/mappers2/**/*.xml"/></bean><!-- Transaction manager for a single JDBC DataSource --><beanid="transactionManager_2"class="org.springframework.jdbc.datasource.DataSourceTransactionManager"><propertyname="dataSource"ref="dataSource_2"/></bean><!-- 使用annotation定義事務 --><tx:annotation-driventransaction-manager="transactionManager_2"/><beanclass="org.mybatis.spring.mapper.MapperScannerConfigurer"><propertyname="basePackage"value="net.aazj.mapper2"/><propertyname="sqlSessionFactoryBeanName"value="sqlSessionFactory_2"/></bean>
【以上的程式碼,是參考文章上copy的,沒有試過】 大概的意思是醬紫的吧 : 事務【transactionManager】 → 資料來源【DataSource】對映SQL Factfactory 到對應XML →【sqlSessionFactory】  ←   配置sql注入 【MapperScannerConfigurer】; 事務【transactionManager2】 → 資料來源【DataSource2】對映SQL Factfactory 到對應XML →【sqlSessionFactory2】  ←   配置sql注入 【MapperScannerConfigurer2】;
這樣依據配置的sql注入檔案xml的目錄,就對應到兩個不同的資料來源上了。 ps:這種方法配置很簡單,就是配置兩套完整的資料來源,各種獨立。 缺點是不夠靈活,同一個sql要想在兩個資料庫執行則要在不同的配置目錄下配置相同的檔案,以及整個對應的東東bean,dao,service。

2.使用多個數據庫,並配置資料來源池進行切換使用。

適合情況【我遇到的情況】:
專案使用oracle資料庫,使用主從備份master-slave,master上允許所有的操作,為分攤master資料庫壓力,將耗時的查詢放在slave上執行【專案master資料為多專案公用,資料庫壓力比較大】【專案資料庫備份為單向備份,所以不允許在slave資料庫上進行任何修改】 ps:由於備份資料庫的實效性上有一定時間的延遲,對資料時效性要求高的查詢必須在master上執行。

核心思想:

Spring在每次操作資料庫的時候都會通過AbstractRoutingDataSource類中的determineTargetDataSource()方法獲取當前資料來源,我們就可以通過切面技術,在不同的切面,切入不同的資料來源名稱,使Spring獲取的時候拿到的是不同的資料來源。
determineCurrentLookupKey()方法是抽象的,所以,我們可以實現這個類,重寫determineCurrentLookupKey方法,通過切面技術實現多資料來源之間切換

具體實現如下:

SpringDataSource配置
<?xml version="1.0" encoding="UTF-8"?><beansxmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:context="http://www.springframework.org/schema/context"xmlns:jdbc="http://www.springframework.org/schema/jdbc"xmlns:jee="http://www.springframework.org/schema/jee"xmlns:tx="http://www.springframework.org/schema/tx"xmlns:util="http://www.springframework.org/schema/util"xmlns:task="http://www.springframework.org/schema/task"xsi:schemaLocation="		http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd		http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd		http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-4.0.xsd		http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-4.0.xsd		http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd		http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd		http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-4.0.xsd"default-lazy-init="true"><description>Spring Configuration</description><!-- 載入配置屬性檔案 --><!-- <context:property-placeholder ignore-unresolvable="true" location="classpath:bms.properties" /> --><context:property-placeholderorder="0"ignore-resource-not-found="true"ignore-unresolvable="true"location="file:/usr/local/tomcat/bin/bms.properties"/><context:property-placeholderorder="1"ignore-resource-not-found="true"ignore-unresolvable="true"location="classpath:bms.properties"/><!-- 載入應用屬性例項,可通過  @Value("#{APP_PROP['jdbc.driver']}") String jdbcDriver 方式引用 --><util:propertiesid="APP_PROP"location="classpath:.properties"local-override="true"/>

相關推薦

jeesite專案使用-資料庫配置Spring mvc + Mybatis

jeesite開源專案使用-多資料庫配置【Spring + Mybatis】 配置DataSource多資料來源 使用場景: 同一個專案涉及多個數據庫,既多資料來源的情況。一般有兩種情況: 1。兩個資料庫沒有關係,各自獨立,只是獨立使用,並

spring boot+mybatis註解使用方式(無xml配置)設定自動駝峰明明轉換(mapUnderscoreToCamelCase),IDEA中xxDao報錯could not autowi

最近使用spring boot+mybatis,使用IntelliJ IDEA開發,記錄一些問題的解決方法。1、在使用@Mapper註解方式代替XXmapper.xml配置檔案,使用@Select等註解配置sql語句的情況下,如何配置資料庫欄位名到JavaBean實體類屬性命

spring boot Mybatis報錯:org.apache.ibatis.binding.BindingException: Invalid bound statement (not found): com.newhope.interview.dao.UserMapper.add

使用 ase err abstract internal tin mic pre uestc 報錯如下: org.apache.ibatis.binding.BindingException: Invalid bound statement (not found): c

Spring+SpringMVC+Mybatis利用SSM整合,完成使用者登入、註冊、修改密碼系統

近年來,由於Struts2+Hibernate3+Spring3,這套SSH框架,Struts2屢次爆出安全漏洞,Hibernate就只會推行它HQL那套而越來越遠離SQL查詢關係資料庫的本質,所以Spring+SpringMVC+Mybatis這套SSM框架悄然興起,現階

spring MvcSpringMVC 檔案上傳配置檔案上傳,使用的MultipartFile

基本的SpringMVC的搭建在我的上一篇文章裡已經寫過了,這篇文章主要說明一下如何使用SpringMVC進行表單上的檔案上傳以及多個檔案同時上傳的步驟 SpringMVC 基礎教程 框架分析:ht

Spring+Spring MVC+Mybatis+Maven搭建模組專案

maven多模組專案SpringMVC簡單例項:劃分多模組,也就是方便多人開發,自己開發自己的那塊沒有多大沖突。 專案結構 整個專案目錄是這樣的: GitHub地址 :https://github.com/thecattle/maven_model —-

Spring MVC註解和配置檔案的程式碼比較

當我們在類檔案裡寫了方法,怎麼被程式知道並呼叫呢?一般有兩種方法: 配置檔案 註解 下面小編就以親自敲的例子“SpringMVC_Test”為例來簡單說說。 配置檔案篇 在springmvc.xml中這樣寫: 在controller包下的類中這樣寫: 註

spring boot 資料庫配置問題

application.yml配置: spring:   jpa:     show-sql: true   application:     name: xxx   thymeleaf:     cache: false #thymeleaf     mode: LEGA

Spring+Spring MVC+Mybatis+Maven搭建模組專案(二)

基於第一篇文章《Spring+Spring MVC+Mybatis+Maven搭建多模組專案(一)》的基礎上,寫一個完整的示例,從頁面到Dao層的整個過程 1、先在bug.model模組下建立com.bug.model.user包,在包中建立UserVO物件

Spring+Spring MVC+Mybatis+Maven搭建模組專案(一)

最近在研究Spring MVC和Maven,工作中也是使用Spring MVC、Mybatis及Maven整合,出於好奇,自己也搭建了一個Spring+Spring MVC+Mybatis+Maven的多模組框架,先介紹一下我的工程結構 bug.root:根模

51. spring boot屬性檔案之環境配置從零開始學Spring Boot

【視訊&交流平臺】 http://study.163.com/course/introduction.htm?courseId=1004329008&utm_campaign=commission&utm_source=40000000

實戰Spring+Spring MVC+Mybatis實戰專案之雲筆記專案

            【實戰】Spring+Spring MVC+Mybatis實戰專案之雲筆記專案一、專案簡介         1、專案概述雲筆記,是tmocc上的一個子專案,用於客戶進行線上學習記錄,分享,收藏筆記,以及參與社群活動。2.模組劃分 使用者模組:登入、註冊

spring mvcSpring MVC配置過濾器並在過濾器中使用bean

使用springMVC的專案,web.xml一般是這樣的: <servlet>     <servlet-name>spring</servlet-name>     <servlet-class>org.sprin

spring MVC新手從零新建第一個hello world專案

說明 本文適合一個沒有搭建過spring MVC專案的新手(甚至沒怎麼參與過java相關專案的新手)參閱! 這也是arvik參考網路文章學習spring MVC搭建的一個筆記 軟體安裝 安裝jdk 開啟windows命令列視窗執行命令檢查

IntelliJ IDEA 最簡單配置搭建Spring MVC Java web Maven專案

原文地址:http://blog.csdn.net/qq_27093465/article/details/68961393 使用這個IntelliJ IDEA建立一個簡單的Java web maven專案,我在前面的文章裡面已經示範過了。 先看下專案目錄

spring mvc(二)spring mvc使用屬性檔案配置c3p0和dbcp資料來源

spring mvc如果要用到資料庫,就要為專案配置資料來源,目前有兩個比較常見的資料來源:c3p0與dbcp。這兩個都是連線池技術,連線池的概念大概是,維護若干個資料庫的連線,程式需要使用的時候直接返回給它,這樣做的用意是降低資料庫連線,關閉的開銷,如果每次程式請求資料庫

Spring+Spring MVC+Mybatis+Maven搭建模塊項目(二)

自己 var user inf 接口 work 過程 cal ber 基於第一篇文章《Spring+Spring MVC+Mybatis+Maven搭建多模塊項目(一)》的基礎上,寫一個完整的示例,從頁面到Dao層的整個過程 1、先在bug.model模塊下創建com.bu

Spring Cloud學習筆記 篇一:分布式配置中心 Spring Colud Config

16px gin war imp web項目 tps conf name request 一、簡介 Spring Cloud Config提供了在分布式系統的外部配置的客戶端支持。通過配置服務(Config Server)來為所有的環境和應用提供外部配置的集中管理。這些概念

基於spring2.5的采用XML配置spring MVC項目

ont encoding cte default 尚學堂 rri 導入jar包 request 事務 Spring MVC 背景介紹 Spring 框架提供了構建 Web 應用程序的全功能 MVC 模塊。使用 Spring 可插入的 MVC 架構,可以選擇是使用內

spring mvc後臺的API,測試中,總提示接口實體的某一個字段不能為null,但是明明給值了還提示不能為空

ont TP 報錯 分享 ima 技術 技術分享 圖片 request 實體是這三個字段 接口的實現類Controller 前臺測試給值 依舊報錯 解決方法: 需要添加@RequestBody註解 【spring mvc】後臺的API,