溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶(hù)服務(wù)條款》

使用SpringBoot注解方式處理事務(wù)回滾的方法

發(fā)布時(shí)間:2020-08-13 11:22:40 來(lái)源:億速云 閱讀:935 作者:小新 欄目:開(kāi)發(fā)技術(shù)

小編給大家分享一下使用SpringBoot注解方式處理事務(wù)回滾的方法,相信大部分人都還不怎么了解,因此分享這篇文章給大家參考一下,希望大家閱讀完這篇文章后大有收獲,下面讓我們一起去了解一下吧!

我們?cè)赟pringBoot和MyBatis整合的時(shí)候,需要在SpringBoot中通過(guò)注解方式配置事務(wù)回滾

1 Pojo類(lèi)

package com.zxf.domain;

import java.util.Date;

public class User {
  private Integer id;
  private String name;
  private String pwd;
  private String head_img;
  private String phone;
  private Date create_time;

  public Integer getId() {
    return id;
  }

  public void setId(Integer id) {
    this.id = id;
  }

  public String getName() {
    return name;
  }

  public void setName(String name) {
    this.name = name;
  }

  public String getPwd() {
    return pwd;
  }

  public void setPwd(String pwd) {
    this.pwd = pwd;
  }

  public String getHead_img() {
    return head_img;
  }

  public void setHead_img(String head_img) {
    this.head_img = head_img;
  }

  public String getPhone() {
    return phone;
  }

  public void setPhone(String phone) {
    this.phone = phone;
  }

  public Date getCreate_time() {
    return create_time;
  }

  public void setCreate_time(Date create_time) {
    this.create_time = create_time;
  }
}

2 Mapper接口

我們這里使用注解的方式編寫(xiě)SQL語(yǔ)句

package com.zxf.mapper;

import com.zxf.domain.User;
import org.apache.ibatis.annotations.Insert;
import org.springframework.stereotype.Repository;

@Repository
public interface UserMapper {
  @Insert("insert into user (name,pwd,head_img,phone,create_time) values(#{name},#{pwd},#{head_img},#{phone},#{create_time})")
    public int save(User user);
}

3 Service接口和實(shí)現(xiàn)類(lèi)

package com.zxf.service;

import com.zxf.domain.User;


public interface UserService {
  public int save(User user);
}
package com.zxf.service.impl;
import com.zxf.domain.User;
import com.zxf.mapper.UserMapper;
import com.zxf.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
@Transactional //實(shí)現(xiàn)事務(wù)的時(shí)候要在業(yè)務(wù)類(lèi)中加入該注解
public class UserServiceImpl implements UserService {
  @Autowired
  private UserMapper userMapper;

  @Override
  public int save(User user) {
    int f= userMapper.save(user);
   // int x=5/0;
    return f;
  }
}

4 Controller層

package com.zxf.controller;

import com.zxf.domain.User;
import com.zxf.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.Date;
@RestController
@RequestMapping("api/v1/user")
public class UserController {
  @Autowired
  private UserService userService;
   @RequestMapping("save")
  public Object save(){
    User user=new User();
      user.setName("zhang3");
      user.setPwd("abc123");
      user.setCreate_time(new Date());
      user.setPhone("1789238734");
      user.setHead_img("aabbddd.jpg");
    userService.save(user);
    return user;
  }
}

5 application.properits 配置文件

spring.datasource.driver-class-name =com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/online
spring.datasource.username=root
spring.datasource.password=******
mybatis.configuration.log-impl=org.apache.ibatis.logging.stdout.StdOutImpl

6 pom文件

<&#63;xml version="1.0" encoding="UTF-8"&#63;>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.3.2.RELEASE</version>
    <relativePath/> <!-- lookup parent from repository -->
  </parent>
  <groupId>com.zxf</groupId>
  <artifactId>xf_spring2</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <name>xf_spring2</name>
  <properties>
    <java.version>1.8</java.version>
  </properties>
  <dependencies>
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <dependency>
      <groupId>org.mybatis.spring.boot</groupId>
      <artifactId>mybatis-spring-boot-starter</artifactId>
      <version>2.1.2</version>
    </dependency>
    <dependency>
      <groupId>mysql</groupId>
      <artifactId>mysql-connector-java</artifactId>
      <version>5.1.13</version>
    </dependency>
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-test</artifactId>
      <scope>test</scope>
      <exclusions>
        <exclusion>
          <groupId>org.junit.vintage</groupId>
          <artifactId>junit-vintage-engine</artifactId>
        </exclusion>
      </exclusions>
    </dependency>
  </dependencies>

  <build>
    <plugins>
      <plugin>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-maven-plugin</artifactId>
      </plugin>
    </plugins>
  </build>
</project>

6 SpringBoot啟動(dòng)類(lèi)

package com.zxf;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.transaction.annotation.EnableTransactionManagement;
@SpringBootApplication
@MapperScan("com.zxf.mapper")//掃描mapper接口
@EnableTransactionManagement//事務(wù)處理的時(shí)候啟動(dòng)類(lèi)必須要加的注解
public class XfSpring2Application {
  public static void main(String[] args) {
    SpringApplication.run(XfSpring2Application.class, args);
  }
}

7 也是最重要,也是很多人忽視的地方,就是MySQL要支持事務(wù)處理才可以

使用SpringBoot注解方式處理事務(wù)回滾的方法

這里一定要記住;否則你的SpringBoot的事務(wù)沒(méi)有任何效果

以上是使用SpringBoot注解方式處理事務(wù)回滾的方法的所有內(nèi)容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內(nèi)容對(duì)大家有所幫助,如果還想學(xué)習(xí)更多知識(shí),歡迎關(guān)注億速云行業(yè)資訊頻道!

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI