spring boot事务管理
spring boot集成事务十分的简单,只需要在启动类上面增加@EnableTransactionManagement注解,然后在需要实现事务的方法上添加@Transactional注解就可以了。下面我们根据上一次的代码来演示下。
首先,我们修改下启动类
package com.example.demo; import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;
import org.springframework.transaction.annotation.EnableTransactionManagement; @SpringBootApplication
@MapperScan(basePackages = ("com.example.demo.mapper"))
@EnableTransactionManagement//开启springboot事务的支持
public class DemoApplication extends SpringBootServletInitializer { @Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
return application.sources(DemoApplication.class);
} public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
} }
在service中添加一个修改方法
package com.example.demo.service;
import com.example.demo.model.Student;
public interface GetStudentService {
public Student getStudentInfo();
public int update();
}
package com.example.demo.service.impl; import com.example.demo.mapper.StudentMapper;
import com.example.demo.model.Student;
import com.example.demo.service.GetStudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional; @Service
public class GetStudentServiceImpl implements GetStudentService { @Autowired
private StudentMapper studentMapper; @Override
public Student getStudentInfo() {
Student student = studentMapper.selectByPrimaryKey("122528");
return student;
} @Transactional//增加事务注解
@Override
public int update(){
Student student = new Student();
student.setId("122528");
student.setName("李四");
int ret = studentMapper.updateByPrimaryKey(student); int i = 100/0; //触发异常,测试更新事件会不会回滚
return ret;
}
}
在上面的类中特意引发了异常,用于我们的测试。最后在controlle中添加对修改方法的调用。
package com.example.demo.controller; import com.example.demo.model.Student;
import com.example.demo.service.GetStudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController; @RestController
public class GetStudentController { @Autowired
private GetStudentService getStudentService; @RequestMapping("/getStudent")
public String getStudent(){
Student student = getStudentService.getStudentInfo();
return "学号=" + student.getId() + ";姓名=" + student.getName();
} @RequestMapping("/updateStudent")
public String updateStudent(){
getStudentService.update();
return "success";
}
}
数据库中原始的姓名是张三,现在将他改为李四,访问地址http://127.0.0.1:8088/demo/updateStudent,最终结果报了异常,然后查看数据库中的值,发现没有发生变化,因此我们的事务注解生效了。
在上面的操作过程中,我发现了一个问题,在controller中如果使用private GetStudentServiceImpl getStudentServiceImpl;启动的时候就会报错:
Description: The bean 'getStudentServiceImpl' could not be injected as a 'com.example.demo.service.impl.GetStudentServiceImpl' because it is a JDK dynamic proxy that implements:
com.example.demo.service.GetStudentService Action: Consider injecting the bean as one of its interfaces or forcing the use of CGLib-based proxies by setting proxyTargetClass=true on @EnableAsync and/or @EnableCaching. Process finished with exit code 1
对此有两种解决方法,一种是我上面的,将代码中的引用由实现类改为接口类即可。另外一种是在开启事务的注解上增加属性。即@EnableTransactionManagement(proxyTargetClass = true),开启CGLIB代理也能解决。
因为开启事务时,会自动开启动态代理,默认的是开启的jdk动态代理。详细解释地址:https://blog.csdn.net/huang_550/article/details/76492600。这块目前还不清楚什么原理,后面再细研究。
spring boot热部署配置,增加pom.xml依赖
<!-- spring boot热部署插件-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>true</scope>
<optional>true</optional>
</dependency>
spring boot事务管理的更多相关文章
- Spring Boot事务管理(上)
摘要 本文主要介绍基于Spring Boot的事务管理,尤其是@Transactional注解详细用法.首先,简要介绍Spring Boot中如何开启事务管理:其次,介绍在Spring,Spring ...
- Spring Boot事务管理(中)
在上一篇 Spring Boot事务管理(上)的基础上介绍Spring Boot事务属性和事务回滚规则 . 4 Spring Boot事务属性 什么是事务属性呢?事务属性可以理解成事务的一些基本配置, ...
- Spring Boot事务管理(下)
在上两篇 Spring Boot事务管理(上)和Spring Boot事务管理(中)的基础上介绍注解@Transactional. 5 @Transactional属性 属性 类型 描述 value ...
- 【Spring Boot学习之四】Spring Boot事务管理
环境 eclipse 4.7 jdk 1.8 Spring Boot 1.5.2 一.springboot整合事务事务分类:编程事务.声明事务(XML.注解),推荐使用注解方式,springboot默 ...
- Java框架spring Boot学习笔记(六):Spring Boot事务管理
SpringBoot和Java框架spring 学习笔记(十九):事务管理(注解管理)所讲的类似,使用@Transactional注解便可以轻松实现事务管理.
- Spring系列.事务管理
Spring提供了一致的事务管理抽象.这个抽象是Spring最重要的抽象之一, 它有如下的优点: 为不同的事务API提供一致的编程模型,如JTA.JDBC.Hibernate和MyBatis数据库层 ...
- Spring的事务管理
事务 事务:是逻辑上一组操作,要么全都成功,要么全都失败. 事务特性(ACID) 原子性:事务不可分割 一致性:事务执行的前后,数据完整性保持一致 隔离性:一个事务执行的时候,不应该受到其他事务的打扰 ...
- spring笔记--事务管理之声明式事务
事务简介: 事务管理是企业级应用开发中必不可少的技术,主要用来确保数据的完整性和一致性, 事务:就是一系列动作,它们被当作一个独立的工作单元,这些动作要么全部完成,要么全部不起作用. Spring中使 ...
- Spring应用——事务管理
事务基础:请参看:http://www.cnblogs.com/solverpeng/p/5720306.html 一.Spring 事务管理 1.前提:事务管理器 在使用 Spring 声明式事务管 ...
随机推荐
- hdu 2986 Ballot evaluation (Simulation)
Problem - 2986 之前在华工赛见过的一道简单的模拟,用map轻松干掉.为了精确,要全程用整型比较.轻松1y~ 代码如下: #include <cstdio> #include ...
- 深入Java线程管理(五):线程池
这几天主要是狂看源程序,在弥补了一些以前知识空白的同时,也学会了不少新的知识(比如 NIO),或者称为新技术吧. 线程池就是其中之一,一提到线程,我们会想到以前<操作系统>的生产者与消费者 ...
- PythonWeb框架之Django
Django 简介: Django是一个开放源代码的Web应用框架,由Python写成.采用了MTV的框架模式,即模型Model,模板Template和视图View.此框架设计模式借鉴了MVC框架的思 ...
- 深入java面向对象二:final关键字
文章内容源于对<疯狂java讲义>及<疯狂Java:突破程序员基本功的16课>学习和总结. 一. final成员变量 final 修饰变量时,表示该变量一旦获取了值就不可以改变 ...
- tf.variable_scope 参数
最近在看TensorFlow的变量管理,发现很多代码中tf.variable_scope()参数的数量及意义还不太清楚,特此记录: def __init__(self, name_or_scope, ...
- Python--day41--递归锁Rlock
1,递归锁Rlock:递归锁是为了解决死锁问题,且递归锁的特点是在同一个线程中可以被acquire()多次 多个acquire()在递归锁中不会阻塞,而互斥锁Lock就会阻塞 代码示例: from t ...
- sqlyog提示:没有执行查询。请在 SQL 窗口中输入查询或将光标放在一个查询上。
以下内容纯属猜测: sqlyog 在执行输入的sql语句时,对已经执行过的语句,是不会再执行的,所以出现这个问题,很可能是sql语句没有修改,就试图执行第二遍.
- VSCode提示没有权限,无法保存文件问题
重装了系统之后,重新打开了VSCode发现无法保存修改的文件,激活系统后发现还是无法保存文件,都是提示权限问题,原因在于文件夹权限继承并不是我所登录的这个用户,接着我试着按照网上的方法,在文件夹后,右 ...
- 第三阶段:3.Web端产品设计:4.产品设计-交互设计
交互设计主要做框架层以及结构层.包括交互关系,信息结构,界面布局,导航设计,信息内容. 导航关系非常重要. 这是框架层. 这是结构层. 要素就是信息内容.
- 基于MIG IP核的DDR3控制器(二)
上一节中,记录到了ddr控制器的整体架构,在本节中,准备把ddr控制器的各个模块完善一下. 可以看到上一节中介绍了DDR控制器的整体架构,因为这几周事情多,又要课设什么的麻烦,今天抽点时间把这个记录完 ...