spring,mybatis,druid,多数据源配置

来源:互联网 发布:八字合婚 知乎 编辑:程序博客网 时间:2024/04/29 08:29

1、实现多数据源,夸库查询;

2、通过配置spring-mybatis.xml中的事务和aop来完成一个service层下多个数据源事务支持异常同时回滚;

<?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:aop="http://www.springframework.org/schema/aop"xmlns:tx="http://www.springframework.org/schema/tx" xmlns:jdbc="http://www.springframework.org/schema/jdbc"xmlns:context="http://www.springframework.org/schema/context"xsi:schemaLocation="        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd        http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-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/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd"><context:annotation-config /><bean name="dataSource_c" class="com.alibaba.druid.pool.DruidDataSource"><property name="driverClassName"><value>com.mysql.jdbc.Driver</value></property><property name="url"><value>${jdbc_url_gx_data}</value></property><property name="username"><value>${jdbc_username_gx_data}</value></property><property name="password"><value>${jdbc_password_gx_data}</value></property><property name="initialSize" value="5" /><property name="maxActive" value="20" /><property name="minIdle" value="3" /><property name="maxWait" value="60000" /><!-- 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒 --><property name="timeBetweenEvictionRunsMillis" value="60000" /><!-- 配置一个连接在池中最小生存的时间,单位是毫秒 --><property name="minEvictableIdleTimeMillis" value="300000" /><property name="validationQuery" value="SELECT 'x'" /><property name="testWhileIdle" value="true" /><property name="testOnBorrow" value="false" /><property name="testOnReturn" value="false" /><!-- 打开PSCache,并且指定每个连接上PSCache的大小 --><property name="poolPreparedStatements" value="true" /><property name="maxPoolPreparedStatementPerConnectionSize"value="20" /><!-- 配置监控统计拦截的filters --><property name="filters" value="stat" /></bean><bean id="sqlSessionFactory_c" class="org.mybatis.spring.SqlSessionFactoryBean"><property name="dataSource" ref="dataSource_c" /><!-- 自动扫描entity目录, 省掉Configuration.xml里的手工配置 --><property name="mapperLocations" value="classpath:com/tm/openapi/newmapper/*.xml" /></bean><bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"><property name="basePackage" value="com/tm/openapi/newmapper" /><property name="sqlSessionFactoryBeanName" value="sqlSessionFactory_c" /></bean><!-- 配置事务管理器 gx_data 默认事物 --><bean id="transactionManager"class="org.springframework.jdbc.datasource.DataSourceTransactionManager"><property name="dataSource" ref="dataSource_c" /><qualifier value="gx_data" /></bean><!-- 全注解方式 <tx:annotation-driven transaction-manager="transactionManager" /> --><tx:advice id="userTxAdvice" transaction-manager="transactionManager"><tx:attributes><tx:method name="delete*" propagation="REQUIRED" read-only="false"rollback-for="Exception" /><tx:method name="insert*" propagation="REQUIRED" read-only="false"rollback-for="Exception" /><tx:method name="save*" propagation="REQUIRED" read-only="false"rollback-for="Exception" /><tx:method name="add*" propagation="REQUIRED" read-only="false"rollback-for="Exception" /><tx:method name="update*" propagation="REQUIRED" read-only="false"rollback-for="Exception" /><tx:method name="del*" propagation="REQUIRED" read-only="false"rollback-for="Exception" /><tx:method name="batch*" propagation="REQUIRED" read-only="false"rollback-for="Exception" /><tx:method name="bach*" propagation="REQUIRED" read-only="false"rollback-for="Exception" /><tx:method name="cancel*" propagation="REQUIRED" read-only="false"rollback-for="Exception" /><tx:method name="dis*" propagation="REQUIRED" read-only="false"rollback-for="Exception" /><tx:method name="en*" propagation="REQUIRED" read-only="false"rollback-for="Exception" /><tx:method name="query*" read-only="true" /><tx:method name="find*" read-only="true" /><tx:method name="get*" read-only="true" /><tx:method name="select*" read-only="true" /><tx:method name="count*" read-only="true" /></tx:attributes></tx:advice><aop:config><aop:pointcut id="pc"expression="execution(public * com.tm.openapi.service..*(..))" /> <!--把事务控制在Service层 --><aop:advisor pointcut-ref="pc" advice-ref="userTxAdvice" /></aop:config><bean name="dataSource" class="com.alibaba.druid.pool.DruidDataSource"><property name="driverClassName"><value>com.mysql.jdbc.Driver</value></property><property name="url"><value>${jdbc_url_gx}</value></property><property name="username"><value>${jdbc_username_gx}</value></property><property name="password"><value>${jdbc_password_gx}</value></property><property name="initialSize" value="5" /><property name="maxActive" value="20" /><!-- <property name="maxIdle" value="20" /> --><property name="minIdle" value="3" /><property name="maxWait" value="60000" /><!-- 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒 --><property name="timeBetweenEvictionRunsMillis" value="60000" /><!-- 配置一个连接在池中最小生存的时间,单位是毫秒 --><property name="minEvictableIdleTimeMillis" value="300000" /><property name="validationQuery" value="SELECT 'x'" /><property name="testWhileIdle" value="true" /><property name="testOnBorrow" value="false" /><property name="testOnReturn" value="false" /><!-- 打开PSCache,并且指定每个连接上PSCache的大小 --><property name="poolPreparedStatements" value="true" /><property name="maxPoolPreparedStatementPerConnectionSize"value="20" /><!-- 配置监控统计拦截的filters --><property name="filters" value="stat" /></bean><!-- myBatis文件 --><bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"><property name="dataSource" ref="dataSource" /><!-- 自动扫描entity目录, 省掉Configuration.xml里的手工配置 --><property name="mapperLocations" value="classpath:com/tm/openapi/mapper/*.xml" /></bean><bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"><property name="basePackage" value="com/tm/openapi/mapper" /><property name="sqlSessionFactoryBeanName" value="sqlSessionFactory" /></bean><!-- 配置事务管理器 gx --><bean id="transactionManager2"class="org.springframework.jdbc.datasource.DataSourceTransactionManager"><property name="dataSource" ref="dataSource" /><qualifier value="gx" /></bean><!-- 全注解方式 需加上@Transactional <tx:annotation-driven transaction-manager="transactionManager2" /> --><tx:advice id="userTxAdviceB" transaction-manager="transactionManager2"><tx:attributes><tx:method name="delete*" propagation="REQUIRED" read-only="false"rollback-for="Exception" /><tx:method name="insert*" propagation="REQUIRED" read-only="false"rollback-for="Exception" /><tx:method name="save*" propagation="REQUIRED" read-only="false"rollback-for="Exception" /><tx:method name="add*" propagation="REQUIRED" read-only="false"rollback-for="Exception" /><tx:method name="update*" propagation="REQUIRED" read-only="false"rollback-for="Exception" /><tx:method name="del*" propagation="REQUIRED" read-only="false"rollback-for="Exception" /><tx:method name="batch*" propagation="REQUIRED" read-only="false"rollback-for="Exception" /><tx:method name="bach*" propagation="REQUIRED" read-only="false"rollback-for="Exception" /><tx:method name="cancel*" propagation="REQUIRED" read-only="false"rollback-for="Exception" /><tx:method name="dis*" propagation="REQUIRED" read-only="false"rollback-for="Exception" /><tx:method name="en*" propagation="REQUIRED" read-only="false"rollback-for="Exception" /><tx:method name="query*" read-only="true" /><tx:method name="find*" read-only="true" /><tx:method name="get*" read-only="true" /><tx:method name="select*" read-only="true" /><tx:method name="count*" read-only="true" /></tx:attributes></tx:advice><aop:config><aop:pointcut id="pc2"expression="execution(public * com.tm.openapi.service..*(..))" /><!--把事务控制在Service层 --><aop:advisor pointcut-ref="pc2" advice-ref="userTxAdviceB" /></aop:config><!-- 新增db --><bean name="dataSource_wms_ware" class="com.alibaba.druid.pool.DruidDataSource"><property name="driverClassName"><value>com.mysql.jdbc.Driver</value></property><property name="url"><value>${jdbc_url_wms_ware}</value></property><property name="username"><value>${jdbc_username_wms_ware}</value></property><property name="password"><value>${jdbc_password_wms_ware}</value></property><property name="initialSize" value="5" /><property name="maxActive" value="20" /><!-- <property name="maxIdle" value="20" /> --><property name="minIdle" value="3" /><property name="maxWait" value="60000" /><!-- 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒 --><property name="timeBetweenEvictionRunsMillis" value="60000" /><!-- 配置一个连接在池中最小生存的时间,单位是毫秒 --><property name="minEvictableIdleTimeMillis" value="300000" /><property name="validationQuery" value="SELECT 'x'" /><property name="testWhileIdle" value="true" /><property name="testOnBorrow" value="false" /><property name="testOnReturn" value="false" /><!-- 打开PSCache,并且指定每个连接上PSCache的大小 --><property name="poolPreparedStatements" value="true" /><property name="maxPoolPreparedStatementPerConnectionSize"value="20" /><!-- 配置监控统计拦截的filters --><property name="filters" value="stat" /></bean><bean id="sqlSessionFactory_wms_ware" class="org.mybatis.spring.SqlSessionFactoryBean"><property name="dataSource" ref="dataSource_wms_ware" /><!-- 自动扫描entity目录, 省掉Configuration.xml里的手工配置 --><property name="mapperLocations" value="classpath:com/tm/openapi/newmapper/*.xml" /></bean><bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"><property name="basePackage" value="com/tm/openapi/newmapper" /><property name="sqlSessionFactoryBeanName" value="sqlSessionFactory_wms_ware" /></bean><!-- 配置事务管理器 gx --><bean id="transactionManager3"class="org.springframework.jdbc.datasource.DataSourceTransactionManager"><property name="dataSource" ref="dataSource" /><qualifier value="wms_ware" /></bean><!-- 全注解方式 需加上@Transactional <tx:annotation-driven transaction-manager="transactionManager2" /> --><tx:advice id="userTxAdviceC" transaction-manager="transactionManager3"><tx:attributes><tx:method name="delete*" propagation="REQUIRED" read-only="false"rollback-for="Exception" /><tx:method name="insert*" propagation="REQUIRED" read-only="false"rollback-for="Exception" /><tx:method name="save*" propagation="REQUIRED" read-only="false"rollback-for="Exception" /><tx:method name="add*" propagation="REQUIRED" read-only="false"rollback-for="Exception" /><tx:method name="update*" propagation="REQUIRED" read-only="false"rollback-for="Exception" /><tx:method name="del*" propagation="REQUIRED" read-only="false"rollback-for="Exception" /><tx:method name="batch*" propagation="REQUIRED" read-only="false"rollback-for="Exception" /><tx:method name="bach*" propagation="REQUIRED" read-only="false"rollback-for="Exception" /><tx:method name="cancel*" propagation="REQUIRED" read-only="false"rollback-for="Exception" /><tx:method name="dis*" propagation="REQUIRED" read-only="false"rollback-for="Exception" /><tx:method name="en*" propagation="REQUIRED" read-only="false"rollback-for="Exception" /><tx:method name="query*" read-only="true" /><tx:method name="find*" read-only="true" /><tx:method name="get*" read-only="true" /><tx:method name="select*" read-only="true" /><tx:method name="count*" read-only="true" /></tx:attributes></tx:advice><aop:config><aop:pointcut id="pc3"expression="execution(public * com.tm.openapi.service..*(..))" /><!--把事务控制在Service层 --><aop:advisor pointcut-ref="pc3" advice-ref="userTxAdviceC" /></aop:config><!-- aop注解支持 --><aop:aspectj-autoproxy proxy-target-class="true" /></beans>


1 0
原创粉丝点击