Get the basic data type field of the entity through reflection and set its default value
If the database does not have a default value, this needs to be set manually. If the database does not have a default value, it is a waste of time to write fields one by one
There are three main steps to deal with it,
- Get all fields of the entity by reflection
private static final Map<Class<? >, List<Field>> FIELD_CACHES = new HashMap<>(100); Public static List<Field> getDeclaredFields(Class<? > clazz) { Assert.notNull(clazz, "Class must not be null"); List<Field> list = FIELD_CACHES.get(clazz); if (list == null) { try { Field[] currentFields = clazz.getDeclaredFields(); list = new ArrayList<>(Arrays.asList(currentFields)); Class<? > superClass = clazz.getSuperclass(); if (superClass ! = null) { Field[] superFields = superClass.getDeclaredFields(); list.addAll(new ArrayList<>(Arrays.asList(superFields))); } FIELD_CACHES.put(clazz, list); } catch (Throwable var3) { throw new IllegalStateException( "Failed to introspect Class [" + clazz.getName() + "] from ClassLoader [" + clazz.getClassLoader() + "]", var3); } } return list; }Copy the code
- Gets all fields and attributes corresponding to periods
Public static MAP <String, public static MAP <String, AliasValue> getPropertyAndValue(Object obj) { Class<? > tClass = obj.getClass(); List<Field> fields = getDeclaredFields(tClass); Map<String, AliasValue> map = new HashMap<>(fields.size()); String alias = ""; if (tClass.isAnnotationPresent(TableAlias.class)) { TableAlias tableAlias = tClass.getAnnotation(TableAlias.class); alias = tableAlias.value(); } for (Field Field: fields) {// Exclude serialVersionUID if (Modifier. IsStatic (field.getModiFIERS ())) {continue; } try { field.setAccessible(true); // Discard null Object value = field.get(obj); if (value == null) { continue; } map.put(field.getName(), new AliasValue(value, alias)); } catch (IllegalAccessException e) { e.printStackTrace(); } } return map; }Copy the code
- Set the period defaults based on the property values
Public static void setDefaultFiledValue(Class<?) public static void setDefaultFiledValue(Class<?) > clazz, Object obj) { if (obj == null) { throw new BaseException(BaseCodeEnum.DATA_NOT_EXISTS); } List<Field> fields = getDeclaredFields(clazz); Map<String, AliasValue> valueMap = getPropertyAndValue(obj); for (Field field : fields) { if (valueMap.get(field.getName()) == null) { try { field.setAccessible(true); if (String.class.equals(field.getType())) { field.set(obj, DefaultConstant.STRING); continue; } if (BigDecimal.class.equals(field.getType())) { field.set(obj, DefaultConstant.BIG_DECIMAL); continue; } if (Integer.class.equals(field.getType())) { field.set(obj, DefaultConstant.INTEGER); continue; } if (Long.class.equals(field.getType())) { field.set(obj, DefaultConstant.LONG); continue; } if (LocalDateTime.class.equals(field.getType())) { field.set(obj, DefaultConstant.DATE_TIME); continue; } if (LocalDate.class.equals(field.getType())) { field.set(obj, DefaultConstant.DATE); } } catch (IllegalAccessException e) { e.printStackTrace(); }}}}Copy the code