applicationContext-jdbc.xml 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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. xmlns:tx="http://www.springframework.org/schema/tx"
  6. xmlns:context="http://www.springframework.org/schema/context"
  7. xsi:schemaLocation="
  8. http://www.springframework.org/schema/beans
  9. http://www.springframework.org/schema/beans/spring-beans.xsd
  10. http://www.springframework.org/schema/tx
  11. http://www.springframework.org/schema/tx/spring-tx.xsd
  12. http://www.springframework.org/schema/context
  13. http://www.springframework.org/schema/context/spring-context.xsd
  14. http://www.springframework.org/schema/aop
  15. http://www.springframework.org/schema/aop/spring-aop.xsd">
  16. <!-- 引入jdbc配置文件 -->
  17. <!--<context:property-placeholder location="classpath:jdbc.properties" />-->
  18. <!-- 配置进行解密 -->
  19. <bean id="propertyConfigurer" class="com.zheng.common.plugin.EncryptPropertyPlaceholderConfigurer">
  20. <property name="locations">
  21. <list>
  22. <value>classpath:jdbc.properties</value>
  23. <value>classpath:redis.properties</value>
  24. </list>
  25. </property>
  26. </bean>
  27. <!-- 主库数据源 -->
  28. <bean id="masterDataSource" class="com.alibaba.druid.pool.DruidDataSource" init-method="init"
  29. destroy-method="close">
  30. <!-- 基本属性 url、user、password -->
  31. <property name="driverClassName" value="${master.jdbc.driver}"/>
  32. <property name="url" value="${master.jdbc.url}"/>
  33. <property name="username" value="${master.jdbc.username}"/>
  34. <property name="password" value="${master.jdbc.password}"/>
  35. <!-- 配置初始化大小、最小、最大 -->
  36. <property name="initialSize" value="1"/>
  37. <property name="minIdle" value="1"/>
  38. <property name="maxActive" value="20"/>
  39. <!-- 配置获取连接等待超时的时间 -->
  40. <property name="maxWait" value="60000"/>
  41. <!-- 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒 -->
  42. <property name="timeBetweenEvictionRunsMillis" value="60000"/>
  43. <!-- 配置一个连接在池中最小生存的时间,单位是毫秒 -->
  44. <property name="minEvictableIdleTimeMillis" value="300000"/>
  45. <!-- 校验语句 -->
  46. <property name="validationQuery" value="SELECT 1"/>
  47. <property name="testWhileIdle" value="true"/>
  48. <property name="testOnBorrow" value="false"/>
  49. <property name="testOnReturn" value="false"/>
  50. <!-- 配置监控统计拦截的filters -->
  51. <property name="filters" value="stat"/>
  52. </bean>
  53. <!-- 从库数据源 -->
  54. <bean id="slaveDataSource" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close">
  55. <!-- 基本属性 url、user、password -->
  56. <property name="driverClassName" value="${slave.jdbc.driver}"/>
  57. <property name="url" value="${slave.jdbc.url}"/>
  58. <property name="username" value="${slave.jdbc.username}"/>
  59. <property name="password" value="${slave.jdbc.password}"/>
  60. <!-- 配置初始化大小、最小、最大 -->
  61. <property name="initialSize" value="1"/>
  62. <property name="minIdle" value="1"/>
  63. <property name="maxActive" value="20"/>
  64. <!-- 配置获取连接等待超时的时间 -->
  65. <property name="maxWait" value="60000"/>
  66. <!-- 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒 -->
  67. <property name="timeBetweenEvictionRunsMillis" value="60000"/>
  68. <!-- 配置一个连接在池中最小生存的时间,单位是毫秒 -->
  69. <property name="minEvictableIdleTimeMillis" value="300000"/>
  70. <!-- 校验语句 -->
  71. <property name="validationQuery" value="SELECT 1"/>
  72. <property name="testWhileIdle" value="true"/>
  73. <property name="testOnBorrow" value="false"/>
  74. <property name="testOnReturn" value="false"/>
  75. <!-- 配置监控统计拦截的filters -->
  76. <property name="filters" value="stat"/>
  77. </bean>
  78. <!-- 动态数据源 -->
  79. <bean id="dataSource" class="com.zheng.common.db.DynamicDataSource">
  80. <property name="targetDataSources">
  81. <map key-type="java.lang.String">
  82. <!-- 可配置多个数据源 -->
  83. <entry value-ref="masterDataSource" key="masterDataSource"></entry>
  84. <entry value-ref="slaveDataSource" key="slaveDataSource"></entry>
  85. </map>
  86. </property>
  87. <property name="defaultTargetDataSource" ref="masterDataSource"></property>
  88. </bean>
  89. <!-- 为Mybatis创建SqlSessionFactory,同时指定数据源 -->
  90. <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
  91. <property name="dataSource" ref="dataSource"/>
  92. <property name="configLocation" value="classpath:mybatis-config.xml"/>
  93. <property name="mapperLocations" value="classpath*:**/mapper/*Mapper.xml"/>
  94. </bean>
  95. <!-- Mapper接口所在包名,Spring会自动查找其下的Mapper -->
  96. <bean id="mapperScannerConfigurer" class="org.mybatis.spring.mapper.MapperScannerConfigurer">
  97. <property name="basePackage" value="**.mapper"/>
  98. <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>
  99. </bean>
  100. <!-- 事务管理器 -->
  101. <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
  102. <property name="dataSource" ref="dataSource"/>
  103. </bean>
  104. <!-- 启动注解事务 -->
  105. <tx:annotation-driven transaction-manager="transactionManager"/>
  106. </beans>