一、简介

ssh ssm都有事务管理service层通过applicationContext.xml配置,所有service方法都加上事务操作;

用来保证一致性,即service方法里的多个dao操作,要么同时成功,要么同时失败;

springboot下的话,在service方法上加上@Transactional即可

二、案例

  2.1  controller

package com.shyroke.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody; import com.shyroke.dao.UserMapper;
import com.shyroke.entity.UserBean;
import com.shyroke.service.UserService; @Controller
@RequestMapping(value = "/")
public class IndexController { @Autowired
private UserService userService; @ResponseBody
@RequestMapping(value="/save")
public String list() { UserBean user1=new UserBean();
user1.setUserName("user1");
user1.setPassWord("123"); userService.save(user1); return "index"; }
}
  • service

package com.shyroke.service;

import com.shyroke.entity.UserBean;

public interface UserService {

    void save(UserBean user1);

}
  • service实现类

  在下面的代码中,我们对save方法加上了@Transactional注解,表示使用事务,当有异常抛出时,就会自动回滚。

package com.shyroke.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional; import com.shyroke.dao.UserMapper;
import com.shyroke.entity.UserBean;
import com.shyroke.service.UserService; @Service
public class UserServiceImpl implements UserService{ @Autowired
private UserMapper userMapper; @Override
@Transactional
public void save(UserBean user1) { userMapper.save(user1); boolean flag = true;
if (flag) {
throw new RuntimeException();
} } }
  • mapper

package com.shyroke.dao;

import org.springframework.data.jpa.repository.JpaRepository;

import com.shyroke.entity.UserBean;

public interface UserMapper extends JpaRepository<UserBean, Integer>{

}
  •   结果:

数据库没有数据,说明已经被回滚了。

(十四)SpringBoot之事务处理的更多相关文章

  1. spring boot 学习(十四)SpringBoot+Redis+SpringSession缓存之实战

    SpringBoot + Redis +SpringSession 缓存之实战 前言 前几天,从师兄那儿了解到EhCache是进程内的缓存框架,虽然它已经提供了集群环境下的缓存同步策略,这种同步仍然需 ...

  2. 十四.spring-boot使用mybatis

    在springMVC+spring中使用mybatis已经非常非常的灵活,但是需要配置很多的信息 一.创建maven web project 二.创建数据库表 三.在application.prope ...

  3. [十四]SpringBoot 之 Spring拦截器(HandlerInterceptor)

    过滤器属于Servlet范畴的API,与spring 没什么关系. Web开发中,我们除了使用 Filter 来过滤请web求外,还可以使用Spring提供的HandlerInterceptor(拦截 ...

  4. SpringBoot进阶教程(六十四)注解大全

    在Spring1.x时代,还没出现注解,需要大量xml配置文件并在内部编写大量bean标签.Java5推出新特性annotation,为spring的更新奠定了基础.从Spring 2.X开始spri ...

  5. springboot(十四):springboot整合shiro-登录认证和权限管理(转)

    springboot(十四):springboot整合shiro-登录认证和权限管理 .embody{ padding:10px 10px 10px; margin:0 -20px; border-b ...

  6. SpringBoot第二十四篇:应用监控之Admin

    作者:追梦1819 原文:https://www.cnblogs.com/yanfei1819/p/11457867.html 版权声明:本文为博主原创文章,转载请附上博文链接! 引言   前一章(S ...

  7. springboot源码解析-管中窥豹系列之bean如何生成?(十四)

    一.前言 Springboot源码解析是一件大工程,逐行逐句的去研究代码,会很枯燥,也不容易坚持下去. 我们不追求大而全,而是试着每次去研究一个小知识点,最终聚沙成塔,这就是我们的springboot ...

  8. spring boot 常见三十四问

    Spring Boot 是微服务中最好的 Java 框架. 我们建议你能够成为一名 Spring Boot 的专家. 问题一 Spring Boot.Spring MVC 和 Spring 有什么区别 ...

  9. 开发指南专题十四:JEECG微云高速开发平台MiniDao 介绍

    版权声明:本文为博主原创文章,未经博主同意不得转载. https://blog.csdn.net/zhangdaiscott/article/details/27068645   开发指南专题十四:J ...

  10. 跟我学SpringCloud | 第十四篇:Spring Cloud Gateway高级应用

    SpringCloud系列教程 | 第十四篇:Spring Cloud Gateway高级应用 Springboot: 2.1.6.RELEASE SpringCloud: Greenwich.SR1 ...

随机推荐

  1. python 可执行

    py2exe使用方法 py2exe作者:zzj 日期:2006-07-05字体大小: 小 中 大 一.简介 py2exe是一个将python脚本转换成windows上的可独立执行的可执行程序(*.ex ...

  2. Access the value of a member expression

    Access the value of a member expression 解答1 You can compile and invoke a lambda expression whose bod ...

  3. Random Projection

    Random Projection在k-means的应用   1. 随机投影 (Random Projection) 首先,这是一种降维方法.之前已经介绍过相对普遍的PCA的降维方法,这里介绍另一种降 ...

  4. Python问题:error: Microsoft Visual C++ 9.0 is required

    Python问题:error: Microsoft Visual C++ 9.0 is required 原因是缺少编译C的 VCForPython包. 解决办法: 安装VCForPython即可. ...

  5. 数据库Sequence创建与使用

    最近几天使用Oracle的sequence序列号,发现对如何创建.修改.使用存在很多迷茫点,在上网寻找答案后,根据各路大神的总结,汇总下对自己的学习成果: 在Oracle中sequence就是序号,每 ...

  6. yield 异步 并行 Promise await async

    yield方式转移执行权的协程之间不是调用者与被调用者的关系,而是彼此对称.平等的 http://www.geeksforgeeks.org/use-yield-keyword-instead-ret ...

  7. android: ListView设置emptyView 误区

    使用ListVIew 来设置EmptyView的时候须注意: ListView listview = (ListView) findViewById(R.id.list); View emptyVie ...

  8. redis不支持多个数据库实例但是支持多个字典

    Redis多个数据库 注意:Redis支持多个数据库,并且每个数据库的数据是隔离的不能共享,并且基于单机才有,如果是集群就没有数据库的概念. Redis是一个字典结构的存储服务器,而实际上一个Redi ...

  9. 命令行启动python的IDLE

    如果你电脑上使用了anaconda2,默认路径为python2,但是你又想使用anaconda2下的python3的idle 方法如下: 首先查看python的路径: (deeplearning3) ...

  10. CentOS7下搭建zabbix监控(三)——Zabbix监控服务配置

    CentOS7下搭建zabbix监控(一)——Zabbix监控端配置 CentOS7下搭建zabbix监控(二)——Zabbix被监控端配置 (1).配置Zabbix监控Apache服务 主机名:yo ...