Spring Boot事务实战

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/chengqiuming/article/details/83214451

一 新建项目

1 新增依赖

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-jpa</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
        <dependency>
        <groupId>com.oracle</groupId>
        <artifactId>ojdbc6</artifactId>
        <version>11.2.0.2.0</version>
    </dependency>
    
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>
</dependencies>

2 配置application.properties

spring.datasource.driverClassName=oracle.jdbc.OracleDriver
spring.datasource.url=jdbc\:oracle\:thin\:@localhost\:1521\:xe
spring.datasource.username=system
spring.datasource.password=oracle

spring.jpa.hibernate.ddl-auto=update
spring.jpa.show-sql=true

spring.jackson.serialization.indent_output=true

二 新建实体类

package com.wisely.ch8_4.domain;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;

@Entity
public class Person {
    @Id
    @GeneratedValue
    private Long id;
    
    private String name;
    
    private Integer age;
    
    private String address;
    
    
    public Person() {
        super();
    }
    public Person(Long id, String name, Integer age, String address) {
        super();
        this.id = id;
        this.name = name;
        this.age = age;
        this.address = address;
    }
    public Long getId() {
        return id;
    }
    public void setId(Long id) {
        this.id = id;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public Integer getAge() {
        return age;
    }
    public void setAge(Integer age) {
        this.age = age;
    }
    public String getAddress() {
        return address;
    }
    public void setAddress(String address) {
        this.address = address;
    }


}

三 新建实体类的Repository

package com.wisely.ch8_4.dao;

import org.springframework.data.jpa.repository.JpaRepository;

import com.wisely.ch8_4.domain.Person;

public interface PersonRepository extends JpaRepository<Person, Long> {
    

}

四 业务服务

1 服务接口

package com.wisely.ch8_4.service;

import com.wisely.ch8_4.domain.Person;

public interface DemoService {
    public Person savePersonWithRollBack(Person person);
    public Person savePersonWithoutRollBack(Person person);

}

2 服务实现

package com.wisely.ch8_4.service.impl;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.wisely.ch8_4.dao.PersonRepository;
import com.wisely.ch8_4.domain.Person;
import com.wisely.ch8_4.service.DemoService;
@Service
public class DemoServiceImpl implements DemoService {
    @Autowired
    PersonRepository personRepository; //直接注入PersonRepository的Bean
    
    @Transactional(rollbackFor={IllegalArgumentException.class}) //rollbackFor指定特定异常时,数据回滚
    public Person savePersonWithRollBack(Person person){
        Person p =personRepository.save(person);

        if(person.getName().equals("汪云飞")){
            throw new IllegalArgumentException("汪云飞已存在,数据将回滚"); //硬编码手动触发异常
        }
        return p;
    }

    @Transactional(noRollbackFor={IllegalArgumentException.class}) //noRollbackFor指定特定异常时,数据不回滚
    public Person savePersonWithoutRollBack(Person person){
        Person p =personRepository.save(person);
        
        if(person.getName().equals("汪云飞")){
            throw new IllegalArgumentException("汪云飞虽已存在,数据将不会回滚");
        }
        return p;
    }
}

五 控制器

package com.wisely.ch8_4.web;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.wisely.ch8_4.domain.Person;
import com.wisely.ch8_4.service.DemoService;

@RestController
public class MyController {
    @Autowired
    DemoService demoService;
    
    @RequestMapping("/rollback")
    public Person rollback(Person person){ //测试回滚情况
        
        return demoService.savePersonWithRollBack(person);
    }
    
    @RequestMapping("/norollback")
    public Person noRollback(Person person){//测试不回滚情况
        
        return demoService.savePersonWithoutRollBack(person);
        
        
    }

}

六 测试

1 测试不回滚

浏览器输入:http://localhost:8080/norollback?name=汪云飞&age=32

数据库新增了一条记录,如下图

2 测试回滚情况

浏览器输入:http://localhost:8080/rollback?name=汪云飞&age=32

数据库没有发生变化,说明回滚成功

猜你喜欢

转载自blog.csdn.net/chengqiuming/article/details/83214451
今日推荐