Spring + MyBatis multi-data source Switching

Source: Internet
Author: User

Spring + MyBatis multi-data source Switching

In actual company projects, it is very likely that there will be a problem: a Java project, but the project involves two databases, the two databases are still on machines with different IP addresses.

In this case, we have two options.

1. Do not use the spring aop method to directly create two dataSource nodes.

2. Use spring for management and flexible data source Switching

Now let's take notes on 2nd methods:

Spring. xml configuration file:

<? 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: task = "http://www.springframework.org/schema/task" xmlns: tx = "http://www.springframework.org/schema/tx"
Xmlns: jdbc = "http://www.springframework.org/schema/jdbc" xmlns: context = "http://www.springframework.org/schema/context"
Xsi: schemaLocation ="
Http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
Http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
Http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.0.xsd
Http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
Http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task.xsd
Http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd ">

<! -- Configure mysql database parameters -->
<Context: property-placeholder location = "mysql. properties"/>

<! -- Configure multiple data sources -->
<Bean id = "db1" class = "org. apache. commons. dbcp. BasicDataSource"
Destroy-method = "close">
<! -- Connect to database parameters -->
<Property name = "driverClassName" value = "$ {db1.driver}"/>
<Property name = "url" value = "$ {db1.url}"/>
<Property name = "username" value = "$ {db1.username}"/>
<Property name = "password" value = "$ {db1.password}"/>

<! -- Connection pool parameters -->
<Property name = "initialSize" value = "10"/>
<Property name = "maxActive" value = "500"/>
<Property name = "maxIdle" value = "40"/>
<Property name = "minIdle" value = "10"/>
</Bean>

<Bean id = "db2" class = "org. apache. commons. dbcp. BasicDataSource"
Destroy-method = "close">
<! -- Connect to database parameters -->
<Property name = "driverClassName" value = "$ {db2.driver}"/>
<Property name = "url" value = "$ {db2.url}"/>
<Property name = "username" value = "$ {db2.username}"/>
<Property name = "password" value = "$ {db2.password}"/>

<! -- Connection pool parameters -->
<Property name = "initialSize" value = "10"/>
<Property name = "maxActive" value = "500"/>
<Property name = "maxIdle" value = "40"/>
<Property name = "minIdle" value = "10"/>
</Bean>

<Bean id = "dataSource" class = "com. ckd. datasource. mybatis. DynamicDataSource">
<Property name = "targetDataSources">
<Map key-type = "java. lang. String">
<Entry key = "db1" value-ref = "db1"/>
<Entry key = "db2" value-ref = "db2"/>
</Map>
</Property>
<Property name = "defaultTargetDataSource" ref = "db2"/>
</Bean>

<! -- Mybatis configuration -->
<Bean id = "sqlSessionFactory" class = "org. mybatis. spring. SqlSessionFactoryBean">
<Property name = "dataSource" ref = "dataSource"/>
<Property name = "configLocation" value = "classpath: mybatis/mybatis-config.xml"/>
<Property name = "mapperLocations" value = "classpath: mybatis/mapper/*. xml"/>
</Bean>
<! -- Obtain sqlSession -->
<Bean id = "sqlSession" class = "org. mybatis. spring. SqlSessionTemplate">
<Constructor-arg index = "0">
<Ref bean = "sqlSessionFactory"/>
</Constructor-arg>
</Bean>
<! -- Transaction Manager configuration, single data source transaction -->
<Bean id = "transactionManager"
Class = "org. springframework. jdbc. datasource. DataSourceTransactionManager">
<Property name = "dataSource" ref = "dataSource"/>
</Bean>

</Beans>

------------------------------------------------------------------

/**
* @ FileName performancecontextholder. java
* @ Author chenkaideng
* @ Date January 1, August 27, 2015
* @ Describe: Data Source value Holder class
*/
Public class performancecontextholder {
 
Private static final ThreadLocal <String> contextHolder = new ThreadLocal <String> ();

Public static void setDbType (String dbType ){
ContextHolder. set (dbType );
}

Public static String getDbType (){
Return (String) contextHolder. get ());
}

Public static void clearDbType (){
ContextHolder. remove ();
}
}

After completing the above steps, the rest will be simple.

-First load the spring. xml file applicationContext = new ClassPathXmlApplicationContext ("spring. xml ");

-And then set the data source performancecontextholder. setDbType ("db1 ");

-"Then obtain sqlSession = (SqlSession) applicationContext. getBean (" sqlSession ") from applicationContext ");

-In the end, you can use this sqlSession to add, delete, modify, and query data.

Note: you do not need to perform close and comit operations on this sqlSession, because they are all managed by spring itself, and you do not need to perform these operations manually.

MyBatis getting started

Practical application of Java: Mybatis implements addition, deletion, and modification of a single table

[Java] [Mybatis] physical paging implementation

Mybatis quick start tutorial

Test batis's batch data operations

Batch insert operation for List <Object> Object List in Mybatis

For more information about MyBatis, click here.
MyBatis: click here

This article permanently updates the link address:

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.