標籤:
項目中使用SpringMVC+myBatis + mySQL 開發 ,需要交易管理功能 , 配置如下
1.service.xml配置
<?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:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd"> <context:property-placeholder location="WEB-INF/conf/jdbc.properties"/> <context:component-scan base-package="dao"/> <!-- 注,此處自動掃描註解的時候,不去掃描Controller--> <context:component-scan base-package="service"/> <context:component-scan base-package="model"/> <tx:annotation-driven transaction-manager="transactionManager" />
<!-- tx:annotation-driver標記,是因為這裡用的是註解事務,這個開啟後spring會在dao,service,model下面所有類中掃描含有@Transactional的標識,並產生相應的代理(預設是基於介面的JDK動態代理),我是在service層進行交易處理,只在service層添加@Transactional標識-->
<!-- 配置資料來源 --> <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close"> <property name="driverClass"> <value>${jdbc.driverClassName}</value> </property> <property name="jdbcUrl"> <value>${jdbc.url}</value> </property> <property name="user"> <value>${jdbc.username}</value> </property> <property name="password"> <value>${jdbc.password}</value> </property> <!--串連池中保留的最小串連數。 --> <property name="minPoolSize"> <value>5</value> </property> <!--串連池中保留的最大串連數。Default: 15 --> <property name="maxPoolSize"> <value>30</value> </property> <!--初始化時擷取的串連數,取值應在minPoolSize與maxPoolSize之間。Default: 3 --> <property name="initialPoolSize"> <value>10</value> </property> <!--最大空閑時間,60秒內未使用則串連被丟棄。若為0則永不丟棄。Default: 0 --> <property name="maxIdleTime"> <value>60</value> </property> <!--當串連池中的串連耗盡的時候c3p0一次同時擷取的串連數。Default: 3 --> <property name="acquireIncrement"> <value>5</value> </property> <!--JDBC的標準參數,用以控制資料來源內載入的PreparedStatements數量。但由於預緩衝的statements 屬於單個connection而不是整個串連池。所以設定這個參數需要考慮到多方面的因素。 如果maxStatements與maxStatementsPerConnection均為0,則緩衝被關閉。Default: 0 --> <property name="maxStatements"> <value>0</value> </property> <!--每60秒檢查所有串連池中的空閑串連。Default: 0 --> <property name="idleConnectionTestPeriod"> <value>60</value> </property> <!--定義在從資料庫擷取新串連失敗後重複嘗試的次數。Default: 30 --> <property name="acquireRetryAttempts"> <value>30</value> </property> <!--擷取串連失敗將會引起所有等待串連池來擷取串連的線程拋出異常。但是資料來源仍有效 保留,並在下次調用getConnection()的時候繼續嘗試擷取串連。如果設為true,那麼在嘗試 擷取串連失敗後該資料來源將申明已斷開並永久關閉。Default: false --> <property name="breakAfterAcquireFailure"> <value>true</value> </property> <!--因效能消耗大請只在需要的時候使用它。如果設為true那麼在每個connection提交的 時候都將校正其有效性。建議使用idleConnectionTestPeriod或automaticTestTable 等方法來提升串連測試的效能。Default: false --> <property name="testConnectionOnCheckout"> <value>false</value> </property> </bean> <!-- 配置Jdbc模板 --> <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate"> <property name="dataSource" ref="dataSource"></property> </bean> <!-- mybatis檔案配置,掃描所有mapper檔案 --> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean" p:dataSource-ref="dataSource" p:configLocation="/WEB-INF/mybatis/mybatis.xml" /> <!-- configLocation為mybatis屬性 mapperLocations為所有mapper--> <!-- spring與mybatis整合配置,掃描所有dao --> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer" p:basePackage="dao" p:sqlSessionFactoryBeanName="sqlSessionFactory"/> <!-- 對資料來源進行交易管理 --> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager" p:dataSource-ref="dataSource"/></beans>
2.servlet.xml配置
<?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:mvc="http://www.springframework.org/schema/mvc" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd"> <context:component-scan base-package="controller"/> <!--注,此處自動掃描註解的時候,不去掃描Service --> <context:component-scan base-package="dao"/> <context:component-scan base-package="model"/> <!-- json處理--> <mvc:annotation-driven> <mvc:message-converters register-defaults="true"> <bean class="com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter"> <property name="supportedMediaTypes" value="text/html;charset=UTF-8"/> <property name="features"> <array> <value>WriteMapNullValue</value> <value>WriteNullStringAsEmpty</value> </array> </property> </bean> </mvc:message-converters> </mvc:annotation-driven> <!-- ViewResolver --> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/views/"/> <property name="suffix" value=".jsp"/> <property name="contentType"> <value>text/html;charset=UTF-8</value> </property> </bean></beans>
3.查看mysql引擎 是否為 InnoDB。
更多資料擷取
SpringMVC + myBatis + mySQL 全註解 事務配置