|
@@ -237,25 +237,31 @@ public class TypeHandlerConfigurer extends MybatisConfigurer {
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
- * 绑定字段类型处理器
|
|
|
+ * 绑定字段类型处理器,成功绑定返回true,否则返回false
|
|
|
*
|
|
|
* @param clazz 对象类型
|
|
|
* @param field 表字段信息
|
|
|
+ * @return true/false
|
|
|
*/
|
|
|
@SuppressWarnings("unchecked")
|
|
|
- private void bindTypeHandler(Class<?> clazz, TableFieldInfo field) {
|
|
|
+ private boolean bindTypeHandler(Class<?> clazz, TableFieldInfo field) {
|
|
|
+ // 查找字段类型处理器
|
|
|
Class<? extends TypeHandler<?>> handler =
|
|
|
this.lookupTypeHandlerClass(clazz, field.getField(), field.getTypeHandler());
|
|
|
- if (handler != null) {
|
|
|
- ObjectUtils.setValue(field, "typeHandler", handler);
|
|
|
- String el = ObjectUtils.getValue(field, "el") + ",typeHandler=" + handler.getName();
|
|
|
- ObjectUtils.setValue(field, "el", el);
|
|
|
+ if (handler == null) {
|
|
|
+ return false;
|
|
|
+ }
|
|
|
|
|
|
- // 注册类型TypeHandler实例
|
|
|
+ // 绑定并注册字段类型处理器
|
|
|
+ ObjectUtils.setValue(field, "typeHandler", handler);
|
|
|
+ String el = ObjectUtils.getValue(field, "el") + ",typeHandler=" + handler.getName();
|
|
|
+ ObjectUtils.setValue(field, "el", el);
|
|
|
+ if (!this.isTypeHandlerRegistered(field.getField().getType())) {
|
|
|
this.configuration.getTypeHandlerRegistry().register(
|
|
|
(Class<Object>) field.getField().getType(), DatabaseContextHolder.initializeTypeHandler(handler)
|
|
|
);
|
|
|
}
|
|
|
+ return true;
|
|
|
}
|
|
|
|
|
|
/**
|
|
@@ -506,7 +512,6 @@ public class TypeHandlerConfigurer extends MybatisConfigurer {
|
|
|
* @param field 对象字段
|
|
|
* @param consumer resultMap元素回调函数
|
|
|
*/
|
|
|
- @SuppressWarnings("unchecked")
|
|
|
private void initializeResultMapping(Document document, Class<?> clazz, Field field, Consumer<Element> consumer) {
|
|
|
// 排除主键字段
|
|
|
if (field.isAnnotationPresent(TableId.class)) {
|
|
@@ -543,11 +548,6 @@ public class TypeHandlerConfigurer extends MybatisConfigurer {
|
|
|
child.setAttribute("typeHandler", handler.getName());
|
|
|
consumer.accept(child);
|
|
|
});
|
|
|
-
|
|
|
- // 注册类型TypeHandler实例
|
|
|
- this.configuration.getTypeHandlerRegistry().register(
|
|
|
- (Class<Object>) field.getType(), DatabaseContextHolder.initializeTypeHandler(handler)
|
|
|
- );
|
|
|
}
|
|
|
|
|
|
@Override
|
|
@@ -566,6 +566,9 @@ public class TypeHandlerConfigurer extends MybatisConfigurer {
|
|
|
for (XNode child : map.getChildren()) {
|
|
|
String property = child.getStringAttribute("property");
|
|
|
properties.add(property);
|
|
|
+ if ("id".equalsIgnoreCase(child.getName())) {
|
|
|
+ continue;
|
|
|
+ }
|
|
|
Field field = ObjectUtils.findField(clazz, property);
|
|
|
if (field == null || field.isAnnotationPresent(TableId.class)) {
|
|
|
continue;
|
|
@@ -576,11 +579,6 @@ public class TypeHandlerConfigurer extends MybatisConfigurer {
|
|
|
(Class<? extends TypeHandler<?>>) this.name2class(child.getStringAttribute("typeHandler"));
|
|
|
if ((handler = this.lookupTypeHandlerClass(clazz, field, handler)) != null) {
|
|
|
((Element) child.getNode()).setAttribute("typeHandler", handler.getName());
|
|
|
-
|
|
|
- // 注册类型TypeHandler实例
|
|
|
- this.configuration.getTypeHandlerRegistry().register(
|
|
|
- (Class<Object>) field.getType(), DatabaseContextHolder.initializeTypeHandler(handler)
|
|
|
- );
|
|
|
}
|
|
|
if (StringUtils.isEmpty(child.getStringAttribute("typeHandler"))) {
|
|
|
continue;
|
|
@@ -638,7 +636,13 @@ public class TypeHandlerConfigurer extends MybatisConfigurer {
|
|
|
// 自动绑定字段类型处理器
|
|
|
List<TableFieldInfo> fields = table.getFieldList();
|
|
|
if (ObjectUtils.notEmpty(fields)) {
|
|
|
- fields.forEach(field -> this.bindTypeHandler(table.getEntityType(), field));
|
|
|
+ boolean bound = false;
|
|
|
+ for (TableFieldInfo field : fields) {
|
|
|
+ bound |= this.bindTypeHandler(table.getEntityType(), field);
|
|
|
+ }
|
|
|
+ if (bound && !table.isAutoInitResultMap()) {
|
|
|
+ ObjectUtils.setValue(table, "autoInitResultMap", true);
|
|
|
+ }
|
|
|
}
|
|
|
}
|
|
|
}
|