SpringBoot事务管理
1、在UserMapper接口中添加更新和删除方法
package com.cppdy.mapper; import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update; import com.cppdy.entity.User; @Mapper
public interface UserMapper { @Select("select * from user where id=#{id}")
User getUserById(@Param("id") int id); @Insert("insert into user values(NULL,#{name})")
User insertUser(@Param("name") String name); @Update("update user set username=#{name} where id=#{id}")
User update(@Param("id") int id,@Param("name") String name); @Delete("delete from user where id=#{id}")
User delete(@Param("id") int id);
}
2、创建UserService接口
package com.cppdy.service;
public interface UserService {
//执行事务
public void tranfor();
}
3、创建UserServiceImpl实现类(@Transactional开启事务管理)
package com.cppdy.service.impl; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional; import com.cppdy.mapper.UserMapper;
import com.cppdy.service.UserService; @Service
public class UserServiceImpl implements UserService { @Autowired
private UserMapper userMapper; // 开启事务管理
@Transactional
public void tranfor() {
// 更新一条数据
userMapper.update(2, "cppdy123");
// 抛出一个异常
int a = 2 / 0;
// 删除一条数据
userMapper.delete(2);
} }
4、在HelloWordController类中创建测试方法
@RequestMapping("tranfor")
public void tranfor() {
userService.tranfor();
}
SpringBoot事务管理的更多相关文章
- SpringBoot学习笔记(三):SpringBoot集成Mybatis、SpringBoot事务管理、SpringBoot多数据源
SpringBoot集成Mybatis 第一步我们需要在pom.xml里面引入mybatis相关的jar包 <dependency> <groupId>org.mybatis. ...
- springboot 事务管理
添加注解就完事儿了 @Transactional 1,Propagation 事务的传播行为 Propagation.REQUIRED 如果存在事务,就加入,没有的话就创建(默认值) 比如 A 方法 ...
- springboot mybatis 事务管理
本文主要讲述springboot提供的声明式的事务管理机制. 一.一些概念 声明式的事务管理是基于AOP的,在springboot中可以通过@Transactional注解的方式获得支持,这种方式的优 ...
- springboot xml声明式事务管理方案
在开发过程中springboot提供的常见的事务解决方案是使用注解方式实现. 使用注解 在启动类上添加注解 @EnableTransactionManagement 在需要事务控制的方法添加@Tran ...
- springboot开启事务管理
spring中开启事务管理需要在xml配置文件中配置,springboot中采取java config的配置方式. 核心是@EnableTransactionManager注解,该注解即为开启事务管理 ...
- Springboot下事务管理的简单使用
关于事务管理的概念这里就不多介绍了,在我的博客“JDBC事务之理论篇”中也有介绍. 关于Spring的事务管理,主要是通过事务管理器来进行的.这里看个Spring事务管理的接口图:(来自博客https ...
- springboot(二)整合mybatis,多数据源和事务管理
-- 1.整合mybatis -- 2.整合多数据源 -- 3. 整合事务 代码地址:https://github.com/showkawa/springBoot_2017/tree/master/ ...
- springboot使用 @Transactional 注解配置事务管理
介绍 springboot对数据库事务的使用非常的方便,只需要在方法上添加@Transactional注解即可.Spring 为事务管理提供了丰富的功能支持.Spring 事务管理分为编程式和声明式的 ...
- SpringBoot 集成MyBatis、事务管理
集成MyBatis (1)在pom.xml中添加依赖 <!-- mybatis的起步依赖.包含了mybatis.mybatis-spring.spring-jdbc(事务要用到)的坐标 --&g ...
随机推荐
- python 的基础 学习 第三
1,in ,not in 判断子元素是是否在原字符串(字典,列表,集合)中,主要是用在检测敏感字 print('a' in 'abcchhhhd') 有则返回True, print('j' in 'a ...
- sql server 2008 windows验证改混合登陆中SqlServer身份验证用户名密码
安装过程中,SQL Server 数据库引擎设置为 Windows 身份验证模式或 SQL Server 和 Windows 身份验证模式.本主题介绍如何在安装后更改安全模式. 如果在安装过程中选择“ ...
- call,apply和bind的区别
call,apply继承并调用该函数,bind继承后未调用函数. var React = require('react'); //核心 var ReactDom = require('react-do ...
- mysql 与linux ~ 内存分析与调优
一 简介:linux内存和mysql二 分类 1 用户空间和内核空间 用户空间内存,从低到高分别是五种不同的内存段 1 只读段 包含代码和常量等 2 数据段 包含全局 ...
- 《Java编程思想第四版》附录 C Java 编程规则
附录 C Java 编程规则 本附录包含了大量有用的建议,帮助大家进行低级程序设计,并提供了代码编写的一般性指导: (1) 类名首字母应该大写.字段.方法以及对象(句柄)的首字母应小写.对于所有标识符 ...
- shiro-5基于url的权限管理
1.1 搭建环境 1.1.1 数据库 mysql5.1数据库中创建表:用户表.角色表.权限表(实质上是权限和资源的结合 ).用户角色表.角色权限表. 完成权限管理的数据模型创建. 1.1.2 开发环境 ...
- 深度学习——深度神经网络(DNN)反向传播算法
深度神经网络(Deep Neural Networks,简称DNN)是深度学习的基础. 回顾监督学习的一般性问题.假设我们有$m$个训练样本$\{(x_1, y_1), (x_2, y_2), …, ...
- Solr版本问题分析
在之前的Solr版本中(Solr5之前),在创建core的时候,Solr会自动创建好schema.xml,但是在之后的版本中,新加入了动态更新schema功能,这个默认的schema.xml确找不到了 ...
- Qemu-KVM管理
内容: 一.KVM基本配置 二.KVM网络的桥接 三.创建虚拟机 四.虚拟机的关闭和启动 关于KVM: 1).KVM是开源软件,全称是kernel-based virtual machine(基于内核 ...
- Linux网络底层收发探究【转】
转自:https://blog.csdn.net/davion_zhang/article/details/51536807 本文为博主原创文章,未经博主允许不得转载. https://blog.cs ...