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>
相关推荐
Spring MVC 基于注解实例Spring MVC 基于注解实例Spring MVC 基于注解实例Spring MVC 基于注解实例Spring MVC 基于注解实例Spring MVC 基于注解实例Spring MVC 基于注解实例Spring MVC 基于注解实例Spring MVC 基于...
自己做的一个简单的spring运用实例,主要是关于事务回滚的一个小例子,希望对还在纠结事务回滚的童靴有点帮助。。。
IT学习者Spring MVC注解实例.pdf
spring3.0MVC注解(附实例). spring3.0MVC注解(附实例). spring3.0MVC注解(附实例).
主要介绍Spring MVC 的使用注解来配置的使用实例
花了一晚上时间终于搞出来了Spring整合Mybatis事务回滚(Mysql数据库),控制Service层中的多次插入操作,多次操作整体是一个事务。 里面有缘嘛和jar包,资源为war包,导入即可。运行Test类中的测试代码即可。 建表...
基于jpa+hibernate+spring+spring mvc注解方式项目案例带整体架构及一个页面操作小流程适合新手
http://blog.csdn.net/penngo/archive/2010/11/27/6038991.aspx这篇文章“spring mvc 注解实现”的附件代码
让我们更好的了解 spring mvc 注解方面的知识 适合入门
spring mvc 注解 拦截器 对一个数据库表进行了增删改查操作 ,初学spring mvc框架的 可以参考 代码下载直接可以运行!~~
spring mvc 注解 easyui 分页
基于spring mvc注解方式,整合mybatis的项目框架哈。
spring mvc注解方式整合jdbctemplate
spring mvc 注解 拦截器 对一个数据库表进行了增删改查操作 ,初学spring mvc框架的 可以参考 代码下载直接可以运行!~~ 包含jar和数据库sql语句
最全的Spring MVC注解例子,异步请求,错误处理,是基于maven工程,很帮的一个Spring mvc注解例子。
基于spring mvc注解的增删改实例,orm采用hibernate。 资源来自csdn,添加了修改功能和readme说明,让初学者更简单的运行起来。
Spring MVC属于SpringFrameWork的后续产品,已经融合在Spring Web Flow里面。Spring 框架提供了构建 Web 应用程序的全功能 MVC 模块。Spring MVC4是当前zuixin的版本,在众多特性上有了进一步的提升。, 在精通Spring...
Spring mvc 注解Demo,适合初学者。下载即可用
Spring3_MVC注解教程.ppt
对spring mvc注解的实例