当前位置:网站首页>Entity annotation - batch generate 10000 test data
Entity annotation - batch generate 10000 test data
2022-07-21 05:22:00 【mabo_ [email protected]】
Tips : When the article is finished , Directories can be generated automatically , How to generate it, please refer to the help document on the right
Entity annotations - Batch build 10000 Bar test data
One 、 Preface
Recently received a demand , Generate test data for multiple tables to test the front-end statistics page Some fields require selection from fixed values department , Personnel and other requirements are in Chinese Dates are required in certain time periods … wait
Of course, the first thing I thought of was writing tools , Then generate the corresponding data . But next time, if there is such a demand , Do I have to write one by one , Can you simplify .
This method can solve the problem of quickly generating test data from a single table , The data generated by multi table association still needs to be studied
Last , I thought of writing a test data batch generation tool for entity classes , The demonstration is as follows
Two 、 effect
First create the entity class
Entity class
public class TestEntity {
private String name;
private Integer age;
private String tel;
private String dept;
private Date date;
}
Generate data SQL And implement
3、 ... and 、 Realization principle
Generate test data according to entity classes , The core principles of implementation are annotation and reflection , It mainly goes through the following three steps
Get the annotation attributes and parameters of the entity
Currently, the only supported data types are String,int Date type
First, get the attributes of the incoming class and the parameters annotated on the attributes
Field[] fields = aClass.getDeclaredFields();
StringConfigure stringConfigure = field.getAnnotation(StringConfigure.class);
among ,String Types of data support the most generation methods , Take an example to illustrate , Here are String Detailed parameters of annotation of type
@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.FIELD)
public @interface StringConfigure {
// The column name of this attribute in the database
String fieldName() default "";
// Rules for generating test data ,NAME,TELNUMBER,UUID16,UUID,METHOD
StringRule createRule() default StringRule.UUID16;
// When createRule by METHOD The following two parameters take effect
// Used to indicate custom parameter generation rules
Class aClass() default Class.class;
String method() default "";
}
Execute the method to obtain test data according to the attributes of the entity
Reflection method internal call RandUtils Method , Such as :
String s = RandUtils.uuId();
RandUtils.uuId16();
RandUtils.name();
Of course, you can use custom tools to generate data
When the built-in string generation method cannot meet the requirements , You need to specify a custom method to generate strings , have access to StringConfigure Comments are indicated by the following parameters
The following example indicates the use of DefaultList Class getDept Method to generate a string
Be careful :
Method must have no parameters , The return value is String type
@StringConfigure(createRule = StringRule.METHOD,aClass = DefaultList.class, method = "getDept")
private String dept;
// Here are
public class DefaultList {
private static String[] deptList=new String[]{
" Product management module "," Tmall information service is limited "," Technology management section "," R & D department "," Manufacturing department "," Product development department "," Material department "
," Equipment department "," Customer service department "," Import and export department "," Information Department "," Planning Department "," Public relations "," The personnel department "," Financial business module "};
public DefaultList() {
}
public static String getDept() {
return deptList[RandUtils.num(0,deptList.length-1)];
}
}
The assembly test data is SQL
StringBuffer sql = new StringBuffer("INSERT INTO " + tableName + " (");
for (int i = 0; i < fields.length; i++) {
Field field=fields[i];
sql=sql.append(fieldName+") values (");
}
Core source code
public static String createSql(Class aClass, String tableName) throws Exception {
Field[] fields = aClass.getDeclaredFields();
Class<DefaultList> randListClass = DefaultList.class;
Method[] randListClassDeclaredMethods = randListClass.getDeclaredMethods();
StringBuffer sql = new StringBuffer("INSERT INTO " + tableName + " (");
for (int i = 0; i < fields.length; i++) {
Field field=fields[i];
StringConfigure stringConfigure = field.getAnnotation(StringConfigure.class);
DateConfigure dateConfigure = field.getAnnotation(DateConfigure.class);
IntConfigure intConfigure = field.getAnnotation(IntConfigure.class);
String fieldName=null;
if (intConfigure != null) {
fieldName=intConfigure.fieldName();
} else if (dateConfigure != null) {
fieldName=dateConfigure.fieldName();
} else if (stringConfigure != null) {
fieldName=stringConfigure.fieldName();
}
if (fieldName.equals("")){
fieldName=field.getName();
}
if (i!= fields.length-1){
sql=sql.append(fieldName+",");
}else {
sql=sql.append(fieldName+") values (");
}
}
for (int i = 0; i < fields.length; i++) {
Field field=fields[i];
String appendS=null;
StringConfigure stringConfigure = field.getAnnotation(StringConfigure.class);
DateConfigure dateConfigure = field.getAnnotation(DateConfigure.class);
IntConfigure intConfigure = field.getAnnotation(IntConfigure.class);
if (intConfigure!=null){
int min = intConfigure.min();
int max = intConfigure.max();
int num = RandUtils.num(min, max);
appendS=String.valueOf(num);
}else if(dateConfigure!=null){
String end = dateConfigure.end();
String start = dateConfigure.start();
Date date = RandUtils.date(start, end,"yyyy-MM-dd HH:mm:ss");
appendS="'"+date2String(date)+"'";
}
else if(stringConfigure!=null){
StringRule rule = stringConfigure.createRule();
if (rule.equals(StringRule.UUID)){
String s = RandUtils.uuId();
appendS="'"+s+"'";
}
else if (rule.equals(StringRule.UUID16)){
appendS="'"+RandUtils.uuId16()+"'";
}
else if (rule.equals(StringRule.NAME)){
appendS="'"+RandUtils.name()+"'";
}
else if (rule.equals(StringRule.TELNUMBER)){
appendS="'"+RandUtils.telNum()+"'";
}
else if (rule.equals(StringRule.METHOD)){
Class arrayClass = stringConfigure.aClass();
if (!arrayClass.equals(Class.class)){
aClass=arrayClass;
}
else {
aClass= DefaultList.class;
}
String arrayName = stringConfigure.method();
for (Method method : randListClassDeclaredMethods) {
String name = method.getName();
if (arrayName.equals(name)){
Object o = aClass.newInstance();
String strings = (String) method.invoke(o);
appendS="'"+strings+"'";
break;
}
}
}
}
if (i!= fields.length-1){
sql=sql.append(appendS+",");
}else {
sql=sql.append(appendS+");");
}
}
return sql.toString();
}
Four 、 How to use
1、 Author entity
package com.mabo.example.entity;
import com.mabo.sql.DefaultList;
import com.mabo.sql.StringRule;
import com.mabo.sql.annotation.DateConfigure;
import com.mabo.sql.annotation.IntConfigure;
import com.mabo.sql.annotation.StringConfigure;
import java.util.Date;
public class TestEntity {
@StringConfigure(createRule = StringRule.NAME)
private String name;
@IntConfigure(max = 50,min=20)
private Integer age;
@StringConfigure(fieldName = "telePhone",createRule = StringRule.TELNUMBER)
private String tel;
@StringConfigure(createRule = StringRule.METHOD,aClass = DefaultList.class, method = "getDept")
private String dept;
@DateConfigure(start = "2021-07-19 00:00:00",end = "2022-07-19 00:00:00")
private Date date;
}
2、 Introduce dependency
Private clothes , The server downloads very fast , There is a demonstration inside demo
Private service download address : http://47.103.194.1:8081/download/demo?fileName=AutoData.zip
github Download address : https://github.com/MaBo2420935619/AutoData
3、 Execute reflection method generation SQL
package com.mabo.example;
import com.mabo.example.entity.TestEntity;
import com.mabo.sql.CreateSqlReflect;
public class Test {
public static void main(String[] args) throws Exception {
String test = CreateSqlReflect.createSql(TestEntity.class, "TEST", 10);
System.out.println(test);
}
}
版权声明
本文为[mabo_ [email protected]]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/202/202207200716565252.html
边栏推荐
猜你喜欢
Addition, deletion, query and modification of MySQL [advanced]
Filebeat 的学习笔记
不用线段树,一个思路解决所有我的日程安排表问题
Error: Cannot run with sound null safety, because the following dependencies don‘t support null safe
AutoJs学习-实现透明状态栏
The use and difference of extern and static as global variables
node-js 异步处理图片,异步处理json文本
PO,BO,VO,DTO和POJO的概念区分
Jmeter 接口必加元素
[Verilog digital system design (Xia Yuwen) -- basic knowledge of Verilog 2]
随机推荐
小程序毕设作品之微信运动场地预约小程序毕业设计(6)开题答辩PPT
Arduino + SI5351 方波发生器
力扣 10. 正则表达式匹配
【深入浅出玩转FPGA9------经验点滴】
JS 将几个对象存入数组中的简便写法
Flume的学习笔记
The difference between copy and copy in XMIND
AutoJs学习-实现透明状态栏
1.54寸TFT ST7789液晶屏图片如何取模
不用线段树,一个思路解决所有我的日程安排表问题
【HBuilder运行到MuMu模拟器无法安装基座的问题,一直卡在安装基座...】
pypi 统计下载次数
Reducing Participation Costs via Incremental Verification for Ledger Systems学习笔记
接口遇到jsonString应该怎么处理
Daily question 1: specified interval reversal in the linked list
逆向分析工具IDA与开源工具Ghidra、Cutter对比测评
力扣 剑指 Offer II 101. 分割等和子集
go runtime 包
extern 、static 作为全局变量的使用与差异
Qt 多线程实现的两种方式 线程实现