step1:配置xml文件

 <!-- 事务管理bean -->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource" />
</bean> <bean id="transactionTemplate"
class="org.springframework.transaction.support.TransactionTemplate">
<property name="transactionManager">
<ref bean="transactionManager"/>
</property>
</bean> <!-- 对@Transactional这个注解进行的驱动,这是基于注解的方式使用事务配置声明 -->
<tx:annotation-driven transaction-manager="transactionManager" />

step2:自动注入TransactionTemplate对象

 import org.springframework.transaction.support.TransactionTemplate;
@Autowired
private TransactionTemplate transactionTemplate;

step3:使用execute方法对事务进行管理

 transactionTemplate.execute(new TransactionCallbackWithoutResult() {

                 @Override
protected void doInTransactionWithoutResult(TransactionStatus arg0) {
String s = userService.newUser(user,currentUserId);
User newUser=userService.getUser(user.getUsername());
roleService.createRoleUser(newUser.getUserId(), rolesId);
}
});

完整流程如下:

 package com.superb.mips.terminal.rest.controller;

 import java.util.HashMap;

 import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.TransactionStatus;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.support.TransactionCallbackWithoutResult;
import org.springframework.transaction.support.TransactionTemplate;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody; import com.superb.mips.system.domain.User;
import com.superb.mips.system.service.inf.RoleService;
import com.superb.mips.system.service.inf.UserService; @Controller
@RequestMapping("api/users")
public class RestUserController {
@Autowired
private UserService userService;
&orgName=中国移动&orgId=1
@Autowired
private RoleService roleService;
@Autowired
private TransactionTemplate transactionTemplate; @RequestMapping(value = "/createUsers.json")
@ResponseBody
@Transactional
public HashMap<String, Object> createUser(final User user,final int currentUserId,final String rolesId) {
HashMap<String, Object> map = new HashMap<String, Object>();
String[] strAry=rolesId.split(",");
if(strAry.length==0){
map.put("result", 1);
map.put("description", "请绑定角色");
return map;
}else{
for(int i=0;i<strAry.length;i++){
try {
Integer.parseInt(strAry[i]);
} catch (Exception e) {
map.put("result", 2);
map.put("description", "请绑定正确的角色id");
return map;
}
}
}
try {
transactionTemplate.execute(new TransactionCallbackWithoutResult() { @Override
protected void doInTransactionWithoutResult(TransactionStatus arg0) {
String s = userService.newUser(user,currentUserId);
User newUser=userService.getUser(user.getUsername());
roleService.createRoleUser(newUser.getUserId(), rolesId);
}
});
map.put("result", 0);
map.put("description", "新增用户成功。");
} catch (Exception e) {
map.put("result", 3);
map.put("description", "新增用户失败!");
} return map;
}
}

spring中编程式事务控制的更多相关文章

  1. 全面分析 Spring 的编程式事务管理及声明式事务管理

    开始之前 关于本教程 本教程将深入讲解 Spring 简单而强大的事务管理功能,包括编程式事务和声明式事务.通过对本教程的学习,您将能够理解 Spring 事务管理的本质,并灵活运用之. 先决条件 本 ...

  2. 全面分析 Spring 的编程式事务管理及声明式事务管理--转

    开始之前 关于本教程 本教程将深入讲解 Spring 简单而强大的事务管理功能,包括编程式事务和声明式事务.通过对本教程的学习,您将能够理解 Spring 事务管理的本质,并灵活运用之. 先决条件 本 ...

  3. Spring的编程式事务和声明式事务

    事务管理对于企业应用来说是至关重要的,当出现异常情况时,它也可以保证数据的一致性. Spring事务管理的两种方式 spring支持编程式事务管理和声明式事务管理两种方式. 编程式事务使用Transa ...

  4. Spring笔记(4) - Spring的编程式事务和声明式事务详解

    一.背景 事务管理对于企业应用而言至关重要.它保证了用户的每一次操作都是可靠的,即便出现了异常的访问情况,也不至于破坏后台数据的完整性.就像银行的自助取款机,通常都能正常为客户服务,但是也难免遇到操作 ...

  5. 【Spring】编程式事务和声明式事务

    一.概述 二.准备工作 1. 创建表 2. 创建项目并引入Maven依赖 3. 编写实体类 4. 编写Dao层 5. 业务层 6. XML中的配置 7. 测试 三.编程式事务 1. 在业务层代码上使用 ...

  6. 阶段3 2.Spring_10.Spring中事务控制_9 spring编程式事务控制1-了解

    编程式的事物控制,使用的情况非常少,主要作为了解 新建项目 首先导入包坐标 复制代码 这里默认值配置了Service.dao和连接池其他的内容都没有配置 也就说现在是没有事物支持的.运行测试文件 有错 ...

  7. 阶段3 2.Spring_10.Spring中事务控制_10spring编程式事务控制2-了解

    在业务层声明 transactionTemplate 并且声称一个set方法等着spring来注入 在需要事物控制的地方执行 execute.但是这个execute需要一个参数 需要的参数是Trans ...

  8. (转)Spring的编程式事务例子

    纯JDBC操作, 对某些项目来说, 也许更好, Spring JDBC Framework让你不用关心Connection, Statement, ResultSet. 定义数据源 spring事务编 ...

  9. spring 采用编程式事务

    1.getCurrentSession()与openSession()的区别? * 采用getCurrentSession()创建的session会绑定到当前线程中,而采用openSession() ...

随机推荐

  1. jquery 点击切换div

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  2. zabbix4.2学习笔记--用自带的mysql监控模块

    这里演示监控zabbix本身用到的mysql 第一步:建立mysql监控用户 在生产环境中,出于安全考虑,建议监控客户端数据库时,单独配置一个查询权限用户做查询操作即可 # 撤掉安装时给予的分配单个数 ...

  3. Spring boot 控制台打印sql

    在application.ym中加入: logging: level: com.wechat.cwbt.dao : debug 发现无效 在log4j.properties中加入: log4j.log ...

  4. 浏览器通知js授权

    // 获得权限 Notification.requestPermission(); // 点击按钮 document.querySelector('#button').addEventListener ...

  5. [Python3网络爬虫开发实战] 3.1.1-发送请求

    使用urllib的request模块,我们可以方便地实现请求的发送并得到响应,本节就来看下它的具体用法. 1. urlopen() urllib.request模块提供了最基本的构造HTTP请求的方法 ...

  6. 常见的Redis问题?

    Redis的那些最常见面试问题[转] 1.什么是redis? Redis 是一个基于内存的高性能key-value数据库. 2.Reids的特点 Redis本质上是一个Key-Value类型的内存数据 ...

  7. Python之面向对象元类

    Python之面向对象元类 call方法: class People: def __init__(self,name): self.name=name # def __call__(self, *ar ...

  8. Ubuntu系统搭建django+nginx+uwsgi

    1. 在开发机上的准备工作 2. 在服务器上的准备工作 3.安装uwsgi 4.编写uwsgi配置文件,使用配置文件启动uwsgi 5. 安装nginx 6. 收集静态文件 7. 编写nginx配置文 ...

  9. 集训第四周(高效算法设计)H题 (贪心)

    Description   Most financial institutions had become insolvent during financial crisis and went bank ...

  10. Vue如何使用vue-awesome-swiper实现轮播效果

    在Vue项目中如何实现轮播图的效果呢,在传统项目中第一个想到的一般都是swiper插件,代码简单好用.一开始我也是直接npm安装swiper然后照着之前的传统写法写,然而却没有效果,只会显示图片但没有 ...