spring-mybatis.xml 5.56 KB
<?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:mvc="http://www.springframework.org/schema/mvc" xmlns:aop="http://www.springframework.org/schema/aop"
	   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/mvc
                        http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
                        http://www.springframework.org/schema/aop
                        http://www.springframework.org/schema/aop/spring-aop-3.1.xsd">

	<!-- yoho_passport 数据源 -->
	<bean id="yohobuylogsMasterDataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
		<property name="driverClassName" value="${jdbc.mysql.driver}" />
		<property name="url" value="${jdbc.mysql.yohopassport.master.url}" />
		<property name="username" value="${jdbc.mysql.yohopassport.username}" />
		<property name="password" value="${jdbc.mysql.yohopassport.password}" />
		<!-- 初始化连接大小 -->
		<property name="initialSize" value="${jdbc.mysql.initialSize}"></property>
		<!-- 连接池最大数量 -->
		<property name="maxActive" value="${jdbc.mysql.maxActive}"></property>
		<!-- 连接池最大空闲 -->
		<property name="maxIdle" value="${jdbc.mysql.maxIdle}"></property>
		<!-- 连接池最小空闲 -->
		<property name="minIdle" value="${jdbc.mysql.minIdle}"></property>
		<!-- 获取连接最大等待时间 -->
		<property name="maxWait" value="${jdbc.mysql.maxWait}"></property>
		<!--定时对线程池中的链接进行校验 -->
		<property name="testWhileIdle" value="${jdbc.mysql.testWhileIdle}"></property>
		<property name="timeBetweenEvictionRunsMillis" value="${jdbc.mysql.timeBetweenEvictionRunsMillis}"></property>
		<property name="validationQuery" value="${jdbc.mysql.validationQuery}"></property>
		<property name="testOnBorrow" value="${jdbc.mysql.testOnBorrow}"></property>
		<property name="testOnReturn" value="${jdbc.mysql.testOnReturn}"></property>

	</bean>
	<bean id="yohobuylogsSlaveDataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
		<property name="driverClassName" value="${jdbc.mysql.driver}" />
		<property name="url" value="${jdbc.mysql.yohopassport.slave.url}" />
		<property name="username" value="${jdbc.mysql.yohopassport.username}" />
		<property name="password" value="${jdbc.mysql.yohopassport.password}" />
		<!-- 初始化连接大小 -->
		<property name="initialSize" value="${jdbc.mysql.initialSize}"></property>
		<!-- 连接池最大数量 -->
		<property name="maxActive" value="${jdbc.mysql.maxActive}"></property>
		<!-- 连接池最大空闲 -->
		<property name="maxIdle" value="${jdbc.mysql.maxIdle}"></property>
		<!-- 连接池最小空闲 -->
		<property name="minIdle" value="${jdbc.mysql.minIdle}"></property>
		<!-- 获取连接最大等待时间 -->
		<property name="maxWait" value="${jdbc.mysql.maxWait}"></property>
		<!--定时对线程池中的链接进行校验 -->
		<property name="testWhileIdle" value="${jdbc.mysql.testWhileIdle}"></property>
		<property name="timeBetweenEvictionRunsMillis" value="${jdbc.mysql.timeBetweenEvictionRunsMillis}"></property>
		<property name="validationQuery" value="${jdbc.mysql.validationQuery}"></property>
		<property name="testOnBorrow" value="${jdbc.mysql.testOnBorrow}"></property>
		<property name="testOnReturn" value="${jdbc.mysql.testOnReturn}"></property>
	</bean>

	<bean id="dynamicDataSource" class="com.yoho.core.dal.datasource.DynamicDataSource">
		<property name="defaultTargetDataSource" ref="yohobuylogsMasterDataSource" />
		<property name="targetDataSources">
			<map>
				<entry key="yohobuylogsMasterDataSource" value-ref="yohobuylogsMasterDataSource" />
			</map>
		</property>
	</bean>

	<bean id="multipleDataSourceRouter" class="com.yoho.core.dal.datasource.MultiDataSourceRouter">
		<property name="defaultDBCluster" value="yohobuylogsDbCluster" />
		<property name="readOnlyInSlave" value="${jdbc.mysql.readonlyinslave:false}" />
		<property name="dbClusterSet">
			<map>
				<entry key="yohobuylogsDbCluster" value="yohobuylogsMasterDataSource,yohobuylogsMasterDataSource" />
			</map>
		</property>
		<property name="daoDbClusterMap">
			<map>

			</map>
		</property>
	</bean>

	<bean id="dataSourceMethodInterceptor" class="com.yoho.core.dal.datasource.DataSourceMethodInterceptor">
	</bean>

	<aop:config>
		<aop:pointcut id="daoPoint" expression="execution(* com.yoho.*.dal.*.*(..)) " />
		<aop:advisor pointcut-ref="daoPoint" advice-ref="dataSourceMethodInterceptor" />
	</aop:config>


	<!-- spring和MyBatis完美整合,不需要mybatis的配置映射文件 -->
	<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
		<property name="dataSource" ref="dynamicDataSource" />
		<!-- 自动扫描mapping.xml文件 -->
		<property name="mapperLocations" value="classpath*:META-INF/mybatis/*.xml"></property>
	</bean>
	<!-- DAO接口所在包名,Spring会自动查找其下的类 -->
	<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
		<property name="basePackage" value="com.yoho.*.dal" />
		<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property>
	</bean>

</beans>