spring,mybatis,atomikos多数据源的整合

来源:互联网 发布:淘宝html5框架 编辑:程序博客网 时间:2024/06/06 12:47

http://www.cnblogs.com/huangjingzhou/articles/2012014.html

spring,mybatis,atomikos多数据源的整合  



由于在接下来的项目中要用到多数据源,我负责这块的工作,所以查了一下资料,但是网上的资料还多都是千篇一律,抄袭过去,抄袭过来,所以自己试着成功了,希望对有需要的人帮助,如果有不正确的地方请扔砖头,经测试成功。话入正题。了解spring的大虾门肯定一看就知道这个原理,所以我就没有贴太多的注解了。如果有需要的可以留言交流


 一、spring3.0.5,mybatis3.0.5、的包都不多说了,需要引入关于atomikos的包如下


 二、关于spring的applicationcontext的配置:


读取数据库的信息
点击(此处)折叠或打开
<!--属性文件读入,使用JdbcPlaceholderConfigurer则可以从数据库读取配置信息 -->
    <bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
        <property name="locations">
            <list>
                <value>classpath*:jdbc.properties</value>
            </list>
        </property>
    </bean>
jdbc.properties的配置文件
点击(此处)折叠或打开
jdbc.username=root
jdbc.password=123456
jdbc.url=jdbc:mysql://192.168.1.8:3310/sitestone?useUnicode=true&amp;characterEncoding=utf-8
jdbc.driver=com.mysql.jdbc.Driver


jdbca.url=jdbc:mysql://192.168.1.8:3310/sitesttwo?useUnicode=true&amp;characterEncoding=utf-8
jdbca.username=root
jdbca.password=123456
jdbca.driver=com.mysql.jdbc.Driver
配置mysql的两个数据源


点击(此处)折叠或打开
<!-- 两个数据源的功用配置,方便下面直接引用 -->
     <bean id="abstractXADataSource" class="com.atomikos.jdbc.AtomikosDataSourceBean" init-method="init" 
             destroy-method="close" abstract="true"> 
        <property name="xaDataSourceClassName" value="com.mysql.jdbc.jdbc2.optional.MysqlXADataSource"/> 
        <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="logWriter" value="60"/>
        <property name="testQuery">
            <value>select 1</value>
        </property>
        
    </bean> 
    <!-- 配置第一个数据源 -->
    <bean id="dataSource" parent="abstractXADataSource">
    <!-- value只要两个数据源不同就行,随便取名 -->
        <property name="uniqueResourceName" value="mysql/sitestone" />
        <property name="xaDataSourceClassName"
            value="com.mysql.jdbc.jdbc2.optional.MysqlXADataSource" />
        <property name="xaProperties">
            <props>
                <prop key="URL">${jdbc.url}</prop>
                <prop key="user">${jdbc.username}</prop>
                <prop key="password">${jdbc.password}</prop>
            </props>
        </property>
    </bean>


    <!-- 配置第二个数据源-->
    <bean id="dataSourceB" parent="abstractXADataSource">
<!-- value只要两个数据源不同就行,随便取名 -->
        <property name="uniqueResourceName" value="mysql/sitesttwo" />
        <property name="xaDataSourceClassName"
            value="com.mysql.jdbc.jdbc2.optional.MysqlXADataSource" />
        <property name="xaProperties">
            <props>
                <prop key="URL">${jdbca.url}</prop>
                <prop key="user">${jdbca.username}</prop>
                <prop key="password">${jdbca.password}</prop>
            </props>
        </property>
    </bean>
配置sessionfactory




点击(此处)折叠或打开
<!-- 配置mybatis的SessionFactory -->
    <bean id="sqlSessionFactoryB" class="org.mybatis.spring.SqlSessionFactoryBean">
        <!--mybatis的总配置文件-->
<property name="configLocation" value="classpath:configurationb.xml"/>
<!--扫描mybatis的写sql的mapper文件-->
        <property name="mapperLocations" value="classpath*:/com/suntel/linkup/model/mappert/*Mapper.xml"/>
<!----选择数据源------>
        <property name="dataSource" ref="dataSourceB" />
    </bean>


    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="configLocation" value="classpath:configuration.xml"/>
        <property name="mapperLocations" value="classpath*:/com/suntel/linkup/model/mapper/*Mapper.xml"/>
        <property name="dataSource" ref="dataSource" />
    </bean>
为dao中选择相应的SqlSessionTemplate,给sqlsession注入相应的session工厂,这样dao和有相应的数据库可以连接了


点击(此处)折叠或打开
<bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate"> 
            <constructor-arg index="0" ref="sqlSessionFactory" /> 
        </bean> 
        <bean id="sqlSessionb" class="org.mybatis.spring.SqlSessionTemplate"> 
            <constructor-arg index="0" ref="sqlSessionFactoryB" /> 
        </bean>
举例说下关于dao的调用,注意下划线的地方;


点击(此处)折叠或打开
private SqlSession sqlSessionb;
    
    public T create(String sql_ID,T entity) {
        this.getSqlSessionb().insert(sql_ID, entity);
        return entity;
    }
    public SqlSession getSqlSessionb() {
        return sqlSessionb;
    }


    public void setSqlSessionb(SqlSession sqlSessionb) {
        this.sqlSessionb = sqlSessionb;
    }
这儿得sqlSessionb是和spring配置文件中的
<bean id="sqlSessionb" class="org.mybatis.spring.SqlSessionTemplate"> 
            <constructor-arg index="0" ref="sqlSessionFactoryB" /> 
        </bean> 相对用的,名称和bean中的id要相同,
调用另一个数据session也是一样的原理:private SqlSession sqlSession;
    public SqlSession getSqlSession() {
        return sqlSession;
    }


    public void setSqlSession(SqlSession sqlSession) {
        this.sqlSession = sqlSession;
    }
<bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate"> 
            <constructor-arg index="0" ref="sqlSessionFactory" /> 
 </bean>
配置事务


点击(此处)折叠或打开
<!-- 事务这块用spring管理atomikos -->
    <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="springTransactionManager"
        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> 
    <!-- 支持 @AspectJ 标记--> 
     <aop:aspectj-autoproxy />
    
 
        <!-- 以AspectJ方式 定义 AOP -->
    <aop:config proxy-target-class="true">
        <aop:advisor pointcut="execution(* com.suntel.linkup.service..*.*(..))" advice-ref="txAdvice"/>
    </aop:config>
    
     <!-- 配置事务传播特性:如果存在一个事务,则支持当前事务。如果没有事务则开启:REQUIRED -->
    <tx:advice id="txAdvice" transaction-manager="springTransactionManager">
        <tx:attributes>
           <tx:method name="add*" propagation="REQUIRED" rollback-for="Exception"/>
           <tx:method name="save*" propagation="REQUIRED" rollback-for="Exception"/>
           <tx:method name="insert*" propagation="REQUIRED" rollback-for="Exception"/>
           <tx:method name="create*" propagation="REQUIRED" rollback-for="Exception"/>
           <tx:method name="del*" propagation="REQUIRED" rollback-for="Exception"/>
           <tx:method name="update*" propagation="REQUIRED" rollback-for="Exception"/>
           <tx:method name="modify*" propagation="REQUIRED" rollback-for="Exception"/>
           <tx:method name="*" read-only="true"/>
       </tx:attributes>
    </tx:advice>


好了,代码贴完了,可以试着运行了。
0 0