目录
pom.xml导入对应的一些坐标,mysql,Mp,等
- <dependency>
-
- <groupId>com.baomidougroupId>
- <artifactId>mybatis-plus-boot-starterartifactId>
- <version>3.5.2version>
- dependency>
- <dependency>
- <groupId>org.projectlombokgroupId>
- <artifactId>lombokartifactId>
- <optional>trueoptional>
- dependency>
-
- <dependency>
- <groupId>mysqlgroupId>
- <artifactId>mysql-connector-javaartifactId>
- <scope>runtimescope>
- dependency>
- 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
{ -
- }
- 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;
-
- }
- 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
{ -
-
- }
-
-
- @Service
- public class PersonServiceImpl extends ServiceImpl
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;
- }
测试类下
- @SpringBootTest
- public class RandomTest {
- @Autowired
- private Person person;
- @Test
- public void KC(){
- System.out.println(person);
- }
- }
运行结果
