- 自動生成的時候用了 hutool 和 commons-lang3 ,這個可以用 java 原生的 random 生成
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.8</version>
</dependency>
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-all</artifactId>
<version>5.1.5</version>
</dependency>
- 運用 java 反射 實現對象的創建和屬性的賦值
public class Util {
/**
* 根據類名 創建一個隨機生成對象
*/
public static Object randomObject(Class<?> clazz) {
return randomObject_(clazz, new ArrayList<Class>());
}
/**
* 動態生成
*
* @param clazz 要生成的類
* @param hasClz 記錄已經生成過的類,爲了避免遞歸死循環
* @return
*/
private static Object randomObject_(Class<?> clazz, final List<Class> hasClz) {
Class sourceClass = clazz;
Object obj = null;
try {
obj = clazz.newInstance();
//獲取對象所有字段 包括父類
ArrayList<Field> fields = new ArrayList<>();
while (sourceClass != null) {
fields.addAll(Arrays.asList(sourceClass.getDeclaredFields()));
sourceClass = sourceClass.getSuperclass();
}
for (Field field : fields) {
//todo:通過自定義註解去篩掉哪些需要自動賦值,哪些不需要
//https://blog.csdn.net/qq_36666651/article/details/81215221
Class<?> type = parseClassType(field.getType());
field.setAccessible(true);
if (String.class == type) {
field.set(obj, RandomUtil.randomString(5));
} else if (Date.class == type) {
field.set(obj, randomDateTime());
} else if (type.isPrimitive()) {
if (boolean.class == type) {
field.set(obj, RandomUtil.randomBoolean());
} else if (char.class == type) {
field.set(obj, RandomUtil.randomChar());
} else if (byte.class == type) {
field.set(obj, RandomUtil.randomBytes(1)[0]);
} else if (short.class == type) {
short ashort = (short) (RandomUtil.randomInt(0, 99999) & 0x7FFF);
field.set(obj, ashort);
} else if (int.class == type) {
field.set(obj, RandomUtil.randomInt(0, 99999));
} else if (long.class == type) {
field.set(obj, RandomUtil.randomLong(0, 9999));
} else if (float.class == type) {
field.set(obj, RandomUtils.nextFloat(100, 9999));
} else if (double.class == type) {
field.set(obj, RandomUtil.randomDouble(10, 99999, 2, RoundingMode.DOWN));
}
} else if (hasClz.contains(type)) {
//已經生成過了,再次生成就陷入了遞歸循環
field.set(obj, null);
} else {
//以上類型都不是,就遞歸生成
hasClz.add(type);
field.set(obj, randomObject_(type, hasClz));
}
}
} catch (Exception e) {
e.printStackTrace();
}
return obj;
}
private static Date randomDateTime() {
String beginDate = "1900-01-01";
String endDate = "2020-01-01";
return randomDate(beginDate, endDate);
}
/**
* 隨機生成日期
*
* @param beginDate 開始日期
* @param endDate 結束日期
* @return
*/
public static Date randomDate(String beginDate, String endDate) {
try {
DateFormat format = new SimpleDateFormat("yyyy-MM-dd");
Date start = format.parse(beginDate);
Date end = format.parse(endDate);
if (start.getTime() >= end.getTime()) {
return new Date();
}
long date = random(start.getTime(), end.getTime());
return new Date(date);
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
private static long random(long begin, long end) {
long rtn = begin + (long) (Math.random() * (end - begin));
if (rtn == begin || rtn == end) {
return random(begin, end);
}
return rtn;
}
/**
* 將基礎類和包裝類相對應
* java.lang.Boolean#TYPE == boolean
* java.lang.Character#TYPE == character
* java.lang.Byte#TYPE == byte
* java.lang.Short#TYPE == short
* java.lang.Integer#TYPE == integer
* java.lang.Long#TYPE == long
* java.lang.Float#TYPE == float
* java.lang.Double#TYPE == double
* java.lang.Void#TYPE == void
*/
private static Class<?> parseClassType(Class<?> type) {
final Map<Class, Class> classes = new HashMap<>(9);
classes.put(Boolean.class, boolean.class);
classes.put(Character.class, char.class);
classes.put(Byte.class, byte.class);
classes.put(Short.class, short.class);
classes.put(Integer.class, int.class);
classes.put(Long.class, long.class);
classes.put(Float.class, float.class);
classes.put(Double.class, double.class);
return classes.getOrDefault(type, type);
}
參考:org.hibernate.query.spi.QueryParameterBindingValidator