<span style="font-family: Arial, Helvetica, sans-serif; background-color: rgb(255, 255, 255);">这个是本人做的一个Atomikos  分布式jta事务的 小的mavenWeb项目 ,包含 mysql 、oracle 、sqlserver 3个数据源</span>

完整工程下载路径 http://download.csdn.net/detail/npf_java/8786825

拿一个mysql + oracle 数据源配置为例吧

database.properties

#mysql DB
mysql.qa.db.url=jdbc:mysql://localhost:3306/ssm_qa?useUnicode=true&characterEncoding=UTF-8
mysql.qa.db.user=root
mysql.qa.db.password=ROOT

mysql.dev.db.url=jdbc:mysql://localhost:3306/ssm_dev?useUnicode=true&characterEncoding=UTF-8
mysql.dev.db.user=root
mysql.dev.db.password=ROOT

#oracle DB
oracle.qa.db.url=jdbc\:oracle\:thin\:@1.1.1.32\:1521\:nyj
oracle.qa.db.driverClassName=oracle.jdbc.OracleDriver
oracle.qa.db.testQuery=select 1 from dual
oracle.qa.db.user=oms
oracle.qa.db.password=oms

oracle.dev.db.url=jdbc\:oracle\:thin\:@1.1.1.32\:1521\:nyj
oracle.dev.db.driverClassName=oracle.jdbc.OracleDriver
oracle.dev.db.testQuery=select 1 from dual
oracle.dev.db.user=com
oracle.dev.db.password=lsd


#sqlserver DB
sqlserver.qa.db.driverClass=com.microsoft.sqlserver.jdbc.SQLServerDriver
sqlserver.qa.db.url=jdbc:sqlserver://1.1.1.11:1433;DatabaseName=dl_xbny
sqlserver.qa.db.user=dl_xbny
sqlserver.qa.db.password=dl123456

sqlserver.dev.db.driverClass=com.microsoft.sqlserver.jdbc.SQLServerDriver
sqlserver.dev.db.url=jdbc:sqlserver://1.1.1.11:1433;DatabaseName=com_xbny
sqlserver.dev.db.user=com_xbny
sqlserver.dev.db.password=com123456

mysql-oracle-beans.xml

<?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:jdbc="http://www.springframework.org/schema/jdbc"
       xmlns:rabbit="http://www.springframework.org/schema/rabbit"
       xmlns:cache="http://www.springframework.org/schema/cache" xmlns:task="http://www.springframework.org/schema/task"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc.xsd http://www.springframework.org/schema/rabbit http://www.springframework.org/schema/rabbit/spring-rabbit.xsd
       http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache.xsd http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task.xsd">
    <context:component-scan base-package="com.xy">
        <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
    </context:component-scan>
    <context:property-placeholder location="classpath:context/database.properties"/>
    <tx:annotation-driven/>

     <bean id="abstractXADataSource" class="com.atomikos.jdbc.nonxa.AtomikosNonXADataSourceBean" init-method="init"
          destroy-method="close" abstract="true">
        <property name="xaDataSourceClassName" value="com.mysql.jdbc.Driver"/>
        <property name="poolSize" value="10" />
        <property name="minPoolSize" value="10"/>
        <property name="maxPoolSize" value="30"/>
        <property name="borrowConnectionTimeout" value="60"/>
        <property name="reapTimeout" value="20"/>
        <!-- 最大空闲时间 -->
        <property name="maxIdleTime" value="60"/>
        <property name="maintenanceInterval" value="60"/>
        <property name="loginTimeout" value="60"/>
        <property name="testQuery">
            <value>select 1</value>
        </property>
    </bean>

   <bean id="qadataSource" class="com.atomikos.jdbc.nonxa.AtomikosNonXADataSourceBean">
        <!-- value只要两个数据源不同就行,随便取名 -->
        <property name="uniqueResourceName" value="dlDB" />
        <property name="driverClassName">
			<value>com.mysql.jdbc.Driver</value>
		</property>
        <property name="url">
			<value>${mysql.qa.db.url}</value>
		</property>
		<property name="user">
			<value>${mysql.qa.db.user}</value>
		</property>
		<property name="password">
			<value>${mysql.qa.db.password}</value>
		</property>
    </bean>

    <bean id="devdataSource" class="com.atomikos.jdbc.nonxa.AtomikosNonXADataSourceBean">
        <!-- value只要两个数据源不同就行,随便取名 -->
        <property name="uniqueResourceName" value="nyxsDB" />
       <property name="driverClassName">
			<value>${oracle.dev.db.driverClassName}</value>
		</property>
        <property name="url">
			<value>${oracle.dev.db.url}</value>
		</property>
		<property name="user">
			<value>${oracle.dev.db.user}</value>
		</property>
		<property name="password">
			<value>${oracle.dev.db.password}</value>
		</property>
    </bean>

    <bean id="qasqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="qadataSource" />
        <property name="mapperLocations" value="classpath*:com/xy/dao/*.xml" />
    </bean>

    <bean id="devsqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="devdataSource" />
        <property name="mapperLocations" value="classpath*:com/xy/daodev/*.xml" />
    </bean>

    <bean id="atomikosTransactionManager" class="com.atomikos.icatch.jta.UserTransactionManager"
          init-method="init" destroy-method="close">
        <property name="forceShutdown">
            <value>true</value>
        </property>
    </bean>
    <bean id="atomikosUserTransaction" class="com.atomikos.icatch.jta.UserTransactionImp">
        <property name="transactionTimeout" value="300" />
    </bean>

    <bean id="transactionManager"
          class="org.springframework.transaction.jta.JtaTransactionManager">
        <property name="transactionManager">
            <ref bean="atomikosTransactionManager"/>
        </property>
        <property name="userTransaction">
            <ref bean="atomikosUserTransaction"/>
        </property>
        <!-- 必须设置,否则程序出现异常 JtaTransactionManager does not support custom isolation levels by default -->
        <property name="allowCustomIsolationLevels" value="true"/>

    </bean>

    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="basePackage" value="com.xy.dao"/>
        <property name="sqlSessionFactoryBeanName" value="qasqlSessionFactory" />
    </bean>

    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="basePackage" value="com.xy.daodev"/>
        <property name="sqlSessionFactoryBeanName" value="devsqlSessionFactory" />
    </bean>
</beans>

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
         version="2.5">
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
    <listener>
        <listener-class>
            org.springframework.web.context.request.RequestContextListener
        </listener-class>
    </listener>
    
    <!--
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:context/mysql-beans.xml</param-value>
    </context-param>
    
     <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:context/oracle-beans.xml</param-value>
    </context-param>
    
     <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:context/sqlserver-beans.xml</param-value>
    </context-param>
    
      <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:context/sqlserver-oracle-beans.xml</param-value>
     </context-param>
     -->
     
    <span style="color:#ff6666;">  <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:context/mysql-oracle-beans.xml</param-value>
     </context-param></span>
    
    <servlet>
        <servlet-name>test</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath:context/test-web.xml</param-value>
        </init-param>
        <load-on-startup>100</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>test</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>
    <welcome-file-list>
        <welcome-file>index</welcome-file>
    </welcome-file-list>

</web-app>


基本上看这个三个配置文件就可以了,不明白的话,可以下载完整工程,dbsql有完整的建表语句





Logo

更多推荐