亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

SpringBoot數(shù)據(jù)層測(cè)試事務(wù)回滾的實(shí)現(xiàn)流程

 更新時(shí)間:2022年10月22日 14:47:03   作者:執(zhí)久呀  
這篇文章主要介紹了SpringBoot數(shù)據(jù)層測(cè)試事務(wù)回滾的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧

數(shù)據(jù)層測(cè)試事務(wù)回滾

pom.xml導(dǎo)入對(duì)應(yīng)的一些坐標(biāo),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對(duì)象

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("測(cè)試回滾2");
        person.setPassword("1");
        person.setGender("1");
        person.setAddr("1");
        System.out.println(personService.save(person));
    }
}

加上@Transactional運(yùn)行

加上@Transactional和@Rollback(false)運(yùn)行

為了測(cè)試用例添加事務(wù),加上@Transactional,SpringBoot會(huì)對(duì)測(cè)試用例對(duì)應(yīng)的事務(wù)提交操作進(jìn)行回滾,也就是springboot識(shí)別到這個(gè)是test,所以不會(huì)進(jìn)行提交事務(wù),但是會(huì)占用id。不會(huì)有數(shù)據(jù)顯示。

如果想在測(cè)試用例中提交事務(wù),可以通過@Rollback(false),不回滾,默認(rèn)值是true,加上false就不會(huì)回滾,測(cè)試數(shù)據(jù)就能在數(shù)據(jù)庫中顯示出來。

測(cè)試用例數(shù)據(jù)設(shè)定

測(cè)試用例數(shù)據(jù)通常采用隨機(jī)值進(jìn)行測(cè)試,使用SpringBoot提供的隨機(jī)數(shù)位器賦值

${random.int}表示隨機(jī)整數(shù)

${random.int(10)}表示10以內(nèi)的隨機(jī)數(shù)

${random.int(10,20)}表示10到20的隨機(jī)數(shù)

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

配置文件下

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

定義一個(gè)類接收

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

測(cè)試類下

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

運(yùn)行結(jié)果

到此這篇關(guān)于SpringBoot數(shù)據(jù)層測(cè)試事務(wù)回滾的實(shí)現(xiàn)流程的文章就介紹到這了,更多相關(guān)SpringBoot事務(wù)回滾內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • Mac上配置JDK?1.8的超詳細(xì)流程

    Mac上配置JDK?1.8的超詳細(xì)流程

    相信每個(gè)拿到MAC的小伙伴都是很欣喜的,但是由于MAC系統(tǒng)與WIN系統(tǒng)有著極大的不同,所以使用起來會(huì)有一些小困擾,這篇文章主要給大家介紹了關(guān)于Mac上配置JDK?1.8的超詳細(xì)流程,需要的朋友可以參考下
    2023-11-11
  • Java中使用HashMap改進(jìn)查找性能的步驟

    Java中使用HashMap改進(jìn)查找性能的步驟

    這篇文章主要介紹了Java中使用HashMap改進(jìn)查找性能的步驟,幫助大家更好的理解和使用Java,感興趣的朋友可以了解下
    2021-02-02
  • Java+opencv3.2.0實(shí)現(xiàn)模板匹配

    Java+opencv3.2.0實(shí)現(xiàn)模板匹配

    這篇文章主要為大家詳細(xì)介紹了Java+opencv3.2.0實(shí)現(xiàn)模板匹配的相關(guān)資料,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2018-02-02
  • Java求最小生成樹的兩種算法詳解

    Java求最小生成樹的兩種算法詳解

    最小生成樹(Minimum Spanning Tree):在連通圖的所有生成樹中,所有邊的權(quán)值和最小的生成樹,稱為最小生成樹。這篇文章主要介紹了求最小生成樹的兩種方法:Prim算法和Kruskal算法,需要的可以參考一下
    2022-01-01
  • 詳解Java5、Java6、Java7的新特性

    詳解Java5、Java6、Java7的新特性

    本編文章詳細(xì)介紹了Java5、Java6、Java7的新特性,需要的朋友可以參考下
    2017-04-04
  • java中動(dòng)態(tài)代理如何實(shí)現(xiàn)詳解

    java中動(dòng)態(tài)代理如何實(shí)現(xiàn)詳解

    動(dòng)態(tài)代理是基于接口實(shí)現(xiàn)的代理,mybatis就是用這個(gè)技術(shù)實(shí)現(xiàn)的,下面這篇文章主要給大家介紹了關(guān)于java中動(dòng)態(tài)代理如何實(shí)現(xiàn)的相關(guān)資料,需要的朋友可以參考下
    2024-01-01
  • SWT(JFace)體驗(yàn)之RowLayout布局

    SWT(JFace)體驗(yàn)之RowLayout布局

    相對(duì)于FillLayout來說,RowLayout比較靈活,功能也比較強(qiáng)。用戶可以設(shè)置布局中子元素的大小、邊距、換行及間距等屬性。
    2009-06-06
  • Java面試之限流的實(shí)現(xiàn)方式小結(jié)

    Java面試之限流的實(shí)現(xiàn)方式小結(jié)

    限流是指在各種應(yīng)用場(chǎng)景中,通過技術(shù)和策略手段對(duì)數(shù)據(jù)流量、請(qǐng)求頻率或資源消耗進(jìn)行有計(jì)劃的限制,本文為大家整理了常見的限流的實(shí)現(xiàn)方式,有需要的可以參考下
    2024-02-02
  • Spring?Data?JPA?映射VO/DTO對(duì)象方式

    Spring?Data?JPA?映射VO/DTO對(duì)象方式

    這篇文章主要介紹了Spring?Data?JPA?映射VO/DTO對(duì)象方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-11-11
  • Mybatis-Plus中的selectByMap使用實(shí)例

    Mybatis-Plus中的selectByMap使用實(shí)例

    Mybatis-Plus來對(duì)數(shù)據(jù)庫進(jìn)行增刪改查時(shí),將里面的函數(shù)試了個(gè)遍,接下來我就將使用selectByMap函數(shù)的簡(jiǎn)單測(cè)試實(shí)例寫出來,方便沒有使用過的朋友們快速上手,感興趣的可以了解一下
    2021-11-11

最新評(píng)論