Java spring mvc多資料來源配置

來源:互聯網
上載者:User

標籤:spring mvc+my batis   kafka   dubbo+zookeerper   restful redis分布式緩衝   

1、首先配置兩個資料庫

<bean id="dataSourceA"         class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">

                <property name="driverClassName" value="${jdbc.driverClassName}" />

                <property name="url" value="${jdbc.url}" />

                <property name="username" value="${jdbc.username}" />

                <property name="password" value="${jdbc.password}" />

                <property name="maxActive" value="${jdbc.maxActive}" />

                <property name="maxWait" value="${jdbc.maxWait}" />

        </bean>

<bean id="dataSourceB"         class="org.apache.commons.dbcp.BasicDataSource">

                <property name="driverClassName" value="${vjdbc.driverClassName}" />

                <property name="url" value="${vjdbc.url}" />

                <property name="username" value="${vjdbc.username}" />

                <property name="password" value="${vjdbc.password}" />

                <property name="maxActive" value="${jdbc.maxActive}" />

                <property name="maxWait" value="${jdbc.maxWait}" />

        </bean>

2、再配置一個dataSource 管理 key 值和value值對應,預設選擇dataSourceA ,其他配置按照正常的spring mvc 配置即可。

<bean id="dataSource" class="com.broadengate.util.DynamicDataSource">

     <!-- 通過key-value的形式來關聯資料來源 -->

                <property name="targetDataSources">

                        <map key-type="java.lang.String">

                                <entry value-ref="dataSourceA" key="dataSourceA"></entry>

                                <entry value-ref="dataSourceB" key="dataSourceB"></entry>

                        </map>

                </property>

                <property name="defaultTargetDataSource" ref="dataSourceA" >

                </property>

        </bean>

3、sessionFactory 中使用 dataSource做資料來源。

4、建立一個DynamicDataSource類繼承AbstractRoutingDataSource。

public class DynamicDataSource extends AbstractRoutingDataSource{

public static final String DATA_SOURCE_A = "dataSourceA";

        public static final String DATA_SOURCE_B = "dataSourceB";

        private static final ThreadLocal<String> contextHolder = new ThreadLocal<String>();

        public static void setCustomerType(String customerType) {

                contextHolder.set(customerType);

        }

        public static String getCustomerType() {

                return contextHolder.get();

        }

        public static void clearCustomerType() {

                contextHolder.remove();

        }

        @Override

        protected Object determineCurrentLookupKey() {

                return getCustomerType();

        }

}

5、切換資料來源,這一步必須在進入業務層之前切換。

DynamicDataSource.setCustomerType(DynamicDataSource.DATA_SOURCE_A);

源碼地址擷取:mingli

有興趣的朋友們可以前往球球哦~一起分享學習技術:2042849237



Java spring mvc多資料來源配置

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.