zoukankan      html  css  js  c++  java
  • 基于 Spring + Atomikos + Mybatis的多数据源配置(含有BaseDao,BaseService)

    1.spring配置文件

    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:util="http://www.springframework.org/schema/util" 
           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"
           xmlns:tx="http://www.springframework.org/schema/tx"
           xmlns:context="http://www.springframework.org/schema/context"
           xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans.xsd
    http://www.springframework.org/schema/tx
    http://www.springframework.org/schema/tx/spring-tx.xsd
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context-3.1.xsd
    http://www.springframework.org/schema/aop
    http://www.springframework.org/schema/aop/spring-aop.xsd
    http://www.springframework.org/schema/util
    http://www.springframework.org/schema/util/spring-util-3.1.xsd
    ">
    
        <!--使用@Autowired注解,需要注入下bean-->
        <bean class="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor"/>
        
        <context:component-scan base-package="com.tykj.weatherservice" annotation-config="false" />
        
        <!--实际上,PropertyPlaceholderConfigurer起的作用就是将占位符指向的数据库配置信息放在bean中定义的工具。-->
        <bean id="propertyConfigurer"
              class="com.tykj.secondparty.context.CustomizedPropertyPlaceholderConfigurer">
            <property name="locations">
                <list>
                    <value>classpath:/system.properties</value>
                    <value>classpath:/jdbc.properties</value> 
                </list>
            </property>
        </bean>
        
        <bean id="settings" 
            class="org.springframework.beans.factory.config.PropertiesFactoryBean">
            <property name="locations">
                <list>
                    <value>classpath:/system.properties</value>
                </list>
            </property>
            <property name="fileEncoding">
                <value>UTF-8</value>
            </property>
        </bean>  
        
        <!-- 多个数据源的功用配置,方便下面直接引用 -->
         <bean id="abstractXADataSource" class="com.atomikos.jdbc.AtomikosDataSourceBean" init-method="init" destroy-method="close" abstract="true">
            <property name="poolSize" value="${mysql_poolSize}" />
            <property name="minPoolSize" value="${mysql_minPoolSize}"/>
            <property name="maxPoolSize" value="${mysql_maxPoolSize}"/>
            <property name="borrowConnectionTimeout" value="${borrowConnectionTimeout}"/>
            <property name="reapTimeout" value="${mysql_reapTimeout}"/>
            <!-- 最大空闲时间 -->
            <property name="maxIdleTime" value="${mysql_maxIdleTime}"/>
            <property name="maintenanceInterval" value="${mysql_maintenanceInterval}" />
            <property name="loginTimeout" value="${mysql_loginTimeout}"/>
            <property name="logWriter" value="${mysql_logWriter}"/>
        </bean>
        
     <!--测试web数据库的分布式事务atomikos 的三种数据源 SimpleDataSourceBean,AtomikosDataSourceBean,AtomikosNonXADataSourceBean-->
        <!-- <bean id="dataSourceOracle" class="com.atomikos.jdbc.AtomikosDataSourceBean"
              init-method="init" destroy-method="close">
            <description>oracle xa datasource</description>
            <property name="uniqueResourceName" value="${oracle_resourceName}"/>
            <property name="xaDataSourceClassName" value="${oracle_xaDataSourceClassName}"/>
            <property name="xaProperties">
                <props>
                    <prop key="user">${oracle_user}</prop>
                    <prop key="password">${oracle_password}</prop>
                    <prop key="URL">${oracle_url}</prop>
                </props>
            </property>
            <property name="minPoolSize" value="${oracle_poolSize}"/>
            <property name="maxPoolSize" value="${oracle_maxPoolSize}"/>
            <property name="maxIdleTime" value="${oracle_maxIdleTime}"/>
            <property name="borrowConnectionTimeout" value="${borrowConnectionTimeout}"/>
        </bean> -->
        
        <bean id="dataSourceXph" parent="abstractXADataSource">
            <description>mysql xph datasource</description>
            <property name="uniqueResourceName" value="${mysql_resourceName_xph}"/>
            <property name="xaDataSourceClassName" value="${mysql_xaDataSourceClassName}"/>
            <property name="xaProperties">
                <props>
                    <prop key="url">${mysql_url_xph}</prop>
                    <prop key="user">${mysql_user_xph}</prop>
                    <prop key="password">${mysql_password_xph}</prop>
                </props>
            </property>
        </bean>
        
        <bean id="dataSourceQs" parent="abstractXADataSource">
            <description>mysql qs datasource</description>
            <property name="uniqueResourceName" value="${mysql_resourceName_qs}"/>
            <property name="xaDataSourceClassName" value="${mysql_xaDataSourceClassName}"/>
            <property name="xaProperties">
                <props>
                    <prop key="url">${mysql_url_qs}</prop>
                    <prop key="user">${mysql_user_qs}</prop>
                    <prop key="password">${mysql_password_qs}</prop>
                </props>
            </property>
        </bean>
        
        <!--Spring让LOB数据操作变得简单易行   LOB类型后期需要的话再加入-->
    <!--     <bean id="lobHandler" class="org.springframework.jdbc.support.lob.DefaultLobHandler"/> -->
        
        <!-- 创建SqlSessionFactory,同时指定数据源-->  
        <!-- <bean id="sqlSessionFactoryOracle" class="org.mybatis.spring.SqlSessionFactoryBean">     
            <property name="dataSource" ref="dataSourceOracle" />     
            <property  name="configLocation"  value="classpath:com/tykj/resources/configuration.xml"/>
            <property name="mapperLocations" value="classpath*:com/tykj/tracingcloud/beans/**/*.xml"/>
        </bean> -->
        
        <bean id="sqlSessionFactoryXph" class="org.mybatis.spring.SqlSessionFactoryBean">     
            <property name="dataSource" ref="dataSourceXph" />     
            <property name="configLocation" value="classpath:com/tykj/resources/configuration.xml"/>
            <!-- <property name="mapperLocations" value="classpath*:com/tykj/weatherservice/bean/**/*.xml"/> -->
            <property name="mapperLocations">
                <array>
                    <value>classpath*:com/tykj/weatherservice/bean/xphdata/xphRealData.xml</value>
                </array>
            </property>
        </bean>
        
        <bean id="sqlSessionFactoryQs" class="org.mybatis.spring.SqlSessionFactoryBean">     
            <property name="dataSource" ref="dataSourceQs" />
            <property name="configLocation" value="classpath:com/tykj/resources/configuration.xml"/>
            <!-- <property name="mapperLocations" value="classpath*:com/tykj/weatherservice/bean/**/*.xml"/> -->
            <property name="mapperLocations">
                <array>
                    <value>classpath*:com/tykj/weatherservice/bean/channel/channel.xml</value>
                    <value>classpath*:com/tykj/weatherservice/bean/deviceinfo/deviceinfo.xml</value>
                    <value>classpath*:com/tykj/weatherservice/bean/webthings/webthings.xml</value>
                </array>
            </property>
        </bean>
        
        
         <!-- MyBatis为不同的mapper注入sqlSessionFactory -->  
        <bean id="xphRealDataDao" class="org.mybatis.spring.mapper.MapperFactoryBean">  
            <property name="sqlSessionFactory" ref="sqlSessionFactoryXph" />  
            <property name="mapperInterface" value="com.tykj.weatherservice.dao.xphdata.XphRealDataDao" />  
        </bean> 
        <bean id="channelDao" class="org.mybatis.spring.mapper.MapperFactoryBean">  
            <property name="sqlSessionFactory" ref="sqlSessionFactoryQs" />  
            <property name="mapperInterface" value="com.tykj.weatherservice.dao.channel.ChannelDao" />  
        </bean>
        <bean id="deviceinfoDao" class="org.mybatis.spring.mapper.MapperFactoryBean">  
            <property name="sqlSessionFactory" ref="sqlSessionFactoryQs" />  
            <property name="mapperInterface" value="com.tykj.weatherservice.dao.deviceinfo.DeviceinfoDao" />  
        </bean>
        <bean id="webThingsDao" class="org.mybatis.spring.mapper.MapperFactoryBean">  
            <property name="sqlSessionFactory" ref="sqlSessionFactoryQs" />  
            <property name="mapperInterface" value="com.tykj.weatherservice.dao.webthings.WebThingsDao" />  
        </bean>  
        
        
        <!-- Mapper接口所在包名,Spring会自动查找其下的Mapper -->
        <!-- <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
            不能跟其他bean的扫描路径重叠
            <property name="basePackage" value="com.tykj.tracingcloud.beans.*" />
            sqlSessionFactory自动注入
            <property name="sqlSessionFactory" ref="sqlSessionFactoryOracle"/>  
        </bean> -->
        <!-- <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
            不能跟其他bean的扫描路径重叠
            <property name="basePackage" value="com.tykj.weatherservice.dao.base.implxph" />
            sqlSessionFactory自动注入
            <property name="sqlSessionFactoryBeanName" ref="sqlSessionFactoryXph"/>  
        </bean>
        <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
            不能跟其他bean的扫描路径重叠
            <property name="basePackage" value="com.tykj.weatherservice.dao.base.implqs" />
            sqlSessionFactory自动注入
            <property name="sqlSessionFactoryBeanName" ref="sqlSessionFactoryQs"/>  
        </bean> -->
        
        <bean id="atomikosUserTransaction" class="com.atomikos.icatch.jta.UserTransactionImp">
            <description>UserTransactionImp1</description>
            <property name="transactionTimeout">
                <value>300</value>
            </property>
        </bean>
    
        <bean id="atomikosTransactionManager" class="com.atomikos.icatch.jta.UserTransactionManager"
              init-method="init" destroy-method="close">
            <description>UserTransactionManager1</description>
            <property name="forceShutdown">
                <value>true</value>
            </property>
        </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>
        
          <bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> 
            <property name="dataSource" ref="dataSourceXph"/> 
        </bean>
        
        <!-- 事务的传播特性(JoinPoint) -->
        <tx:advice id="txAdvice" transaction-manager="txManager">
            <tx:attributes>
                <tx:method name="add*" propagation="REQUIRED" rollback-for="java.lang.Exception" />
                <tx:method name="modify*" propagation="REQUIRED"
                           rollback-for="java.lang.Exception"/>
                <tx:method name="delete*" propagation="REQUIRED"
                           rollback-for="java.lang.Exception"/>
                <tx:method name="update*" propagation="REQUIRED"
                           rollback-for="java.lang.Exception"/>
                <tx:method name="deal*" propagation="REQUIRED"
                           rollback-for="java.lang.Exception"/>
               <!-- SUPPORTS支持当前事务,如果当前没有事务,就以非事务方式执行。-->
                <tx:method name="*" propagation="SUPPORTS"/>
            </tx:attributes>
        </tx:advice>
        
        <!-- 开启注解 -->  
        <context:annotation-config />  
        <!-- base-package指向注解要扫描的包 -->  
        <context:component-scan base-package="com.tykj.weatherservice" annotation-config="false"/> 
        
        <!-- 配置那些类参与事务(PointCut) -->
        <aop:config>
            <aop:pointcut expression="execution(* com.tykj.weatherservice.service.*..impl.*(..))" id="serviceMethod"/>
            <aop:advisor advice-ref="txAdvice" pointcut-ref="serviceMethod"/>
        </aop:config>
    </beans>

    2.项目结构


  • 相关阅读:
    基础数据类型之字符串str
    python编码基础知识
    python逻辑运算之and、or
    Django中消息中间键和form组件的运用
    Django中 cookies and session的使用
    JavaScript 正则制表符,单词边界,去空格
    paramiko堡垒机、线程及锁
    0911 Socket网络编程
    os.system和os.popen
    类高级方法、反射、异常、动态导入模块
  • 原文地址:https://www.cnblogs.com/ysgcs/p/7452219.html
Copyright © 2011-2022 走看看