Entity classes are quickly mapped to Map

/**
 * Entity class data acquisition and insertion into builder (reflection mechanism, deprecated)
 * @param obj the entity class that needs to get data
 * @param builder The Builder that needs to insert data
 * @throws Exception
 */
public static void entityGet(Object obj, Builder builder) throws Exception {
	Field[] fields = obj.getClass().getDeclaredFields();
	for (int j = 0; j < fields.length; j++) {
		Field field = fields[j];
		PropertyDescriptor pd = new PropertyDescriptor(field.getName(), obj.getClass());
		Method getMethod = pd.getReadMethod();
		if (getMethod != null) {
			if ("class java.util.Date".equals(field.getType().toString())
					&& getMethod.invoke(obj) != null) {
				builder.addField(field.getName(),
						new SimpleDateFormat("yyyy-MM-dd").format((Date) getMethod.invoke(obj)));
			} else if ("class java.lang.Integer".equals(field.getType().toString())
					&& getMethod.invoke(obj) != null) {
				builder.addField(field.getName(), Integer.toString((int) getMethod.invoke(obj)));
			} else {
				if (getMethod.invoke(obj) != null) {
					builder.addField(field.getName(), (String) getMethod.invoke(obj));
				} else {
					builder.addField(field.getName(), "");
				}
			}
		}
	}
}

The reflection mechanism was used for entity class mapping at the beginning, but it was found that the speed was not fast enough. Later, BeanMap was used for mapping, and the speed was doubled.

/**
 * Entity class data acquisition and insertion into builder (BeanMap mapping mechanism)
 * @param obj the entity class that needs to get data
 * @param builder The Builder that needs to insert data
 * @throws Exception
 */
@SuppressWarnings("unchecked")
public static Builder entityGetTest(AlarmInfluxDB obj, Builder builder) throws InterruptedException {
	BeanMap map = BeanMap.create(new AlarmInfluxDB());
	map.setBean(obj);
	builder.fields(map);
	return builder;
}
<dependency>  
	<groupId>cglib</groupId>  
	<artifactId>cglib-nodep</artifactId>  
	<version>2.2.2</version>  
</dependency>



Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324732117&siteId=291194637