Home IT Linux Windows Database Network Programming Server Mobile  
           
  Home \ Database \ Spring + MyBatis Multi data source switching     - How to choose the first programming language based on the life you want (Programming)

- Java-based data source database access (Programming)

- Linux compiler of GCC (Linux)

- CentOS 6.7 install Nagios Tutorials (Server)

- FreeBSD install Gnome 3 Desktop (Linux)

- Android imageView in the Src and Background (Programming)

- Matters Oracle 11.2 single instance when connecting ASM need to pay attention and deal with the problem (Database)

- Modifying the system registry protection server security (Linux)

- 256 with rich colors decorate your terminal (Linux)

- ORA-12154 TNS could not resolve the specified identifier (Database)

- How to contribute code to the GNU Project (Linux)

- Replace element and non-replaced elements of learning (Programming)

- How to create an alternative Android / iOS connected wireless hotspot AP in Ubuntu 15.04 (Linux)

- Android developers learning Adapter (data adapter) (Programming)

- Ftp user to create multiple virtual machines to support different access rights Examples (Server)

- VSFTPD Security (Linux)

- Linux system package manager (rpm, yum, source packages installation) (Linux)

- To remove those IP is prohibited Fail2ban on CentOS 6/7 (Server)

- Several Methods of SSH Auto - login (Linux)

- PostgreSQL 9.3.5 database installation under Ubuntu Server 14.04 (Database)

 
         
  Spring + MyBatis Multi data source switching
     
  Add Date : 2017-08-31      
         
       
         
  In the actual company projects, they are likely to encounter a problem is a Java project, but the project involves two databases on two different IP database still loom.

When this happens, we have two choices

1, do not take the spring aop way, directly to do two dataSource

2, spring management, flexible data source switching

Now come the first two kinds of ways notes:

spring.xml profile:

< ? 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 ">
  
      < ! - Mysql database configuration parameters ->
    < Context: property-placeholder location = "mysql.properties" />
    
    < ! - Configure multiple data sources ->
    < Bean id = "db1" class = "org.apache.commons.dbcp.BasicDataSource"
        destroy-method = "close">
        < ! - Database connection 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">
        < ! - Database connection 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>
< ! - Get sqlSession ->
< Bean id = "sqlSession" class = "org.mybatis.spring.SqlSessionTemplate">
< Constructor-arg index = "0">
        < Ref bean = "sqlSessionFactory" />
        < / Constructor-arg>
< / Bean>
< ! - Transaction Manager configuration, single-source services ->
< Bean id = "transactionManager"
class = "org.springframework.jdbc.datasource.DataSourceTransactionManager">
< Property name = "dataSource" ref = "dataSource" />
< / Bean>
    
< / Beans>

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

/ **
 * @fileName DataSourceContextHolder.java
 * @author Chenkaideng
 * @date 2015 Nian Yue 27 Ri. 8
 * @describe Data source setter Holder class
 * /
public class DataSourceContextHolder {
 
  private static final ThreadLocal contextHolder = new ThreadLocal ();
  
  public static void setDbType (String dbType) {
  contextHolder.set (dbType);
  }
  
  public static String getDbType () {
  return ((String) contextHolder.get ());
  }
  
  public static void clearDbType () {
  contextHolder.remove ();
  }
}

Get that done more things, the rest of the things simple

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

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

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

- "Finally, you can take this sqlSession do CRUD operations

Note: Do not do this sqlSession close and comit operation since the spring have their own administration, do not do these operations manually.
     
         
       
         
  More:      
 
- Oracle Automatic Diagnostic Repository (Automatic Diagnostic Repository, ADR) (Database)
- MySQL dual master configuration (Database)
- Nginx configuration support f4v video format player (Server)
- Web cache basics: terminology, HTTP headers, and caching policies (Server)
- Ubuntu comes with gedit editor to add Markdown preview widget (Linux)
- Fedora 22 users to install the VLC media player (Linux)
- Editor of the popular Linux Gvim (Linux)
- JEdit 5.2 Pro Edition installation on Ubuntu 14.04 (Linux)
- Start Linux ISO image directly from the hard disk (Linux)
- C ++ Supplements - locates the new expression (Programming)
- Why learn Java EE (Programming)
- Using Java program determines whether it is a leap year (Programming)
- Ubuntu installed Gimp 2.6.11 (stable version) with PPA (Linux)
- OpenGL shadow map (Programming)
- Python in yield (Programming)
- CentOS6 installation configuration rsh (Server)
- Comparison of Nginx and Nginx + (Server)
- Use Ansible to bulk manage remote servers (Server)
- Compile Android libwebcore.so error occurs when solving (Programming)
- Firewall Configuration Red Hat Enterprise Linux 4 (Linux)
     
           
     
  CopyRight 2002-2016 newfreesoft.com, All Rights Reserved.