在实际使用中,使用Spring Boot默认的配置就能满足我们绝大多数需求。在本节的实战里,我们将演示如何使用@Transactional使用异常导致数据回滚和使用异常让数据不会滚。
1.新建Spring Boot项目
新建Spring Boot项目,依赖为JPA,Web。由于本项目连接了SQL Server数据库,因此多一个依赖SQL Server。
pom.xml:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="maven.apache./POM/4.0.0" xmlns:xsi="w3./2001/XMLSchema-instance"xsi:schemaLocation="maven.apache./POM/4.0.0 maven.apache./xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><parent><groupId>.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>2.1.2.RELEASE</version><relativePath/> <!-- lookup parent from repository --></parent><groupId>.jxufe</groupId><artifactId>demopro</artifactId><version>0.0.1-SNAPSHOT</version><name>demopro</name><description>Demo project for Spring Boot</description><properties><java.version>1.8</java.version></properties><dependencies><dependency><groupId>.springframework.boot</groupId><artifactId>spring-boot-starter-data-jpa</artifactId></dependency><dependency><groupId>.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><dependency><groupId>.microsoft.sqlserver</groupId><artifactId>mssql-jdbc</artifactId><scope>runtime</scope></dependency><dependency><groupId>.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope></dependency></dependencies><build><plugins><plugin><groupId>.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId></plugin></plugins></build></project>
2.实体类
package .jxufe.demopro.domain;import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;@Entity
public class Person {@Id@GeneratedValue(strategy = GenerationType.IDENTITY)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;}
}
3.实体类Repository
package .jxufe.demopro.dao;import .springframework.data.jpa.repository.JpaRepository;import .jxufe.demopro.domain.Person;public interface PersonRepository extends JpaRepository<Person, Long> {}
4.业务服务Service
(1)服务接口:
package .jxufe.demopro.service;import .jxufe.demopro.domain.Person;public interface DemoService {public Person savePersonWithRollBack(Person person);public Person savePersonWithoutRollBack(Person person);
}
(2)服务实现:
package .jxufe.demopro.service.impl;import javax.transaction.Transactional;import .springframework.beans.factory.annotation.Autowired;
import .springframework.stereotype.Service;import .jxufe.demopro.dao.PersonRepository;
import .jxufe.demopro.domain.Person;
import .jxufe.demopro.service.DemoService;@Service
public class DemoServiceImpl implements DemoService{@Autowiredprivate PersonRepository personRepository;@Transactional(rollbackOn = {IllegalArgumentException.class})@Overridepublic Person savePersonWithRollBack(Person person) {Person p = personRepository.save(person);if(p.getName().equals("徐彤")) {throw new IllegalArgumentException("徐彤已经存在,数据将回滚!!");}return p;}@Transactional(dontRollbackOn = {IllegalArgumentException.class})@Overridepublic Person savePersonWithoutRollBack(Person person) {Person p = personRepository.save(person);if(p.getName().equals("徐彤")) {throw new IllegalArgumentException("徐彤已经存在,但不会回滚!!");}return p;}
}
5.控制器
package .jxufe.demopro.controller;import .springframework.beans.factory.annotation.Autowired;
import .springframework.web.bind.annotation.RequestMapping;
import .springframework.web.bind.annotation.RestController;import .jxufe.demopro.domain.Person;
import .jxufe.demopro.service.DemoService;@RestController
public class MyController {@Autowiredprivate DemoService demoService;@RequestMapping("/rollback")public Person rollback(Person person) {return demoService.savePersonWithRollBack(person);}@RequestMapping("/norollback")public Person noRollback(Person person) {return demoService.savePersonWithoutRollBack(person);}}
6.application.properties
server.port=8443
spring.datasource.driver-class-name=.microsoft.sqlserver.jdbc.SQLServerDriver
spring.datasource.url=jdbc:sqlserver://127.0.0.1:1433; databaseName=Person
spring.datasource.username=sa
spring.datasource.password=12345678
经过测试,上述代码可以成功运行。
更多推荐
声明,事务,Spring,Boot
发布评论