|
@@ -11,6 +11,9 @@ import java.util.Objects;
|
|
|
import com.chelvc.framework.common.util.JacksonUtils;
|
|
|
import com.chelvc.framework.common.util.ObjectUtils;
|
|
|
import com.chelvc.framework.common.util.StringUtils;
|
|
|
+import com.chelvc.framework.database.annotation.Sensitive;
|
|
|
+import com.chelvc.framework.database.context.DatabaseContextHolder;
|
|
|
+import com.chelvc.framework.database.handler.JsonTypeHandler;
|
|
|
import com.google.common.collect.Maps;
|
|
|
import lombok.AllArgsConstructor;
|
|
|
import lombok.Data;
|
|
@@ -51,7 +54,7 @@ public class Binlog implements Serializable {
|
|
|
* @param object 字段原始值
|
|
|
* @return 转换后字段值
|
|
|
*/
|
|
|
- public static Object convert(@NonNull Field field, Object object) {
|
|
|
+ private static Object convert(@NonNull Field field, Object object) {
|
|
|
if (object == null) {
|
|
|
return null;
|
|
|
}
|
|
@@ -63,13 +66,17 @@ public class Binlog implements Serializable {
|
|
|
}
|
|
|
if (ObjectUtils.isMetaClass(type) || type.isEnum() || type == String.class
|
|
|
|| Date.class.isAssignableFrom(type)) {
|
|
|
- return JacksonUtils.convert(object, type);
|
|
|
+ Object value = JacksonUtils.convert(object, type);
|
|
|
+ if (type == String.class && field.isAnnotationPresent(Sensitive.class)) {
|
|
|
+ value = DatabaseContextHolder.getDatabaseCipherHandler().decrypt((String) value, true);
|
|
|
+ }
|
|
|
+ return value;
|
|
|
} else if (object instanceof String && StringUtils.notEmpty(object)) {
|
|
|
// binlog消息不支持JSON格式,所以需要单独反序列化处理
|
|
|
String string = (String) object;
|
|
|
char first = string.charAt(0), last = string.charAt(string.length() - 1);
|
|
|
if ((first == '{' && last == '}') || (first == '[' && last == ']')) {
|
|
|
- return JacksonUtils.deserialize(string, field.getGenericType());
|
|
|
+ return JacksonUtils.deserialize(JsonTypeHandler.MAPPER, string, field.getGenericType());
|
|
|
}
|
|
|
}
|
|
|
return JacksonUtils.convert(object, type);
|
|
@@ -84,32 +91,20 @@ public class Binlog implements Serializable {
|
|
|
* @return 实体对象实例
|
|
|
*/
|
|
|
public static <T> T convert(@NonNull Class<T> model, Map<?, ?> mapping) {
|
|
|
- T entity = ObjectUtils.instance(model);
|
|
|
- update(entity, mapping);
|
|
|
- return entity;
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * 更新实体属性值
|
|
|
- *
|
|
|
- * @param entity 实体对象实例
|
|
|
- * @param mapping 字段名/值映射表
|
|
|
- */
|
|
|
- public static void update(@NonNull Object entity, Map<?, ?> mapping) {
|
|
|
if (ObjectUtils.isEmpty(mapping)) {
|
|
|
- return;
|
|
|
+ return null;
|
|
|
}
|
|
|
|
|
|
- Class<?> model = entity.getClass();
|
|
|
+ T instance = ObjectUtils.instance(model);
|
|
|
Map<String, Field> fields = ObjectUtils.getClassFields(model);
|
|
|
if (ObjectUtils.notEmpty(fields)) {
|
|
|
for (Map.Entry<String, Field> entry : fields.entrySet()) {
|
|
|
String column = StringUtils.hump2underscore(entry.getKey());
|
|
|
- Object value = mapping.get(column);
|
|
|
- value = convert(entry.getValue(), value);
|
|
|
- ObjectUtils.setObjectValue(entity, entry.getValue(), value);
|
|
|
+ Object value = convert(entry.getValue(), mapping.get(column));
|
|
|
+ ObjectUtils.setObjectValue(instance, entry.getValue(), value);
|
|
|
}
|
|
|
}
|
|
|
+ return instance;
|
|
|
}
|
|
|
|
|
|
/**
|
|
@@ -183,16 +178,6 @@ public class Binlog implements Serializable {
|
|
|
return false;
|
|
|
}
|
|
|
|
|
|
- /**
|
|
|
- * 赋值实体对象更新后属性值
|
|
|
- *
|
|
|
- * @param entity 实体对象实例
|
|
|
- * @param <T> 实体类型
|
|
|
- */
|
|
|
- public <T> void after(@NonNull T entity) {
|
|
|
- update(entity, this.after);
|
|
|
- }
|
|
|
-
|
|
|
/**
|
|
|
* 赋值实体对象更新后属性值
|
|
|
*
|
|
@@ -204,16 +189,6 @@ public class Binlog implements Serializable {
|
|
|
return ObjectUtils.isEmpty(this.after) ? null : convert(model, this.after);
|
|
|
}
|
|
|
|
|
|
- /**
|
|
|
- * 赋值实体对象更新前属性值
|
|
|
- *
|
|
|
- * @param entity 实体对象实例
|
|
|
- * @param <T> 实体类型
|
|
|
- */
|
|
|
- public <T> void before(@NonNull T entity) {
|
|
|
- update(entity, this.before);
|
|
|
- }
|
|
|
-
|
|
|
/**
|
|
|
* 赋值实体对象更新前属性值
|
|
|
*
|