我用的是ssm框架,也就是(spring+springMVC+Mybatis)整理有两种方式可以实现事务控制
经过自己摸索,终于搞起了这个事务控制,于是想把这个分享给大家,希望大家少走一些弯路,不喜勿喷
首先给大家看看我的项目结构
web.xml的配置
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" version="2.5">
<display-name>LxjTest</display-name>
<servlet>
<servlet-name>SpringMVC</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring/springMVC-servlet.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>SpringMVC</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<filter>
<filter-name>character</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>utf-8</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>character</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<listener>
<listener-class>lxj.util.ListenerUtil</listener-class>
</listener>
<listener>
<listener-class>org.springframework.web.context.request.RequestContextListener</listener-class>
</listener>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring/springConfig.xml</param-value>
</context-param>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
一、通过@Transactional管理
1、springConfig.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:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.1.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.1.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.1.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd">
<!--添加数据库配置文件-->
<context:property-placeholder location="classpath:mybatis/mysql.properties" />
<!--设置数据源jdbc-->
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<!-- 数据库驱动 -->
<property name="driverClassName" value="${jdbc.driver}" />
<!-- 连接URL串 -->
<property name="url" value="${jdbc.url}" />
<!-- 连接用户名 -->
<property name="username" value="${jdbc.username}" />
<!-- 连接密码 -->
<property name="password" value="${jdbc.password}" />
</bean>
<!--配置sqlsessionFactory-->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<!--配置mybatis配置文件-->
<property name="configLocation" value="classpath:mybatis/mybatisConfig.xml" />
<property name="mapperLocations" value="classpath:mybatis/*Mapper.xml" />
<!--配置数据源-->
<property name="dataSource" ref="dataSource" />
</bean>
<!--配置自动扫描mapper,将mapper接口加入到IOC容器-->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory" />
<property name="basePackage" value="lxj.dao" />
</bean>
<!-- ================================事务控制相关配置================================================= -->
<!-- 事务管理器 -->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource" />
</bean>
<!-- 事务支持注解 -->
<tx:annotation-driven transaction-manager="transactionManager"/>
<!-- 加入对services的扫描 -->
<context:component-scan base-package="lxj.services.impl" />
</beans>
2、springMVC-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:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<!-- 加入对controller的扫描,不加入对services的扫描,不然事务管理会不起作用 -->
<context:component-scan base-package="lxj.controllers" />
<!-- 启动注解驱动 SpringMVC 功能 -->
<mvc:annotation-driven />
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<!--前缀 <property name="prefix" value="/views/" /> -->
<!--后缀 <property name="suffix" value=".jsp" /> -->
</bean>
<!-- 扫描静态文件 -->
<mvc:resources mapping="/images/**" location="/images/"/>
<mvc:resources mapping="/css/**" location="/css/"/>
<mvc:resources mapping="/js/**" location="/js/"/>
<mvc:resources mapping="/fonts/**" location="/fonts/"/>
<mvc:resources mapping="/upload/**" location="/upload/"/>
<mvc:resources mapping="/html/**" location="/html/"/>
<mvc:resources mapping="/bootstrap/**" location="/bootstrap/"/>
<!-- 配置文件上传,如果没有使用文件上传可以不用配置,当然如果不配,那么配置文件中也不必引入上传组件包 -->
<bean id="multipartResolver"
class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<!-- 默认编码 -->
<property name="defaultEncoding" value="utf-8" />
<!-- 文件大小最大值 -->
<property name="maxUploadSize" value="10485760000" />
<!-- 内存中的最大值 -->
<property name="maxInMemorySize" value="40960" />
</bean>
</beans>
3、services实现类
package lxj.services.impl;
import java.util.Map;
import lxj.dao.UserDAO;
import lxj.services.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
@Service("userService")
public class UserServiceImpl implements UserService {
@Autowired
private UserDAO userDAO;
@Override
@Transactional
public int saveUser(Map<String, Object> map) {
int n=0;
n=userDAO.saveUser(map);
n=userDAO.saveInfor(map);
return n;
}
}
二、通过AOP控制
1、springConfig.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:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.1.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.1.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.1.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd">
<!--添加数据库配置文件-->
<context:property-placeholder location="classpath:mybatis/mysql.properties" />
<!--设置数据源jdbc-->
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<!-- 数据库驱动 -->
<property name="driverClassName" value="${jdbc.driver}" />
<!-- 连接URL串 -->
<property name="url" value="${jdbc.url}" />
<!-- 连接用户名 -->
<property name="username" value="${jdbc.username}" />
<!-- 连接密码 -->
<property name="password" value="${jdbc.password}" />
</bean>
<!--配置sqlsessionFactory-->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<!--配置mybatis配置文件-->
<property name="configLocation" value="classpath:mybatis/mybatisConfig.xml" />
<property name="mapperLocations" value="classpath:mybatis/*Mapper.xml" />
<!--配置数据源-->
<property name="dataSource" ref="dataSource" />
</bean>
<!--配置自动扫描mapper,将mapper接口加入到IOC容器-->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory" />
<property name="basePackage" value="lxj.dao" />
</bean>
<!-- ================================事务控制相关配置================================================= -->
<!-- 事务管理器 -->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource" />
</bean>
<aop:config>
<!-- 第一个"*"代表所有类, 第二个"*"代表所有方法, ".."代表任意参数 -->
<aop:pointcut id="txMethod" expression="execution(* lxj.services.impl.*.*(..))" />
<!-- 把事务控制在service层 -->
<aop:advisor pointcut-ref="txMethod" advice-ref="txAdvice" />
</aop:config>
<!-- 配置哪些前缀的方法使用什么类型的事务 -->
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="save*" propagation="REQUIRED" /> <!-- 支持当前事务,如果执行到save开头的任何方法时没有事务则开启一个事务 这是最常见的方式-->
<tx:method name="update*" propagation="REQUIRED" />
<tx:method name="add*" propagation="REQUIRED" />
<tx:method name="delete*" propagation="REQUIRED" />
<tx:method name="find*" propagation="SUPPORTS" read-only="true"/> <!-- 支持当前事务,如果当前没有事务,就以非事务方式执行。只读 -->
<tx:method name="get*" propagation="SUPPORTS" read-only="true"/>
<tx:method name="load*" propagation="SUPPORTS" read-only="true"/>
<tx:method name="*" />
</tx:attributes>
</tx:advice>
<!-- 加入对services的扫描 -->
<context:component-scan base-package="lxj.services.impl" />
</beans>
2、springMVC-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:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<!-- 加入对controller的扫描,不加入对services的扫描,不然事务管理会不起作用 -->
<context:component-scan base-package="lxj.controllers" />
<!-- 启动注解驱动 SpringMVC 功能 -->
<mvc:annotation-driven />
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<!--前缀 <property name="prefix" value="/views/" /> -->
<!--后缀 <property name="suffix" value=".jsp" /> -->
</bean>
<!-- 扫描静态文件 -->
<mvc:resources mapping="/images/**" location="/images/"/>
<mvc:resources mapping="/css/**" location="/css/"/>
<mvc:resources mapping="/js/**" location="/js/"/>
<mvc:resources mapping="/fonts/**" location="/fonts/"/>
<mvc:resources mapping="/upload/**" location="/upload/"/>
<mvc:resources mapping="/html/**" location="/html/"/>
<mvc:resources mapping="/bootstrap/**" location="/bootstrap/"/>
<!-- 配置文件上传,如果没有使用文件上传可以不用配置,当然如果不配,那么配置文件中也不必引入上传组件包 -->
<bean id="multipartResolver"
class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<!-- 默认编码 -->
<property name="defaultEncoding" value="utf-8" />
<!-- 文件大小最大值 -->
<property name="maxUploadSize" value="10485760000" />
<!-- 内存中的最大值 -->
<property name="maxInMemorySize" value="40960" />
</bean>
</beans>
3、services实现类
package lxj.services.impl;
import java.util.Map;
import lxj.dao.UserDAO;
import lxj.services.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service("userService")
public class UserServiceImpl implements UserService {
@Autowired
private UserDAO userDAO;
@Override
//save开头的方法会自动匹配xml中相关配置
public int saveUser(Map<String, Object> map) {
int n=0;
n=userDAO.saveUser(map);
n=userDAO.saveInfor(map);
return n;
}
}
另外附上本人整理的案例:http://download.csdn.net/download/xiatiancsdn/10116770
注意:在services层千万不要使用try catch来捕捉异常,不然spring检测不到异常事务控制就不会生效,要想捕捉异常,可以写在controller管理层