MyBatis Configuring multiple data sources

Source: Internet
Author: User

I. Spring configuring multiple data sources

Two. Spring Configuration data source

Three. Implementation of Multipledatasource
   1:package com.wbl.modal;
   2:
   3:import org.springframework.jdbc.datasource.lookup.AbstractRoutingDataSource;
   4:
   5:/**
   6:  * Created with Simple_love
   7:  * date:2016/4/26.
   8:  * Time:11:12
   9:  * /
  :publicclassextends abstractroutingdatasource{
  11:
  :         Private Static Final New Inheritablethreadlocal<string> ();
  13:
  :          Public Static void Setdatasourcekey (String dataSource) {
  :                 datasourcekey.set (DataSource);
  :         }
  17:
  :         @Override
  :         protected Object Determinecurrentlookupkey () {
  :                 return datasourcekey.get ();
  :         }
  : }

Multipledatasource implements the Abstractroutingdatasource abstract class, then implements the Determinecurrentlookupkey method, This method is used to select which data source in the targetdatasources is used specifically

Four. Automatic switching of data sources using spring's AOP

(1) Configuring Pointcuts

Tangent setdatasource indicates that a slice is executed when all methods of all classes under the Com.wbl.service package are executed. Because all operations on the DAO layer are invoked at the service, the data source needs to be replaced before entering the DAO layer.

(2) Configuring facets

When the service that corresponds to the external data source is called, the switch data source is Otherdatasource and the other service is switched to datasource.

MyBatis Configuring multiple data sources

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.