<?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>
<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>