一、简介

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. windows开机执行bat

    一.以windows下备份sql数据库为例,开机自动执行.bat脚本        1.新建dump.bat文件,文件中的代码如下:                set YYYYmmdd=%date ...

  2. error storage size of my_addr isn't known

  3. GDPR全文翻译(一)

    General Data Protection Regulation <一般数据保护法案>全文翻译(一) 编者按 2016年4月14日,欧洲议会投票通过了商讨四年的<一般数据保护法案 ...

  4. EINVRES Request to https://bower.herokuapp.com/packages/ failed with 502

    Bower install fails with 502 - Bad Gateway when downloading bower packages. For example bower instal ...

  5. Bitmap: 使用Bitmap作为绘图缓冲时设置抗锯齿

    android上绘图时常用的抗锯齿方法是: paint.setAntiAlias(true); 但是在以Bitmap作为绘图缓冲绘制时,绘制出来的Bitmap可能仍然有锯齿,此时可以在绘制开始前加上下 ...

  6. VMware与宿主机同一网段

    将VMware做为一个物理的虚拟机,设置网段与宿主机在同一子网.

  7. [Java复习] 缓存Cache part1

    1. 在项目中是如何使用缓存的?为什么要用?不用行不行?用了可能会有哪些不良后果? 结合项目业务,主要两个目的:高性能和高并发.缓存走内存,天然支持高并发. 不良后果: 缓存与DB双写不一致 缓存雪崩 ...

  8. Visual studio 正在从以下位置加载符号:Microsoft符号服务器 尝试取消禁用后续符号加载

    正在从以下位置加载符号:Microsoft符号服务器   尝试取消禁用后续符号加载 进入VS---工具---选项----调试----符号,看右边有个“Microsoft符号服务器”,将前面的勾去掉,( ...

  9. 123456123456#0#-----com.threeapp.xiongMaoPaoPao01----熊猫跑酷01

    com.threeapp.xiongMaoPaoPao01----熊猫跑酷01

  10. 使用Apache,压力测试redisson的一般高并发

    安装 Linux linux直接yum -y install httpd-tools,然后ab -V测试 Windows 1查看80端口有没有被占用,netstat -ano | findstr &q ...