|
@@ -17,6 +17,7 @@ package com.baomidou.mybatisplus.extension.handlers;
|
|
|
|
|
|
import com.baomidou.mybatisplus.annotation.EnumValue;
|
|
|
import com.baomidou.mybatisplus.core.toolkit.EnumUtils;
|
|
|
+import com.baomidou.mybatisplus.core.toolkit.ExceptionUtils;
|
|
|
import com.baomidou.mybatisplus.core.toolkit.ReflectionKit;
|
|
|
|
|
|
import org.apache.ibatis.logging.Log;
|
|
@@ -25,6 +26,8 @@ import org.apache.ibatis.type.BaseTypeHandler;
|
|
|
import org.apache.ibatis.type.JdbcType;
|
|
|
|
|
|
import java.lang.reflect.Field;
|
|
|
+import java.lang.reflect.InvocationTargetException;
|
|
|
+import java.lang.reflect.Method;
|
|
|
import java.sql.CallableStatement;
|
|
|
import java.sql.PreparedStatement;
|
|
|
import java.sql.ResultSet;
|
|
@@ -53,26 +56,28 @@ public class EnumAnnotationTypeHandler<E extends Enum<E>> extends BaseTypeHandle
|
|
|
|
|
|
private static final Log LOGGER = LogFactory.getLog(EnumAnnotationTypeHandler.class);
|
|
|
|
|
|
- private static final Map<Class<?>, Field> TABLE_FIELD_OF_ENUM_TYPES = new ConcurrentHashMap<>();
|
|
|
+ private static final Map<Class<?>, Method> TABLE_FIELD_OF_ENUM_TYPES = new ConcurrentHashMap<>();
|
|
|
|
|
|
- public static void addEnumType(Class<?> clazz, Field field) {
|
|
|
- field.setAccessible(true);
|
|
|
- TABLE_FIELD_OF_ENUM_TYPES.put(clazz, field);
|
|
|
+ public static void addEnumType(Class<?> clazz, Method method) {
|
|
|
+ TABLE_FIELD_OF_ENUM_TYPES.put(clazz, method);
|
|
|
}
|
|
|
|
|
|
|
|
|
@Override
|
|
|
public void setNonNullParameter(PreparedStatement ps, int i, Enum parameter, JdbcType jdbcType) throws SQLException {
|
|
|
- Field tableEnumField = getField(type);
|
|
|
+ Method method = getField(type);
|
|
|
try {
|
|
|
+ method.setAccessible(true);
|
|
|
if (jdbcType == null) {
|
|
|
- ps.setObject(i, tableEnumField.get(parameter));
|
|
|
+ ps.setObject(i, method.invoke(parameter));
|
|
|
} else {
|
|
|
// see r3589
|
|
|
- ps.setObject(i, tableEnumField.get(parameter), jdbcType.TYPE_CODE);
|
|
|
+ ps.setObject(i, method.invoke(parameter), jdbcType.TYPE_CODE);
|
|
|
}
|
|
|
} catch (IllegalAccessException e) {
|
|
|
LOGGER.error("unrecognized jdbcType, failed to set StringValue for type=" + parameter);
|
|
|
+ } catch (InvocationTargetException e) {
|
|
|
+ throw ExceptionUtils.mpe("Error: NoSuchMethod in %s. Cause:", e, type.getName());
|
|
|
}
|
|
|
}
|
|
|
|
|
@@ -85,7 +90,7 @@ public class EnumAnnotationTypeHandler<E extends Enum<E>> extends BaseTypeHandle
|
|
|
if (s == null) {
|
|
|
return null;
|
|
|
}
|
|
|
- return EnumUtils.valueOf(type, s, ReflectionKit.getMethod(type, getField(type)));
|
|
|
+ return EnumUtils.valueOf(type, s, getField(type));
|
|
|
}
|
|
|
|
|
|
@Override
|
|
@@ -102,11 +107,12 @@ public class EnumAnnotationTypeHandler<E extends Enum<E>> extends BaseTypeHandle
|
|
|
return clazz.isEnum() ? Arrays.stream(clazz.getDeclaredFields()).filter(field -> field.isAnnotationPresent(EnumValue.class)).findFirst() : Optional.empty();
|
|
|
}
|
|
|
|
|
|
- private Field getField(Class<?> clazz) {
|
|
|
+ private Method getField(Class<?> clazz) {
|
|
|
return Optional.ofNullable(TABLE_FIELD_OF_ENUM_TYPES.get(type)).orElseGet(() -> {
|
|
|
Field field = dealEnumType(clazz).orElseThrow(() -> new IllegalArgumentException("当前[" + type.getName() + "]枚举类未找到标有@EnumValue注解的字段"));
|
|
|
- addEnumType(clazz, field);
|
|
|
- return field;
|
|
|
+ Method method = ReflectionKit.getMethod(clazz, field);
|
|
|
+ addEnumType(clazz, method);
|
|
|
+ return method;
|
|
|
});
|
|
|
}
|
|
|
}
|