dynamic-datasource-mybatis.xml 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xmlns:aop="http://www.springframework.org/schema/aop"
  5. xsi:schemaLocation="http://www.springframework.org/schema/beans
  6. http://www.springframework.org/schema/beans/spring-beans.xsd
  7. http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">
  8. <bean id="dataSourceAdplatform" class="com.alibaba.druid.pool.DruidDataSource"
  9. init-method="init" destroy-method="close">
  10. <property name="driverClassName" value="com.mysql.jdbc.Driver" />
  11. <property name="url" value="${spring.adplatform.jdbcUrl}" />
  12. <property name="username" value="wx2016_longvideo" />
  13. <property name="password" value="wx2016_longvideoP@assword1234" />
  14. <property name="maxActive" value="20" />
  15. <property name="initialSize" value="1" />
  16. <property name="maxWait" value="60000" />
  17. <property name="minIdle" value="3" />
  18. <property name="connectionProperties" value="config.decrypt=true;clientEncoding=UTF-8" />
  19. <property name="connectionInitSqls" value="set names utf8mb4;" />
  20. <property name="filters" value="stat" />
  21. <property name="timeBetweenEvictionRunsMillis" value="600000" />
  22. <property name="minEvictableIdleTimeMillis" value="300000" />
  23. <property name="validationQuery" value="select 1" />
  24. <property name="testWhileIdle" value="true" />
  25. <property name="testOnBorrow" value="false" />
  26. <property name="testOnReturn" value="false" />
  27. <property name="maxOpenPreparedStatements" value="20" />
  28. <property name="removeAbandoned" value="true" />
  29. <property name="removeAbandonedTimeout" value="1800" />
  30. <property name="logAbandoned" value="true" />
  31. </bean>
  32. <bean id="dataSourceOriginal" class="com.alibaba.druid.pool.DruidDataSource"
  33. init-method="init" destroy-method="close">
  34. <property name="driverClassName" value="com.mysql.jdbc.Driver" />
  35. <property name="url" value="${spring.datasource.jdbcUrl}" />
  36. <property name="username" value="${spring.datasource.username}" />
  37. <property name="password" value="${spring.datasource.password}" />
  38. <property name="maxActive" value="20" />
  39. <property name="initialSize" value="1" />
  40. <property name="maxWait" value="60000" />
  41. <property name="minIdle" value="3" />
  42. <property name="connectionProperties" value="config.decrypt=true;clientEncoding=UTF-8" />
  43. <property name="connectionInitSqls" value="set names utf8mb4;" />
  44. <property name="filters" value="stat" />
  45. <property name="timeBetweenEvictionRunsMillis" value="600000" />
  46. <property name="minEvictableIdleTimeMillis" value="300000" />
  47. <property name="validationQuery" value="select 1" />
  48. <property name="testWhileIdle" value="true" />
  49. <property name="testOnBorrow" value="false" />
  50. <property name="testOnReturn" value="false" />
  51. <property name="maxOpenPreparedStatements" value="20" />
  52. <property name="removeAbandoned" value="true" />
  53. <property name="removeAbandonedTimeout" value="1800" />
  54. <property name="logAbandoned" value="true" />
  55. </bean>
  56. <!--动态数据源的配置-->
  57. <bean id="dynamicDataSource" class=" com.tzld.piaoquan.ad.datasource.DynamicDataSource">
  58. <property name="targetDataSources">
  59. <map key-type="java.lang.String">
  60. <entry key="dataSourceAdplatform" value-ref="dataSourceAdplatform" />
  61. <entry key="dataSourceOriginal" value-ref="dataSourceOriginal" />
  62. </map>
  63. </property>
  64. </bean>
  65. <bean id="dataSourceChangeAspect" class="com.tzld.piaoquan.ad.datasource.DataSourceAspect">
  66. <property name="defaultDataSource" value="dataSourceOriginal"></property>
  67. <property name="targetDataSources">
  68. <map key-type="java.lang.String">
  69. <entry key="dataSourceAdplatform" value="dataSourceAdplatform"/>
  70. <entry key="dataSourceOriginal" value="dataSourceOriginal"/>
  71. </map>
  72. </property>
  73. </bean>
  74. <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
  75. <property name="dataSource" ref="dynamicDataSource" />
  76. <property name="configLocation" value="classpath:mybatis-config.xml" />
  77. <property name="mapperLocations" value="classpath:mapper/**/*.xml" />
  78. </bean>
  79. <!-- 指定Mapper接口所在的包 -->
  80. <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
  81. <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory" />
  82. <property name="basePackage" value="com.tzld.piaoquan.ad.dao.mapper" />
  83. </bean>
  84. <!-- 指定事务-->
  85. <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
  86. <property name="dataSource" ref="dynamicDataSource" />
  87. </bean>
  88. <!-- 一定要在事务之前切换数据源,不然则会失效-->
  89. <aop:config>
  90. <aop:pointcut id="serviceAop" expression="execution(* com.tzld.piaoquan.ad.service..*.*(..))" />
  91. <aop:aspect ref="dataSourceChangeAspect" order="0">
  92. <aop:before method="doBefore" pointcut-ref="serviceAop"/>
  93. <aop:after-returning method="doAfterReturning" pointcut-ref="serviceAop"/>
  94. </aop:aspect>
  95. </aop:config>
  96. <!--配置事务模板对象 编程式事务 模板-->
  97. <bean id="transactionTemplate" class="org.springframework.transaction.support.TransactionTemplate">
  98. <property name="transactionManager" ref="transactionManager"/>
  99. </bean>
  100. </beans>