Spring MVC + mybatis + mysql transaction rollback service layer, annotate the transaction with @Transactional.
1. The mysql table creation engine is set to INNODB, such as:
ALTER TABLE goods_order ENGINE = INNODB;
2. Service layer
public interface IGoodsOrderService { /** * Add order * @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()); // add order orderDao.addGoodsOrder(order); // add order details for (GoodsOrderDetail orderDetail : order.getDetails()) { // orderDetail.setOrderNo(order.getOrderNo()); orderDetailDao.addGoodsOrderDetail(orderDetail); } result = true; } } catch (Exception e) { //Transaction rollback exception needs to be thrown throw e; } return result; } }
3. Configure 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"> <!-- Load jdbc.properties file --> <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" /> <!-- datasource--> <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 configuration --> <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> <!-- Scan Dao layer--> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <property name="basePackage" value="com.test.dao" /> <property name="annotationClass" value="org.springframework.stereotype.Repository" /> </bean> <!-- Transaction Manager --> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSource" /> </bean> <!-- Transaction Configuration --> <tx:annotation-driven transaction-manager="transactionManager" /> </beans>
4. spring-servlet.xml configuration
<?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"> <!-- Scan System Module--> <context:component-scan base-package="com.test.dao" /> <context:component-scan base-package="com.test.service"> <!-- The context of component-scan is different from that of the transaction, so let spring scan the classes annotated with @Service first. Otherwise the @Transactional annotation will be invalid --> <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>