項目中采用SpringMVC+Mybatis的架構,資料庫采用mysql,采用的配置檔案為形式為無單獨Mybatis配置檔案的那一種,套用網上的一句話叫做“spring和MyBatis完美整合,不需要mybatis的配置映射檔案”。
我得出的結論有兩種可能:
1. mysql表引擎不正确,隻有InnoDB可以支援事務
2. 配置檔案有問題,SpringMVC在處理注解資訊的時候發生的沖突。下面貼出項目中的正确配置
a. web.xml中
<!-- Spring和mybatis的配置檔案 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:conf/spring-mybatis.xml</param-value>
</context-param>
<!-- Spring MVC servlet -->
<span style="white-space:pre"> </span><servlet>
<span style="white-space:pre"> </span><servlet-name>SpringMVC</servlet-name>
<span style="white-space:pre"> </span><servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<span style="white-space:pre"> </span><init-param>
<span style="white-space:pre"> </span><param-name>contextConfigLocation</param-name>
<span style="white-space:pre"> </span><param-value>classpath:conf/spring-mvc.xml</param-value>
<span style="white-space:pre"> </span></init-param>
<span style="white-space:pre"> </span><load-on-startup>1</load-on-startup>
<span style="white-space:pre"> </span><async-supported>true</async-supported>
<span style="white-space:pre"> </span></servlet>
其中有兩個配置檔案
b. spring-mybatis.xml 中
<mvc:annotation-driven />
<!-- 自動掃描 -->
<context:component-scan base-package="com.XXX">
<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
</context:component-scan>
<tx:annotation-driven transaction-manager="transactionManager" />
<tx:annotation-driven transaction-manager="transactionManagerBMS" />
c, spring-mvc.xml 中
<mvc:annotation-driven />
<context:component-scan base-package="com.XXX">
<context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Service"/>
</context:component-scan>
配置完成之後,事務就可以生效了