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事务管理的更多相关文章

  1. SpringBoot学习笔记(三):SpringBoot集成Mybatis、SpringBoot事务管理、SpringBoot多数据源

    SpringBoot集成Mybatis 第一步我们需要在pom.xml里面引入mybatis相关的jar包 <dependency> <groupId>org.mybatis. ...

  2. springboot 事务管理

    添加注解就完事儿了 @Transactional 1,Propagation  事务的传播行为 Propagation.REQUIRED 如果存在事务,就加入,没有的话就创建(默认值) 比如 A 方法 ...

  3. springboot mybatis 事务管理

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

  4. springboot xml声明式事务管理方案

    在开发过程中springboot提供的常见的事务解决方案是使用注解方式实现. 使用注解 在启动类上添加注解 @EnableTransactionManagement 在需要事务控制的方法添加@Tran ...

  5. springboot开启事务管理

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

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

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

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

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

  8. springboot使用 @Transactional 注解配置事务管理

    介绍 springboot对数据库事务的使用非常的方便,只需要在方法上添加@Transactional注解即可.Spring 为事务管理提供了丰富的功能支持.Spring 事务管理分为编程式和声明式的 ...

  9. SpringBoot 集成MyBatis、事务管理

    集成MyBatis (1)在pom.xml中添加依赖 <!-- mybatis的起步依赖.包含了mybatis.mybatis-spring.spring-jdbc(事务要用到)的坐标 --&g ...

随机推荐

  1. 最好用的 IntelliJ 插件 Top 10

    最好用的 IntelliJ 插件 Top 10 https://my.oschina.net/didispace/blog/1583238

  2. 最小生成树入门(克鲁斯卡尔+普利姆 hdu1233)

    克鲁斯卡尔 #include <set> #include <map> #include <queue> #include <stack> #inclu ...

  3. 帝国cms建站方法和知识点

    帝国cms建站方法和知识点 1.  首先在帝国cms网站上下载模板系统.根据模板系统上的提示,将指定的目录文件放在指定的位置.然后进行安装.后台管理系统的命名设置.数据库的设置等等. 2.  安装完成 ...

  4. Eclipse安装lombok及常用注解

    转自:https://blog.csdn.net/ZJDWHD/article/details/77795023 lombok的官方网址:http://projectlombok.org/ https ...

  5. Python 关于在ubuntu部署Django项目

    Django的部署可以有很多方式,采用nginx+uwsgi的方式是其中比较常见的一种方式. 在这种方式中,我们的通常做法是,将nginx作为服务器最前端,它将接收WEB的所有请求,统一管理请求.ng ...

  6. 【jquery隐藏、显示事件and提示callback】【淡入淡出fadeToggle】【滑入滑出slideToggle】【动画animate】【停止动画stop】

    1.jquery隐藏and显示事件 $("p").hide();      //隐藏事件$("p").hide(1000);  //1秒内缓慢隐藏$(" ...

  7. JSP表达式语音EF--2

    JSP 表达式语言 | 菜鸟教程 http://www.runoob.com/jsp/jsp-expression-language.html

  8. IMX6开发板学习烧写Linux-QT系统步骤做个笔记

    平台:迅为-i.MX6开发板  烧写系统:Linux-QT   <ignore_js_op>   Qt系统的烧写,是使用 MfgTool2 工具,只需要简单的配置下.   打开 “Mfgt ...

  9. spring源码学习2

    spring总览 从入口看起 我们用spring时会用ClassPathXmlApplicationContext来加载spring配置文件,就从它开始吧. 1.双击shhift,输入ClassPat ...

  10. 使用PHP、MySQL实现修改密码 + 防止通过url强行进入系统

    ●使用PHP+MySQL实现修改密码 页面: index.php  登陆页面,输入默认密码登陆系统 check.php 核查页面,通过查询数据库检测密码是否正确   ——> 正确,则进入系统   ...