(十四)SpringBoot之事务处理
一、简介
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之事务处理的更多相关文章
- spring boot 学习(十四)SpringBoot+Redis+SpringSession缓存之实战
SpringBoot + Redis +SpringSession 缓存之实战 前言 前几天,从师兄那儿了解到EhCache是进程内的缓存框架,虽然它已经提供了集群环境下的缓存同步策略,这种同步仍然需 ...
- 十四.spring-boot使用mybatis
在springMVC+spring中使用mybatis已经非常非常的灵活,但是需要配置很多的信息 一.创建maven web project 二.创建数据库表 三.在application.prope ...
- [十四]SpringBoot 之 Spring拦截器(HandlerInterceptor)
过滤器属于Servlet范畴的API,与spring 没什么关系. Web开发中,我们除了使用 Filter 来过滤请web求外,还可以使用Spring提供的HandlerInterceptor(拦截 ...
- SpringBoot进阶教程(六十四)注解大全
在Spring1.x时代,还没出现注解,需要大量xml配置文件并在内部编写大量bean标签.Java5推出新特性annotation,为spring的更新奠定了基础.从Spring 2.X开始spri ...
- springboot(十四):springboot整合shiro-登录认证和权限管理(转)
springboot(十四):springboot整合shiro-登录认证和权限管理 .embody{ padding:10px 10px 10px; margin:0 -20px; border-b ...
- SpringBoot第二十四篇:应用监控之Admin
作者:追梦1819 原文:https://www.cnblogs.com/yanfei1819/p/11457867.html 版权声明:本文为博主原创文章,转载请附上博文链接! 引言 前一章(S ...
- springboot源码解析-管中窥豹系列之bean如何生成?(十四)
一.前言 Springboot源码解析是一件大工程,逐行逐句的去研究代码,会很枯燥,也不容易坚持下去. 我们不追求大而全,而是试着每次去研究一个小知识点,最终聚沙成塔,这就是我们的springboot ...
- spring boot 常见三十四问
Spring Boot 是微服务中最好的 Java 框架. 我们建议你能够成为一名 Spring Boot 的专家. 问题一 Spring Boot.Spring MVC 和 Spring 有什么区别 ...
- 开发指南专题十四:JEECG微云高速开发平台MiniDao 介绍
版权声明:本文为博主原创文章,未经博主同意不得转载. https://blog.csdn.net/zhangdaiscott/article/details/27068645 开发指南专题十四:J ...
- 跟我学SpringCloud | 第十四篇:Spring Cloud Gateway高级应用
SpringCloud系列教程 | 第十四篇:Spring Cloud Gateway高级应用 Springboot: 2.1.6.RELEASE SpringCloud: Greenwich.SR1 ...
随机推荐
- JavaWeb基础知识
一.WEB基本概念 1.1.WEB开发的相关知识 WEB,在英语中web即表示网页的意思,它用于表示Internet主机上供外界访问的资源. Internet上供外界访问的Web资源分为: 静态web ...
- php发现一个神奇的函数
echo strtr('aaddffvvbbcc','avc','242'); //22ddff44bb22 echo '<br>'; echo str_replace('ad',22,' ...
- Path.Combine Method
https://docs.microsoft.com/en-us/dotnet/api/system.io.path.combine?view=netframework-4.8#System_IO_P ...
- TP框架修改后台路径方法
直接映射 admin 后台修改路径为 myadmin888 文章来源:外星人来地球 欢迎关注,有问题一起学习欢迎留言.评论
- idea使用maven私服
nexus3中央仓库改为阿里云/ 参考:这里写链接内容 找到中央仓库 然后修改成: http://maven.aliyun.com/nexus/content/groups/public/ 1 ...
- java匿名内部类new(){}
匿名内部类:顾名思义,没有名字的内部类.表面上看起来它们似乎有名字,实际那不是它们的名字.当程序中使用匿名内部类时,在定义匿名内部类的地方往往直接创建该类的一个对象.匿名内部类的声明格式如下:new ...
- js实现文本框支持加减运算的方法
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN""http://www.w3.org/T ...
- Java 8 Steam 例子整理
Java 8 Steam 例子整理 kexue 关注 2016.06.06 17:44* 字数 1860 阅读 3901评论 0喜欢 6 IBM: Java 8 中的 Streams API 详解 为 ...
- EOF使用
1.cat向文件覆盖内容 cat > local.repo << EOF [local]name=localbaseurl=file:///mnt/cdromgpgcheck=0en ...
- 【Leetcode_easy】766. Toeplitz Matrix
problem 766. Toeplitz Matrix solution1: class Solution { public: bool isToeplitzMatrix(vector<vec ...