spring + mybatis实现读写分离

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
        xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
        xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
        http://www.springframework.org/schema/aop
        http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
        http://www.springframework.org/schema/tx  
        http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context-3.0.xsd">

        <!-- 导入属性配置文件 -->
        <context:property-placeholder location="classpath*:*.properties" />

    <bean id="abstractDataSource" abstract="true"
                class="com.mchange.v2.c3p0.ComboPooledDataSource"
                destroy-method="close">
                <property name="driverClass" value="com.mysql.jdbc.Driver" />
                <property name="user" value="root" />
                <property name="password" value="" />
        </bean>

        <bean id="readDS" parent="abstractDataSource">
                <property name="jdbcUrl" value="jdbc:mysql://localhost:3306/test" />
        </bean>
       
        <bean id="writeDS" parent="abstractDataSource">
                <property name="jdbcUrl" value="jdbc:mysql://localhost:3306/test" />
        </bean>
       
        <!--简单的一个master和一个slaver 读写分离的数据源 -->
        <bean id="routingDS" class="com.test.rwmybatis.RoutingDataSource">
            <property name="targetDataSources">
                 <map key-type="java.lang.String">
                     <entry key="read" value-ref="readDS"></entry>
                     <entry key="write" value-ref="writeDS"></entry>
                 </map>
            </property>
            <property name="defaultTargetDataSource" ref="writeDS"></property>
        </bean>
       
        <!-- 适用于一个master和多个slaver的场景,并用roundrobin做负载均衡 -->
        <bean id="roundRobinDs"  class="com.test.rwmybatis.RoundRobinRWRoutingDataSource">
              <property name="writeDataSource"  ref="writeDS"></property>
              <property name="readDataSoures">
                  <list>
                      <ref bean="readDS"/>
                      <ref bean="readDS"/>
                      <ref bean="readDS"/>
                  </list>
              </property>
              <property name="readKey" value="READ"></property>
              <property name="writeKey" value="WRITE"></property>
        </bean>
       
        <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
                <property name="dataSource" ref="routingDS" />
                <property name="configLocation" value="classpath:mybatis-config.xml" />
                <!-- mapper和resultmap配置路径 -->
                <property name="mapperLocations">
                        <list>
                                <value>classpath:com/test/rwmybatis/mapper/**/*-Mapper.xml
                                </value>
                        </list>
                </property>
        </bean>
       
        <bean id="sqlSessionTemplate" class="com.test.rwmybatis.RWSqlSessionTemplate">
      <constructor-arg ref="sqlSessionFactory" />
    </bean>
        <!-- 通过扫描的模式,扫描目录下所有的mapper, 根据对应的mapper.xml为其生成代理类-->
        <bean id="mapper" class="com.test.rwmybatis.RWMapperScannerConfigurer">
                <property name="basePackage" value="com.test.rwmybatis.mapper" />
                <property name="sqlSessionTemplate" ref="sqlSessionTemplate"></property>
        </bean>

<!--    <bean id="monitor" class="org.springframework.aop.interceptor.PerformanceMonitorInterceptor"></bean> -->
<!--    <aop:config> -->
<!--       <aop:pointcut expression="execution(* com.taofang.smc.persistence..*.*(..))"  id="my_pc"/> -->
<!--       <aop:advisor advice-ref="monitor" pointcut-ref="my_pc"/> -->
<!--    </aop:config> -->
</beans>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值