欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

使用SpringBoot注解方式處理事務(wù)回滾實(shí)現(xiàn)

 更新時(shí)間:2020年08月12日 11:40:06   作者:風(fēng)學(xué)長  
這篇文章主要介紹了使用SpringBoot注解方式處理事務(wù)回滾實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧

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

1 Pojo類

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接口

我們這里使用注解的方式編寫SQL語句

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)類

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ù)類中加入該注解
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文件

<?xml version="1.0" encoding="UTF-8"?>
<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)類

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)類必須要加的注解
public class XfSpring2Application {
  public static void main(String[] args) {
    SpringApplication.run(XfSpring2Application.class, args);
  }
}

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


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

到此這篇關(guān)于使用SpringBoot注解方式處理事務(wù)回滾實(shí)現(xiàn)的文章就介紹到這了,更多相關(guān)SpringBoot注解處理事務(wù)回滾內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • Spring Cloud Zuul路由網(wǎng)關(guān)服務(wù)過濾實(shí)現(xiàn)代碼

    Spring Cloud Zuul路由網(wǎng)關(guān)服務(wù)過濾實(shí)現(xiàn)代碼

    這篇文章主要介紹了Spring Cloud Zuul路由網(wǎng)關(guān)服務(wù)過濾實(shí)現(xiàn)代碼,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2020-04-04
  • OpenFeign實(shí)現(xiàn)遠(yuǎn)程調(diào)用

    OpenFeign實(shí)現(xiàn)遠(yuǎn)程調(diào)用

    這篇文章主要為大家詳細(xì)介紹了OpenFeign實(shí)現(xiàn)遠(yuǎn)程調(diào)用,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2022-08-08
  • 解決@Scope(“prototype“)不生效的問題

    解決@Scope(“prototype“)不生效的問題

    這篇文章主要介紹了解決@Scope(“prototype“)不生效的問題,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-06-06
  • Java之Mybatis的二級緩存

    Java之Mybatis的二級緩存

    本文主要介紹Java中Mybatis的二級緩存,緩存就是一塊內(nèi)存空間,保存臨時(shí)數(shù)據(jù),它是SqlSessionFactory的緩存,對Mybaits感興趣的小伙伴可以參考閱讀
    2023-03-03
  • spring-boot報(bào)錯(cuò)java: 程序包javax.servlet.http不存在

    spring-boot報(bào)錯(cuò)java: 程序包javax.servlet.http不存在

    當(dāng)springboot項(xiàng)目從2.7.x的升級到3.0.x的時(shí)候,會(huì)遇到一個(gè)問題java: 程序包javax.servlet.http不存在,下面就來具體介紹一下,感興趣的可以了解一下
    2024-08-08
  • springmvc視圖解析流程代碼實(shí)例

    springmvc視圖解析流程代碼實(shí)例

    這篇文章主要介紹了springmvc視圖解析流程代碼實(shí)例,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2020-01-01
  • java學(xué)習(xí)之猜數(shù)字小游戲

    java學(xué)習(xí)之猜數(shù)字小游戲

    這篇文章主要為大家詳細(xì)介紹了java學(xué)習(xí)之猜數(shù)字小游戲,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2019-07-07
  • Java經(jīng)典算法匯總之選擇排序(SelectionSort)

    Java經(jīng)典算法匯總之選擇排序(SelectionSort)

    選擇排序也是比較簡單的一種排序方法,原理也比較容易理解,選擇排序在每次遍歷過程中只記錄下來最小的一個(gè)元素的下標(biāo),待全部比較結(jié)束之后,將最小的元素與未排序的那部分序列的最前面一個(gè)元素交換,這樣就降低了交換的次數(shù),提高了排序效率。
    2016-04-04
  • Java中高效的對象映射庫Orika的用法詳解

    Java中高效的對象映射庫Orika的用法詳解

    Orika是一個(gè)高效的Java對象映射庫,專門用于在Java應(yīng)用程序中簡化對象之間的轉(zhuǎn)換,下面就跟隨小編一起來深入了解下Orika的具體使用吧
    2024-11-11
  • maven打包上傳到私有倉庫的實(shí)現(xiàn)步驟

    maven打包上傳到私有倉庫的實(shí)現(xiàn)步驟

    這篇文章主要介紹了maven打包上傳到私有倉庫的實(shí)現(xiàn)步驟,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-01-01

最新評論