Spring MVC + mybatis + mysql 事务回滚服务层,用@Transactional方式注解事务。
1、mysql建表设置引擎为INNODB,如:
ALTER TABLE goods_order ENGINE = INNODB;
2、服务层
public interface IGoodsOrderService { /** * 添加订单 * @param order * @return */ boolean addOrder(GoodsOrder order) throws Exception; }
@Service public class GoodsOrderServiceImpl implements IGoodsOrderService { @Autowired private IGoodsOrderDao orderDao; @Autowired private IGoodsOrderDetailDao orderDetailDao; @Autowired private IGoodsOrderStatusDao orderStatusDao; @Override @Transactional(readOnly=false,propagation=Propagation.REQUIRED,rollbackFor=Exception.class) public boolean addOrder(GoodsOrder order) throws Exception { boolean result = false; try { if (null != order.getDetails() && order.getDetails().size() > 0) { order.setOrderNum(order.getDetails().size()); // 添加订单 orderDao.addGoodsOrder(order); // 添加订单详细 for (GoodsOrderDetail orderDetail : order.getDetails()) { // orderDetail.setOrderNo(order.getOrderNo()); orderDetailDao.addGoodsOrderDetail(orderDetail); } result = true; } } catch (Exception e) { //事务回滚异常需要抛出来 throw e; } return result; } }
3、配置applicationContext.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:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context" 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.1.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd" default-lazy-init="true"> <!-- 加载jdbc.properties文件 --> <context:property-placeholder location="classpath:jdbc.properties" /> <!-- Root Context: defines shared resources visible to all other web components --> <context:annotation-config /> <context:component-scan base-package="com.test" /> <!-- 数据源 --> <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"> <property name="driverClassName" value="${db.driver}" /> <property name="url" value="${db.url}" /> <property name="username" value="${db.username}" /> <property name="password" value="${db.password}" /> <property name="minIdle" value="1" /> <property name="maxActive" value="25" /> <property name="maxIdle" value="5" /> </bean> <!-- mybatis 配置 --> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="mapperLocations" value="classpath:myibatis/*-mapper.xml" /> <property name="dataSource" ref="dataSource" /> <property name="typeAliasesPackage" value="com.test.domain" /> </bean> <!-- 扫描Dao层 --> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <property name="basePackage" value="com.test.dao" /> <property name="annotationClass" value="org.springframework.stereotype.Repository" /> </bean> <!-- 事务管理器 --> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSource" /> </bean> <!-- 事物配置 --> <tx:annotation-driven transaction-manager="transactionManager" /> </beans>
4、spring-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:mongo="http://www.springframework.org/schema/data/mongo" 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.0.xsd http://www.springframework.org/schema/data/mongo http://www.springframework.org/schema/data/mongo/spring-mongo.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd"> <!-- 扫描系统Module--> <context:component-scan base-package="com.test.dao" /> <context:component-scan base-package="com.test.service"> <!-- component-scan和事务所在的上下文不一样,先不让spring扫描到@Service注解的类。否则@Transactional注解会失效 --> <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Service" /> </context:component-scan> <context:component-scan base-package="com.test.controller" /> <!-- spring mvc view --> <bean id="viewResolver" class="org.springframework.web.servlet.view.UrlBasedViewResolver"> <property name="viewClass" value="org.springframework.web.servlet.view.JstlView" /> <!-- <property name="prefix" value="/WEB-INF/views/" /> --> <property name="prefix" value="/views/" /> <property name="suffix" value=".jsp" /> </bean> </beans>