歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> Spring tomcat jdbc pool mybatis 集成配置

Spring tomcat jdbc pool mybatis 集成配置

日期:2017/3/1 10:06:15   编辑:Linux編程

最近項目用到了Spring、Tomcat7自帶的數據庫連接池tomcat jdbc pool 、mybatis,現將三者集成的配置記錄一下,以備不時之需。
所需的jar包:

spring框架需要的jar包(這裡就不詳細說了)
mybatis-3.1.1.jar
mybatis-spring-1.1.1.jar
mysql-connector-java-5.1.15-bin.jar
tomcat-jdbc.jar
tomcat-juli.jar

配置文件如下:

  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:p="http://www.springframework.org/schema/p"
  5. xmlns:aop="http://www.springframework.org/schema/aop"
  6. xmlns:tx="http://www.springframework.org/schema/tx"
  7. xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
  8. http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
  9. http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">
  10. <!--tomcat jdbc pool數據源配置-->
  11. <bean id="dataSource" class="org.apache.tomcat.jdbc.pool.DataSource" destroy-method="close">
  12. <property name="poolProperties">
  13. <bean class="org.apache.tomcat.jdbc.pool.PoolProperties">
  14. <property name="driverClassName" value="com.mysql.jdbc.Driver"/>
  15. <property name="url" value="jdbc:mysql://localhost:3306/test?autoReconnect=true&useUnicode=true&characterEncoding=UTF-8"/>
  16. <property name="username" value="root"/>
  17. <property name="password" value=""/>
  18. <property name="jmxEnabled" value="true"/>
  19. <property name="testWhileIdle" value="true"/>
  20. <property name="testOnBorrow" value="true"/>
  21. <property name="testOnReturn" value="false"/>
  22. <property name="validationInterval" value="30000"/>
  23. <property name="validationQuery" value="SELECT 1"/>
  24. <property name="timeBetweenEvictionRunsMillis" value="30000"/>
  25. <property name="maxActive" value="100"/>
  26. <property name="initialSize" value="10"/>
  27. <property name="maxWait" value="10000"/>
  28. <property name="minEvictableIdleTimeMillis" value="30000"/>
  29. <property name="minIdle" value="10"/>
  30. <property name="logAbandoned" value="false"/>
  31. <property name="removeAbandoned" value="true"/>
  32. <property name="removeAbandonedTimeout" value="60"/>
  33. <property name="jdbcInterceptors" value="org.apache.tomcat.jdbc.pool.interceptor.ConnectionState;org.apache.tomcat.jdbc.pool.interceptor.StatementFinalizer"/>
  34. </bean>
  35. </property>
  36. </bean>
  37. <!-- 配置事務管理器,注意這裡的dataSource和SqlSessionFactoryBean的dataSource要一致,不然事務就沒有作用了 -->
  38. <bean id="transactionManager"
  39. class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
  40. <property name="dataSource" ref="dataSource" />
  41. </bean>
  42. <!-- 定義攔截器,用來指定事務屬性、級別和異常處理 -->
  43. <bean id="transactionInterceptor" class="org.springframework.transaction.interceptor.TransactionInterceptor">
  44. <property name="transactionManager" ref="transactionManager"></property>
  45. <property name="transactionAttributes">
  46. <props>
  47. <prop key="save*">PROPAGATION_REQUIRED,-Exception</prop>
  48. <prop key="update*">PROPAGATION_REQUIRED,-Exception</prop>
  49. <prop key="delete*"> PROPAGATION_REQUIRED,-Exception</prop>
  50. <prop key="find*">PROPAGATION_REQUIRED,readOnly</prop>
  51. <prop key="get*">PROPAGATION_REQUIRED,readOnly</prop>
  52. </props>
  53. </property>
  54. </bean>
  55. <!-- 用來定義哪些類需要事務管理 spring事務動態代理類 BeanNameAutoProxyCreator 根據類名自動代理,接受表達式 -->
  56. <bean id="BeanProxy" class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator">
  57. <property name="beanNames">
  58. <!-- 對類名以Service結尾的類進行代理 -->
  59. <value>*Service</value>
  60. </property>
  61. <!-- 對代理類進行加載攔截器(實現通知的過程) -->
  62. <property name="interceptorNames">
  63. <list>
  64. <value>transactionInterceptor</value>
  65. </list>
  66. </property>
  67. </bean>
  68. <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
  69. <property name="configLocation" value="classpath:mybatis-config.xml" />
  70. <property name="dataSource" ref="dataSource" />
  71. </bean>
  72. <bean id="authorityDao" class="com.hongdian.ithings.authoritymanagement.dao.AuthorityDaoImpl">
  73. <property name="sessionFactory" ref="sqlSessionFactory"/>
  74. </bean>
  75. <bean id="authorityService" class="com.hongdian.ithings.authoritymanagement.service.AuthorityServiceImpl">
  76. <property name="authorityDao" ref="authorityDao"/>
  77. </bean>
  78. <bean class="com.hongdian.ithings.authoritymanagement.common.SpringContext"></bean>
  79. <bean id="taskExecutor" class="org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor">
  80. <!--線程池活躍的線程數-->
  81. <property name="corePoolSize" value="5" />
  82. <!--線程池最大活躍的線程數-->
  83. <property name="maxPoolSize" value="50" />
  84. <!--任務隊列的最大容量-->
  85. <property name="queueCapacity" value="5" />
  86. </bean>
  87. <bean id="authorityComponent" class="com.hongdian.ithings.authoritymanagement.communication.AuthorityComponent">
  88. <property name="taskExecutor" ref="taskExecutor"/>
  89. </bean>
  90. </beans>
Copyright © Linux教程網 All Rights Reserved