Multi-Data Source configuration demo based on Spring + Atomikos + mybatis

Source: Internet
Author: User

1.spring 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:p= "http://www.springframework.org/schema/p"Xmlns:context= "Http://www.springframework.org/schema/context"Xmlns:tx= "Http://www.springframework.org/schema/tx"Xmlns:mvc= "Http://www.springframework.org/schema/mvc"xsi:schemalocation= "Http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsdhttp//Www.springframework.org/schema/contexthttp//www.springframework.org/schema/context/spring-context-3.1.xsdhttp//Www.springframework.org/schema/txhttp//www.springframework.org/schema/tx/spring-tx-3.2.xsdhttp//Www.springframework.org/schema/mvchttp//www.springframework.org/schema/mvc/spring-mvc-4.0.xsd "><!--automatically scanned-<context:component-scan base- Package= "com.cn"/> <!--Introduction Profile--<!--<bean id= "Propertyconfigurer"class= "Org.springframework.beans.factory.config.PropertyPlaceholderConfigurer" > <property name= "Location" value= "Classpath:jdbc.properties"/> </bean>--> <bean id= "Datasourceoracle"class= "Com.atomikos.jdbc.AtomikosDataSourceBean"Init-method= "Init" destroy-method= "close" > <property name= "uniqueresourcename" value= "Oracle_ds"/> &lt ;p roperty name= "Xadatasourceclassname"value= "Oracle.jdbc.xa.client.OracleXADataSource"/> <property name= "xaproperties" > <props> <prop key= "URL" >jdbc:oracle:thin:@192.168.222.1:1521:orcl</prop> <prop key= "user ">scott</prop> <prop key=" password ">orcl</prop> </props> </ property> <property name= "minpoolsize" value= "ten"/> <property name= "maxpoolsize" value= "100"/&        Gt <property name= "borrowconnectiontimeout" value= ""/> <property name= "Maintenanceinterval" value= "/&G"    T </bean> <bean id= "Datasourcemysql"class= "Com.atomikos.jdbc.AtomikosDataSourceBean"Init-method= "Init" destroy-method= "close" > <property name= "uniqueresourcename" value= "Mysql_ds"/> < Property Name= "Xadatasourceclassname"value= "Com.mysql.jdbc.jdbc2.optional.MysqlXADataSource"/> <property name= "xaproperties" > <props&                Gt <prop key= "url" >jdbc:mysql://localhost:3306/test</prop><prop key= "User" >root</prop> <prop key= "password" ></prop> </props> </property> <property name= "minpoolsize" value= "ten"/> <property name= "Maxpoolsize" V Alue= "/> <property name=" borrowconnectiontimeout "value=" "/> <property name=" maintenance Interval "value="/> </bean> <bean id= "Sqlsessionfactoryoracle"class= "Org.mybatis.spring.SqlSessionFactoryBean" > <property name= "dataSource" ref= "Datasourceoracle"/> <!--automatically scan mapping.xml files--<property name= "mapperlocations" > <array> &L    T;value>classpath:com/cn/dao/dept/deptdaomapper.xml</value> </array> </property> </bean> <bean id= "Sqlsessionfactorymysql"class= "Org.mybatis.spring.SqlSessionFactoryBean" > <property name= "dataSource" ref= "Datasourcemysql"/> & lt;! --Auto scan mapping.xml Files--<property name= "mapperlocations" > <array> <val ue>classpath:com/cn/dao/brand/branddaomapper.xml</value> </array> </property> &L T;/bean> <!--go to the DAO layer to scan the corresponding interface, the top of the interface to add mapperscan annotations--<beanclass= "Org.mybatis.spring.mapper.MapperScannerConfigurer" > <property name= "basepackage" value= "com.cn.dao.dept"/ > <property name= "sqlsessionfactorybeanname" value= "sqlsessionfactoryoracle" ></property> </bea N> <beanclass= "Org.mybatis.spring.mapper.MapperScannerConfigurer" > <property name= "basepackage" value= "Com.cn.dao.brand" /> <property name= "sqlsessionfactorybeanname" value= "Sqlsessionfactorymysql" ></property> </bea n> <!--Atomikos transaction Manager--<bean id= "Atomikostransactionmanager"class= "Com.atomikos.icatch.jta.UserTransactionManager"Init-method= "Init" destroy-method= "close" > <description>UserTransactionManager</description> < Property name= "Forceshutdown" > <value>true</value> </property> </bean> <bean id= "Atomikosusertransaction"class= "COM.ATOMIKOS.ICATCH.JTA.USERTRANSACTIONIMP" > <property name= "transactiontimeout" value= "/> </" Bean> <!--Spring transaction Manager--<bean id= "TransactionManager"class= "Org.springframework.transaction.jta.JtaTransactionManager" > <property name= "TransactionManager" ref= " Atomikostransactionmanager "/> <property name=" usertransaction "ref=" atomikosusertransaction "/> &lt ;p roperty name= "Allowcustomisolationlevels" value= "true"/> </bean> <!--use annotation to define a transaction, for the class to which you want to add things, simply Class plus @Transactional-<tx:annotation-driven transaction-manager= "TransactionManager"/> <!--<bean ID = "DataSource"class= "Org.apache.commons.dbcp.BasicDataSource"Destroy-method= "Close" > <property name= "driverclassname" value= "${driverclassname}"/> <property name= " URL "value=" ${url} "/> <property name=" username "value=" ${username} "/> <property name=" password "Value=" ${password} "/> &lt;! &ndash; Initialize Connection size &ndash;&gt; <property name= "InitialSize" value= "${initialsize}" ></property> &lt;! &ndash; Maximum number of connection pools &ndash;&gt; <property name= "maxactive" value= "${maxactive}" ></property> &lt;! &ndash; Maximum Connection pool idle &ndash;&gt; <property name= "Maxidle" value= "${maxidle}" ></property> &lt;! &ndash; Connection Pool min Idle &ndash;&gt; <property name= "Minidle" value= "${minidle}" ></property> &lt;! &ndash; Get connection Max wait time &ndash;&gt; <property name= "maxwait" value= "${maxwait}" ></property> </bean>--> <!--spring and MyBatis are seamlessly integrated, Configuration mapping files that do not require MyBatis-<!--<bean id= "Sqlsessionfactory"class= "Org.mybatis.spring.SqlSessionFactoryBean" > <property name= "dataSource" ref= "DataSource"/> &l t;! &ndash;<property name= "configlocation" value= "Classpath:mybatis-config.xml"/>&ndash;&gt; &lt;! &ndash; Auto Scan mapping.xml Files &ndash;&gt; <property name= "mapperlocations" value= "Classpath:com/cn/**/*.xml" ></property> </bean>--> <!--the DAO interface package name, Spring automatically finds the class under it--<!--<beanclass= "Org.mybatis.spring.mapper.MapperScannerConfigurer" > <property name= "basepackage" value= "Com.cn.dao"/>    <property name= "Sqlsessionfactorybeanname" value= "Sqlsessionfactory" ></property> </bean>--> <!--(transaction management) transaction manager, use Jtatransactionmanager forGlobal TX--<!--<bean id= "TransactionManager"class= "Org.springframework.jdbc.datasource.DataSourceTransactionManager" > <property name= "DataSource" ref= " DataSource "/> </bean>--></beans>

2. Project Structure

3.mybatis Mapping File

Multi-Data Source configuration demo based on Spring + Atomikos + mybatis

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.