• 基于 Spring + Atomikos + Mybatis的多数据源配置demo


    1.spring配置文件

    <?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.xsd
                            http://www.springframework.org/schema/context
                            http://www.springframework.org/schema/context/spring-context-3.1.xsd
                            http://www.springframework.org/schema/tx
                            http://www.springframework.org/schema/tx/spring-tx-3.2.xsd
                            http://www.springframework.org/schema/mvc
                            http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd">
        <!-- 自动扫描 -->
        <context:component-scan base-package="com.cn" />
        <!-- 引入配置文件 -->
        <!--<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" />
            <property 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="10" />
            <property name="maxPoolSize" value="100" />
            <property name="borrowConnectionTimeout" value="30" />
            <property name="maintenanceInterval" value="60" />
        </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>
                    <prop key="url">jdbc:mysql://localhost:3306/test</prop>
                    <prop key="user">root</prop>
                    <prop key="password"></prop>
                </props>
            </property>
            <property name="minPoolSize" value="10" />
            <property name="maxPoolSize" value="100" />
            <property name="borrowConnectionTimeout" value="30" />
            <property name="maintenanceInterval" value="60" />
        </bean>
    
        <bean id="sqlSessionFactoryOracle" class="org.mybatis.spring.SqlSessionFactoryBean">
            <property name="dataSource" ref="dataSourceOracle" />
            <!-- 自动扫描mapping.xml文件 -->
            <property name="mapperLocations">
                <array>
                    <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" />
            <!-- 自动扫描mapping.xml文件 -->
            <property name="mapperLocations">
                <array>
                    <value>classpath:com/cn/dao/brand/BrandDaoMapper.xml</value>
                </array>
            </property>
        </bean>
    
        <!--去dao层扫描对应的接口,接口上边要添加MapperScan注解-->
        <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
            <property name="basePackage" value="com.cn.dao.dept" />
            <property name="sqlSessionFactoryBeanName" value="sqlSessionFactoryOracle"></property>
        </bean>
        <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
            <property name="basePackage" value="com.cn.dao.brand" />
            <property name="sqlSessionFactoryBeanName" value="sqlSessionFactoryMysql"></property>
        </bean>
    
        <!-- atomikos事务管理器 -->
        <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="300" />
        </bean>
    
        <!-- spring 事务管理器 -->
        <bean id="transactionManager"
              class="org.springframework.transaction.jta.JtaTransactionManager">
            <property name="transactionManager" ref="atomikosTransactionManager"/>
            <property name="userTransaction" ref="atomikosUserTransaction" />
            <property name="allowCustomIsolationLevels" value="true"/>
        </bean>
    
        <!-- 使用annotation定义事务,对于要加入事物的类,只需对该类加 @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; 初始化连接大小 &ndash;&gt;
            <property name="initialSize" value="${initialSize}"></property>
            &lt;!&ndash; 连接池最大数量 &ndash;&gt;
            <property name="maxActive" value="${maxActive}"></property>
            &lt;!&ndash; 连接池最大空闲 &ndash;&gt;
            <property name="maxIdle" value="${maxIdle}"></property>
            &lt;!&ndash; 连接池最小空闲 &ndash;&gt;
            <property name="minIdle" value="${minIdle}"></property>
            &lt;!&ndash; 获取连接最大等待时间 &ndash;&gt;
            <property name="maxWait" value="${maxWait}"></property>
        </bean>-->
    
        <!-- spring和MyBatis完美整合,不需要mybatis的配置映射文件 -->
        <!--<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
            <property name="dataSource" ref="dataSource" />
            &lt;!&ndash;<property name="configLocation"  value="classpath:mybatis-config.xml"/>&ndash;&gt;
            &lt;!&ndash; 自动扫描mapping.xml文件 &ndash;&gt;
            <property name="mapperLocations" value="classpath:com/cn/**/*.xml"></property>
        </bean>-->
    
        <!-- DAO接口所在包名,Spring会自动查找其下的类 -->
        <!--<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
            <property name="basePackage" value="com.cn.dao" />
            <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property>
        </bean>-->
    
        <!-- (事务管理)transaction manager, use JtaTransactionManager for global tx -->
        <!--<bean id="transactionManager"
              class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
            <property name="dataSource" ref="dataSource" />
        </bean>
    -->
    </beans>

    2.项目结构

    3.mybatis映射文件

  • 相关阅读:
    C++中volatile及编译器优化
    virtualbox 复制虚拟机提示uuid is exists
    Opencv实现简易播放器
    机器学习 Hidden Markov Models 2
    MFC显示Mat图片
    HDU 1518 Square
    马的遍历问题
    JAVA反射机制
    UNIX基础知识
    开机黑屏 仅仅显示鼠标 电脑黑屏 仅仅有鼠标 移动 [已成功解决]
  • 原文地址:https://www.cnblogs.com/ysgcs/p/7452330.html
Copyright © 2020-2023  润新知