SpringBoot中如何实现数据层测试事务回滚的具体步骤?

2026-05-25 23:521阅读0评论SEO资讯
  • 内容介绍
  • 文章标签
  • 相关推荐

本文共计777个文字,预计阅读时间需要4分钟。

SpringBoot中如何实现数据层测试事务回滚的具体步骤?

目录 + 数据层测试事务回滚 + DAO下POJO对象 + Service + 测试用例数据设置 + 数据层测试事务回滚 + pom.xml导入依赖,包括mysql, MyBatis Plus等,如:groupId: com.baomidou, artifactId: mybatis-plus-boot-starter

目录
  • 数据层测试事务回滚
    • dao下
    • pojo对象
    • service
  • 测试用例数据设定

    数据层测试事务回滚

    pom.xml导入对应的一些坐标,mysql,Mp,等

    <dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus-boot-starter</artifactId> <version>3.5.2</version> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <optional>true</optional> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <scope>runtime</scope> </dependency>

    dao下

    import com.baomidou.mybatisplus.core.mapper.BaseMapper; import com.pojo.Person; import org.apache.ibatis.annotations.Mapper; import org.mybatis.spring.annotation.MapperScan; import org.springframework.stereotype.Component; import org.springframework.stereotype.Repository; @Mapper//使用注解配置映射 @Component//给spring管理,方便注入 public interface PersonDao extends BaseMapper<Person> { }

    pojo对象

    package com.pojo; import com.baomidou.mybatisplus.annotation.IdType; import com.baomidou.mybatisplus.annotation.TableId; import com.baomidou.mybatisplus.annotation.TableName; import lombok.Data; @Data @TableName("tb_user") public class Person { private Long id; private String username; private String password; private String gender; private String addr; }

    service

    package com.service; import com.baomidou.mybatisplus.core.metadata.IPage; import com.baomidou.mybatisplus.extension.service.IService; import com.pojo.Person; public interface PersonService extends IService<Person> { }

    serviceImpl

    @Service public class PersonServiceImpl extends ServiceImpl<PersonDao, Person> implements PersonService { }

    PersonServiceTest类下

    package com.serviceTest; import com.pojo.Person; import com.service.PersonService; import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.test.annotation.Rollback; import org.springframework.transaction.annotation.Transactional; @SpringBootTest @Transactional @Rollback(false) public class PersonServiceTest { @Autowired private PersonService personService; @Test void testAdd(){ Person person = new Person(); person.setUsername("测试回滚2"); person.setPassword("1"); person.setGender("1"); person.setAddr("1"); System.out.println(personService.save(person)); } }

    加上@Transactional运行

    加上@Transactional和@Rollback(false)运行

    为了测试用例添加事务,加上@Transactional,SpringBoot会对测试用例对应的事务提交操作进行回滚,也就是springboot识别到这个是test,所以不会进行提交事务,但是会占用id。不会有数据显示。

    如果想在测试用例中提交事务,可以通过@Rollback(false),不回滚,默认值是true,加上false就不会回滚,测试数据就能在数据库中显示出来。

    测试用例数据设定

    测试用例数据通常采用随机值进行测试,使用SpringBoot提供的随机数位器赋值

    ${random.int}表示随机整数

    ${random.int(10)}表示10以内的随机数

    ${random.int(10,20)}表示10到20的随机数

    其中()可以是任意字符,如[ ],@@都可以。

    配置文件下

    personRandom:
    age: ${random.int(1,100)}
    name: ${random.value}
    detail: ${random.uuid}

    定义一个类接收

    @Data @Component//给spring管理 @ConfigurationProperties(prefix = "personrandom") public class Person { private String name; private String age; private String detail; }

    测试类下

    SpringBoot中如何实现数据层测试事务回滚的具体步骤?

    @SpringBootTest public class RandomTest { @Autowired private Person person; @Test public void KC(){ System.out.println(person); } }

    运行结果

    到此这篇关于SpringBoot数据层测试事务回滚的实现流程的文章就介绍到这了,更多相关SpringBoot事务回滚内容请搜索自由互联以前的文章或继续浏览下面的相关文章希望大家以后多多支持自由互联!

    标签:实现

    本文共计777个文字,预计阅读时间需要4分钟。

    SpringBoot中如何实现数据层测试事务回滚的具体步骤?

    目录 + 数据层测试事务回滚 + DAO下POJO对象 + Service + 测试用例数据设置 + 数据层测试事务回滚 + pom.xml导入依赖,包括mysql, MyBatis Plus等,如:groupId: com.baomidou, artifactId: mybatis-plus-boot-starter

    目录
    • 数据层测试事务回滚
      • dao下
      • pojo对象
      • service
    • 测试用例数据设定

      数据层测试事务回滚

      pom.xml导入对应的一些坐标,mysql,Mp,等

      <dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus-boot-starter</artifactId> <version>3.5.2</version> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <optional>true</optional> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <scope>runtime</scope> </dependency>

      dao下

      import com.baomidou.mybatisplus.core.mapper.BaseMapper; import com.pojo.Person; import org.apache.ibatis.annotations.Mapper; import org.mybatis.spring.annotation.MapperScan; import org.springframework.stereotype.Component; import org.springframework.stereotype.Repository; @Mapper//使用注解配置映射 @Component//给spring管理,方便注入 public interface PersonDao extends BaseMapper<Person> { }

      pojo对象

      package com.pojo; import com.baomidou.mybatisplus.annotation.IdType; import com.baomidou.mybatisplus.annotation.TableId; import com.baomidou.mybatisplus.annotation.TableName; import lombok.Data; @Data @TableName("tb_user") public class Person { private Long id; private String username; private String password; private String gender; private String addr; }

      service

      package com.service; import com.baomidou.mybatisplus.core.metadata.IPage; import com.baomidou.mybatisplus.extension.service.IService; import com.pojo.Person; public interface PersonService extends IService<Person> { }

      serviceImpl

      @Service public class PersonServiceImpl extends ServiceImpl<PersonDao, Person> implements PersonService { }

      PersonServiceTest类下

      package com.serviceTest; import com.pojo.Person; import com.service.PersonService; import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.test.annotation.Rollback; import org.springframework.transaction.annotation.Transactional; @SpringBootTest @Transactional @Rollback(false) public class PersonServiceTest { @Autowired private PersonService personService; @Test void testAdd(){ Person person = new Person(); person.setUsername("测试回滚2"); person.setPassword("1"); person.setGender("1"); person.setAddr("1"); System.out.println(personService.save(person)); } }

      加上@Transactional运行

      加上@Transactional和@Rollback(false)运行

      为了测试用例添加事务,加上@Transactional,SpringBoot会对测试用例对应的事务提交操作进行回滚,也就是springboot识别到这个是test,所以不会进行提交事务,但是会占用id。不会有数据显示。

      如果想在测试用例中提交事务,可以通过@Rollback(false),不回滚,默认值是true,加上false就不会回滚,测试数据就能在数据库中显示出来。

      测试用例数据设定

      测试用例数据通常采用随机值进行测试,使用SpringBoot提供的随机数位器赋值

      ${random.int}表示随机整数

      ${random.int(10)}表示10以内的随机数

      ${random.int(10,20)}表示10到20的随机数

      其中()可以是任意字符,如[ ],@@都可以。

      配置文件下

      personRandom:
      age: ${random.int(1,100)}
      name: ${random.value}
      detail: ${random.uuid}

      定义一个类接收

      @Data @Component//给spring管理 @ConfigurationProperties(prefix = "personrandom") public class Person { private String name; private String age; private String detail; }

      测试类下

      SpringBoot中如何实现数据层测试事务回滚的具体步骤?

      @SpringBootTest public class RandomTest { @Autowired private Person person; @Test public void KC(){ System.out.println(person); } }

      运行结果

      到此这篇关于SpringBoot数据层测试事务回滚的实现流程的文章就介绍到这了,更多相关SpringBoot事务回滚内容请搜索自由互联以前的文章或继续浏览下面的相关文章希望大家以后多多支持自由互联!

      标签:实现