<?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"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.2.xsd ">
<!-- 配置c3p0数据源 -->
<bean id="dataSourceOracle" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close">
<property name="driverClass" value="${datasource.driver}"/>
<property name="jdbcUrl" value="${datasource.url}"/>
<property name="user" value="${datasource.username}"/>
<property name="password" value="${datasource.password}"/>
<property name="acquireIncrement" value="${c3p0.acquireIncrement}"/>
<property name="initialPoolSize" value="${c3p0.initialPoolSize}"/>
<property name="minPoolSize" value="${c3p0.minPoolSize}"/>
<property name="maxPoolSize" value="${c3p0.maxPoolSize}"/>
<property name="maxIdleTime" value="${c3p0.maxIdleTime}"/>
<property name="idleConnectionTestPeriod" value="${c3p0.idleConnectionTestPeriod}"/>
<property name="maxStatements" value="${c3p0.maxStatements}"/>
<property name="numHelperThreads" value="${c3p0.numHelperThreads}"/>
<property name="preferredTestQuery" value="${c3p0.preferredTestQuery}"/>
<property name="testConnectionOnCheckout" value="${c3p0.testConnectionOnCheckout}"/>
</bean>
<bean id="dataSourceMySQL" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close">
<property name="driverClass" value="com.mysql.jdbc.Driver"/>
<property name="jdbcUrl" value="jdbc:mysql://172.31.108.178:3306/world?useUnicode=true&characterEncoding=UTF-8&zeroDateTimeBehavior=convertToNull"/>
<property name="user" value="root"/>
<property name="password" value="jp2011"/>
<property name="acquireIncrement" value="${c3p0.acquireIncrement}"/>
<property name="initialPoolSize" value="${c3p0.initialPoolSize}"/>
<property name="minPoolSize" value="${c3p0.minPoolSize}"/>
<property name="maxPoolSize" value="${c3p0.maxPoolSize}"/>
<property name="maxIdleTime" value="${c3p0.maxIdleTime}"/>
<property name="idleConnectionTestPeriod" value="${c3p0.idleConnectionTestPeriod}"/>
<property name="maxStatements" value="${c3p0.maxStatements}"/>
<property name="numHelperThreads" value="${c3p0.numHelperThreads}"/>
<property name="preferredTestQuery" value="${c3p0.preferredTestQuery}"/>
<property name="testConnectionOnCheckout" value="${c3p0.testConnectionOnCheckout}"/>
</bean>
<bean id="multipleDataSource" class="com.hoo.framework.spring.support.MultipleDataSource">
<property name="defaultTargetDataSource" ref="dataSourceOracle"/>
<property name="targetDataSources">
<map>
<!-- 注意这里的value是和上面的DataSource的id对应,key要和下面的CustomerContextHolder中的常量对应 -->
<entry value-ref="dataSourceOracle" key="oracleDataSource"/>
<entry value-ref="dataSourceMySQL" key="mySqlDataSource"/>
</map>
</property>
</bean>
<!-- Annotation 配置sessionFactory,配置数据库连接,注入hibernate数据库配置 -->
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<property name="dataSource" ref="multipleDataSource"/>
<property name="packagesToScan" value="com.hoo.**.entity"/>
<property name="hibernateProperties">
<props>
<!-- prop key="hibernate.dialect">org.hibernate.dialect.OracleDialect</prop-->
<!-- 链接释放策略 on_close | after_transaction | after_statement | auto -->
<prop key="hibernate.connection.release_mode">after_transaction</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.format_sql">true</prop>
<!--prop key="hibernate.hbm2ddl.auto">update</prop-->
</props>
</property>
<!-- property name="configLocation" value="classpath:hibernate.cfg.xml" /-->
<property name="namingStrategy">
<bean class="com.hoo.framework.hibernate.PrefixedNamingStrategy" />
</property>
</bean>
<!-- 事务管理器,注入sessionFactory -->
<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
<!-- 配置事务的传播特性 -->
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="add*" propagation="REQUIRED" rollback-for="java.lang.Exception"/>
<tx:method name="edit*" propagation="REQUIRED" rollback-for="java.lang.Exception"/>
<tx:method name="remove*" propagation="REQUIRED" rollback-for="java.lang.Exception"/>
<tx:method name="insert*" propagation="REQUIRED" rollback-for="java.lang.Exception"/>
<tx:method name="save*" propagation="REQUIRED" rollback-for="java.lang.Exception"/>
<tx:method name="update*" 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="execute*" propagation="REQUIRED" rollback-for="java.lang.Exception"/>
<tx:method name="*" read-only="true" />
</tx:attributes>
</tx:advice>
<!-- 配置那些类、方法纳入到事务的管理 -->
<aop:config>
<aop:pointcut expression="execution(* com.hoo.**.service.impl.*.*(..))" id="transactionManagerMethod"/>
<aop:advisor advice-ref="txAdvice" pointcut-ref="transactionManagerMethod" />
</aop:config>
<!-- 配置SqlSessionFactoryBean -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="multipleDataSource"/>
<property name="configLocation" value="classpath:mybatis.xml"/>
<!-- mapper和resultmap配置路径 -->
<property name="mapperLocations">
<list>
<!-- 表示在com.hoo目录下的任意包下的resultmap包目录中,以-resultmap.xml或-mapper.xml结尾所有文件 -->
<value>classpath:com/hoo/framework/mybatis/mybatis-common.xml</value>
<value>classpath:com/hoo/**/resultmap/*-resultmap.xml</value>
<value>classpath:com/hoo/**/mapper/*-mapper.xml</value>
<value>classpath:com/hoo/**/mapper/**/*-mapper.xml</value>
</list>
</property>
</bean>
<!-- 通过扫描的模式,扫描目录在com/hoo/任意目录下的mapper目录下,所有的mapper都需要继承SqlMapper接口的接口 -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.hoo.**.mapper"/>
<property name="markerInterface" value="com.hoo.framework.mybatis.SqlMapper"/>
</bean>
</beans>
上面分配配置了Oracle和MySQL数据源,MultipleDataSource为自定义的DataSource,它是继承AbstractRoutingDataSource实现抽象方法即可。