springboot中junit回滚的作用是什么
springboot中使用junit编写单元测试,并且测试结果不影响数据库。pom引入依赖如果是IDE生成的项目,该包已经默认引入。org.springframework.bootspring-boot-starter-testtest数据库原始数据原始数据编写单元测试packagecom.mos.quote;importcom.mos.quote.model.Area;importcom.mos.quote.service.IAreaService;importorg.junit.Assert;impo
springboot中使用junit编写单元测试,并且测试结果不影响数据库。
pom引入依赖
如果是IDE生成的项目,该包已经默认引入。
org.springframework.boot spring-boot-starter-test test
数据库原始数据

原始数据
编写单元测试
package com.mos.quote;
import com.mos.quote.model.Area;
import com.mos.quote.service.IAreaService;
import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.annotation.Rollback;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.transaction.annotation.Transactional;
import java.util.List;
@RunWith(SpringRunner.class)
@SpringBootTest
public class QuoteApplicationTests {
@Autowired
private IAreaService areaService;
@Test
public void contextLoads() {
}
@Test
public void testUpdate(){
Area area = new Area();
area.setCode("001003");
area.setName("洛阳市");
Integer result = areaService.update(area);
Assert.assertEquals(1, (long)result);
}
@Test
@Transactional
@Rollback
public void testUpdate4Rollback(){
Area area = new Area();
area.setCode("001001");
area.setName("郑州市123");
Integer result = areaService.update(area);
Assert.assertEquals(1, (long)result);
}
}结果数据

结果数据
结论
可以看出code=001001的数据没有更改,而code=001003的数据修改成功。回头看代码:
@Transactional表示该方法整体为一个事务,
@Rollback表示事务执行完回滚,支持传入一个参数value,默认true即回滚,false不回滚。
该注解一样支持对类的注解,若如此做,对整个class的方法有效。

注解在class上
Windows 10 是一款微软推出的经典操作系统,拥有硬件兼容性与多任务处理能力。它更偏向把系统状态查看和常用调节动作放在一起,适合需要持续观察和微调设备状态的场景。
极度公式是一款跨平台专业LaTeX公式识别编辑软件,支持OCR公式识别和多平台编辑。和使用说明,避免使用,享受完整功能与稳定支持。做扫描整理、文字提取和表格转换时,它能把识别后的处理步骤接得更顺,资料录入这类场景会省下不少时间。
















