Spring Data Source Configuration Two: multiple data sources

Source: Internet
Author: User
Tags mssql

With the previous section Spring data source Configuration one: Single data source We understand the configuration of a single data source, where we continue to configure multiple data sources

as follows (Applicationcontent.xml content)

One: Spring configuration:

<!--MYSQL configuration --<bean id= "Mysqldatasource"class= "Org.springframework.jdbc.datasource.DriverManagerDataSource" > <property name= "driverclassname" >< value>${mysql.jdbc.driverclassname}</value></property> <property name= "url" ><value>${ mysql.jdbc.url}</value></property> <property name= "username" ><value>${ mysql.jdbc.username}</value></property> <property name= "password" ><value>${ mysql.jdbc.password}</value></property> </bean> <bean id= "mysqlsessionfactory"class= "Org.mybatis.spring.SqlSessionFactoryBean" > <property name= "dataSource" ref= "Mysqldatasource"/> & lt;! --Specify Sqlmapconfig general profile, custom environment not valid in spring container--<property name= "configlocation" value= "Classpath:mybatis-config-mysql.xml"/><!--Specify the Entity class mapping file, you can specify that both a package and all configuration files under the child package are specified, mapperlocations and configlocation have one, and when you need to specify an alias for an entity class, you can specify the Configlocation property. Then introduce the Entity class mapping file in the MyBatis general configuration file using Mapper--<property name= "Mapperlocations" > <list><value>classpath*:/mysqlmapper/* mapper.xml</value></list> </property> </bean> <bean class= "Org.mybatis.spring.mapper.MapperScannerConfig Urer "> <property name=" basepackage "value=" com.robin.it. Permission.dao"/> <!--optional unless there is multiple session factories defined-<property name= "Sqlsessionfactorybeanname" value= "Mysqlsessionfactory"/></bean><!--SQL SERVER configuration -<bean id= "SQLServerDataSource"class=" Org.springframework.jdbc.datasource.DriverManagerDataSource "> <property name=" Driverclassname "> <value>${mssql.jdbc.driverClassName}</value></property> <property name= "url" ><value >${mssql.jdbc.url}</value></property> <property name= "username" ><value>${ mssql.jdbc.username}</value></property> <property name= "password" ><value>${ mssql.jdbc.password}</value></property> </bean> <bean id= "sqlserversessionfactory"class=" Org.mybatis.spring.SqlSessionFactoryBean "> <property name=" DataSource "ref="SQLServerDataSource"/> <!--specify Sqlmapconfig general profile, custom environment not valid in spring container--<property name= "configlocation" value= "Classpath:mybatis-config-sqlserver.xml"/><!--specify an entity class mapping file, you can specify that you specify both a package and all of the configuration files under the child package, mapperlocations and Configlocation have one, and you can specify the Configlocation property when you want to specify an alias for the entity class , introducing mapper into the Entity class mapping file in the MyBatis general configuration file---<property name= "Mapperlocations" > <list><value>classpath*:/sqlservermapper/*Mapper.xml</value></list> </property> </bean> <bean class= "Org.mybatis.spring.mapper.MapperScannerConfig Urer "> <property name=" basepackage "value=" com.robin.it.Mes.dao"/> <!--optional unless there is multiple session factories defined-<property name= "Sqlsessionfactorybeanname" value= "Sqlserversessionfactory"/></bean>

The above configuration, respectively set up two data (MySQL, SQL Server), from the yellow highlighting, basically do the following points:

1. Data sources are different (nonsense, is a multi-data source), and even the database vendor is different (reflected in the Database.properties file)

2. MyBatis + database Paging engine is different (embodied in mybatis-config-*.xml files)

3. Mybastis the corresponding XML SQL file (reflected in the different paths of the/*mapper.xml)

4. Mapper file path is different (reflected in Mapperscannerconfigurer Com.robin.it.*.dao)

Combined above four points, it can be seen that the multi-data source, is basically a single data source configuration of the "overlay", as long as the above 4 points mentioned in the content of the separation.

II: database.properties Participation: Spring data source Configuration one: Single data source

Three: The difference between the mybatis-config-*.xml is that the data engine is different, the dialect is different

Mybatis-config-mysql.xml

<configuration>     <settings>        <setting name= "cacheenabled" value= "false"/>        < Setting name= "Lazyloadingenabled" value= "false"/> <setting        name= "aggressivelazyloading" value= "false"/ >        <setting name= "Jdbctypefornull" value= "NULL"/>    </settings>    <plugins>        < Plugin interceptor= "Com.github.miemiedev.mybatis.paginator.OffsetLimitInterceptor" >            < Property Name= "Dialectclass" value= "Com.github.miemiedev.mybatis.paginator.dialect.MySQLDialect"/>        </plugin>    </plugins>    </configuration>

Mybatis-config-sqlserver.xml

<configuration>     <settings>        <setting name= "cacheenabled" value= "false"/>        < Setting name= "Lazyloadingenabled" value= "false"/> <setting        name= "aggressivelazyloading" value= "false"/ >        <setting name= "Jdbctypefornull" value= "NULL"/>    </settings>        <plugins>        < Plugin interceptor= "Com.github.miemiedev.mybatis.paginator.OffsetLimitInterceptor" >            < Property Name= "Dialectclass" value= "Com.github.miemiedev.mybatis.paginator.dialect.SQLServerDialect"/>        </plugin>    </plugins>    </configuration>

Four: The path of *mapper.xml is different

=========================================================================================

To this, the configuration of the multi-data is basically complete, so easy. But .....

Some of the issues we need to address are as follows:

1. If the above 2 data, data sheet, structure business, etc. are not, then the above configuration is not a problem (this time did not join the transaction), if it is the same data source of different copies (such as Master/slaver)?

So mapper.xml/xxxdao/and other code should be exactly the same (that is, the above mentioned 4 points, do not separate, only one copy), how to switch data source?

2. Different methods for the same service, if data is selected dynamically? (for reading and writing separation)

3 ...

4 .....

Continue to think .....

Spring Data Source Configuration Two: 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.