关于事务就不介绍了,前面在研究spring的时候就已经研究过了,参考:https://www.cnblogs.com/qlqwjy/p/7296493.html

  这里直接研究springboot中事务的开启以及测试方法。

  在Spring Boot中推荐使用@Transactional注解来申明事务。

首先需要导入依赖:

  1. <dependency>
  2. <groupId>org.springframework.boot</groupId>
  3. <artifactId>spring-boot-starter-jdbc</artifactId>
  4. </dependency>

  当引入jdbc依赖之后,Spring Boot会自动默认分别注入DataSourceTransactionManager或JpaTransactionManager,所以我们不需要任何额外配置就可以用@Transactional注解进行事务的使用。

  

  在Service中添加@Transactional注解:

===================测试事务效果===========

Service层代码:

  1. package cn.qlq.service.impl.user;
  2.  
  3. import java.util.List;
  4.  
  5. import org.springframework.beans.factory.annotation.Autowired;
  6. import org.springframework.stereotype.Service;
  7.  
  8. import cn.qlq.bean.user.User;import cn.qlq.mapper.user.UserMapper;
  9. import cn.qlq.service.user.UserService;
  10.  
  11. @Service
  12. public class UserServiceImpl implements UserService {
  13.  
  14. @Autowired
  15. private UserMapper userMapper;
  16.  
  17. @Override
  18. public void addUser(User user) {
  19. userMapper.insert(user);
  20. int i = 1 / 0;
  21. }
  22.  
  23. }

(1)测试未添加事务

  数据库原来记录:

测试添加后查看日志:(插入数据之后报异常)

  1. 2019-02-21 16:25:20.983 INFO 20960 --- [nio-8088-exec-1] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring FrameworkServlet 'dispatcherServlet'
  2. 2019-02-21 16:25:20.984 INFO 20960 --- [nio-8088-exec-1] o.s.web.servlet.DispatcherServlet : FrameworkServlet 'dispatcherServlet': initialization started
  3. 2019-02-21 16:25:21.004 INFO 20960 --- [nio-8088-exec-1] o.s.web.servlet.DispatcherServlet : FrameworkServlet 'dispatcherServlet': initialization completed in 20 ms
  4. 2019-02-21 16:25:21.047 INFO 20960 --- [nio-8088-exec-1] cn.qlq.action.UserController : user -> User [id=null, username=2, password=2, userfullname=2, createtime=Thu Feb 21 16:25:21 CST 2019, isdeleted=null, sex=女, address=2]
  5. 2019-02-21 16:25:21.320 DEBUG 20960 --- [nio-8088-exec-1] cn.qlq.mapper.user.UserMapper.insert : ==> Preparing: insert into user (id, username, password, userfullname, createtime, isdeleted, sex, address) values (?, ?, ?, ?, ?, ?, ?, ?)
  6. 2019-02-21 16:25:21.335 DEBUG 20960 --- [nio-8088-exec-1] cn.qlq.mapper.user.UserMapper.insert : ==> Parameters: null, 2(String), 2(String), 2(String), 2019-02-21(Date), null, 女(String), 2(String)
  7. 2019-02-21 16:25:21.674 DEBUG 20960 --- [nio-8088-exec-1] cn.qlq.mapper.user.UserMapper.insert : <== Updates: 1
  8. java.lang.ArithmeticException: / by zero
  9. at cn.qlq.service.impl.user.UserServiceImpl.addUser(UserServiceImpl.java:27)
  10. at cn.qlq.action.UserController.addUser(UserController.java:31)
  11. at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
  12. at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
  13. at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
  14. at java.lang.reflect.Method.invoke(Method.java:606)
  15. at org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:205)
  16. at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:133)
  17. at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:116)
  18. at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:827)
  19. at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:738)
  20. at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:85)
  21. at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:963)
  22. at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:897)
  23. at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:970)
  24. at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:872)
  25. at javax.servlet.http.HttpServlet.service(HttpServlet.java:648)
  26. at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:846)
  27. at javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
  28. at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:230)
  29. at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:165)
  30. at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
  31. at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:192)
  32. at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:165)
  33. at org.springframework.web.filter.RequestContextFilter.doFilterInternal(RequestContextFilter.java:99)
  34. at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
  35. at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:192)
  36. at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:165)
  37. at org.springframework.web.filter.HttpPutFormContentFilter.doFilterInternal(HttpPutFormContentFilter.java:105)
  38. at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
  39. at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:192)
  40. at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:165)
  41. at org.springframework.web.filter.HiddenHttpMethodFilter.doFilterInternal(HiddenHttpMethodFilter.java:81)
  42. at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
  43. at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:192)
  44. at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:165)
  45. at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:197)
  46. at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
  47. at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:192)
  48. at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:165)
  49. at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:198)
  50. at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:96)
  51. at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:474)
  52. at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:140)
  53. at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:79)
  54. at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:87)
  55. at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:349)
  56. at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:783)
  57. at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:66)
  58. at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:798)
  59. at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1434)
  60. at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49)
  61. at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
  62. at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
  63. at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
  64. at java.lang.Thread.run(Thread.java:745)
  65. 2019-02-21 16:25:21.763 WARN 20960 --- [nio-8088-exec-1] .m.m.a.ExceptionHandlerExceptionResolver : Resolved exception caused by Handler execution: java.lang.ArithmeticException: / by zero

查看数据库:(数据增加,没有事务回滚)

(2)测试添加事务

只需在类加上@Transactional注解就可以了。

测试查看日志同上面一样,但是事务进行回滚了,只是没有打出日志。(下次提交发现id会跳过本次提交的值)

查看数据库记录:

SpringBoot设置事务管理的更多相关文章

  1. springboot mybatis 事务管理

    本文主要讲述springboot提供的声明式的事务管理机制. 一.一些概念 声明式的事务管理是基于AOP的,在springboot中可以通过@Transactional注解的方式获得支持,这种方式的优 ...

  2. Springboot下事务管理的简单使用

    关于事务管理的概念这里就不多介绍了,在我的博客“JDBC事务之理论篇”中也有介绍. 关于Spring的事务管理,主要是通过事务管理器来进行的.这里看个Spring事务管理的接口图:(来自博客https ...

  3. SpringBoot设置事务隔离等级

    "If you're gonna play the game, boy, ya gotta learn to play it right" Spring Boot 使用事务非常简单 ...

  4. springboot开启事务管理

    spring中开启事务管理需要在xml配置文件中配置,springboot中采取java config的配置方式. 核心是@EnableTransactionManager注解,该注解即为开启事务管理 ...

  5. spring boot学习(6) SpringBoot 之事务管理

    两个操作要么同时成功,要么同时失败: 事务的一致性: 以前学ssh ssm都有事务管理service层通过applicationContext.xml配置,所有service方法都加上事务操作: 用来 ...

  6. spring05-Spring事务管理

    事务的第一个方面是传播行为(propagation behavior).当事务方法被另一个事务方法调用时,必须指定事务应该如何传播.例如:方法可能继续在现有事务中运行,也可能开启一个新事务,并在自己的 ...

  7. 【Spring Boot学习之四】Spring Boot事务管理

    环境 eclipse 4.7 jdk 1.8 Spring Boot 1.5.2 一.springboot整合事务事务分类:编程事务.声明事务(XML.注解),推荐使用注解方式,springboot默 ...

  8. spring 配置事务管理器

    在Spring中数据库事务是通过PlatformTransactionManager进行管理的,jdbcTemplate是不能支持事务的,而能够支持事务的是org.springframework.tr ...

  9. springboot(二)整合mybatis,多数据源和事务管理

     -- 1.整合mybatis -- 2.整合多数据源 -- 3. 整合事务 代码地址:https://github.com/showkawa/springBoot_2017/tree/master/ ...

随机推荐

  1. HAN模型理解1

    HAN 模型 最开始看这个模型是看的这个解释: RNN+Attention(HAN) 文本分类 阅读笔记 - 今天做作业没的文章 - 知乎 https://zhuanlan.zhihu.com/p/4 ...

  2. linxu信号种类

    使用kill -l 命令,可看到linux支持的信号列表: 1) SIGHUP 2) SIGINT 3) SIGQUIT 4) SIGILL 5) SIGTRAP 6) SIGABRT 7) SIGB ...

  3. 13、python中的函数(闭包与装饰器)

    一.嵌套函数 函数的内部又再定义另一个函数,这个函数就叫嵌套函数,里面含函数就叫内部函数. 示例: 二.返回函数 函数可以接收函数对象作为参数,同理函数也能返回一个函数对象作为返回值. 示例: 返回函 ...

  4. css媒体类型

    all 用于所有的媒体设备. aural 用于语音和音频合成器. braille 用于盲人用点字法触觉回馈设备. embossed 用于分页的盲人用点字法打印机. handheld 用于小的手持的设备 ...

  5. python3知识点之---------列表的介绍

    1.列表是什么? 它是由一系列特定顺序排序的元素组成.元素可以表示一切任何的事物,元素之间可以没有任何关系.列表用方括号[ ] 表示,元素之间由逗号隔开.   例如表示一系列数字的列表:  numbe ...

  6. Wordpress 为用户或角色 role 添加 capabilities(权限)

    首先查看角色具有哪些权限: $admin_role_set = get_role( 'administrator' )->capabilities; $author_role_set = get ...

  7. wireshark简单使用

    过滤表达式的规则   表达式规则   1. 协议过滤   比如TCP,只显示TCP协议. ip.src == 219.216.87.200 and ip.dst==219.216.87.254   2 ...

  8. JavaScript里面的条件、循环语句以及异常处理

    1.JavaScript里面条件语句主要有两种形式 if(条件){ ... }else if(条件){ ... }else{ ...} switch(变量名): case 值1://如果变量名为值1, ...

  9. HDU 3775 Chain Code pick定理

    pick定理:一个计算点阵中顶点在格点上的多边形面积公式:S=a+b÷2-1,其中a表示多边形内部的点数,b表示多边形边界上的点数,s表示多边形的面积. 思路:http://blog.csdn.net ...

  10. 贪吃蛇StringBuilder 和 定时器

    ConsoleKeyInfo info = Console.ReadKey(); while (true) { if (info.Key == ConsoleKey.UpArrow)//只能输入一次但 ...