Commit d38d4566 authored by yanzg's avatar yanzg

常规BUG的修改

parent 9fad4ad8
...@@ -5,10 +5,7 @@ import com.yanzuoguang.util.exception.CodeException; ...@@ -5,10 +5,7 @@ import com.yanzuoguang.util.exception.CodeException;
import com.yanzuoguang.util.exception.ExceptionHelper; import com.yanzuoguang.util.exception.ExceptionHelper;
import com.yanzuoguang.util.helper.StringHelper; import com.yanzuoguang.util.helper.StringHelper;
import java.lang.reflect.Array; import java.lang.reflect.*;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.util.*; import java.util.*;
/** /**
...@@ -206,6 +203,13 @@ public class ObjectHelper { ...@@ -206,6 +203,13 @@ public class ObjectHelper {
return; return;
} }
try { try {
setByType(target, item, value);
} catch (Exception ex) {
ExceptionHelper.handleException(ObjectHelper.class, ex, item.getName());
}
}
public static void setByType(Object target, MethodField item, Object value) throws IllegalAccessException, InvocationTargetException {
if (item.getField() != null && Modifier.isPublic(item.getField().getModifiers())) { if (item.getField() != null && Modifier.isPublic(item.getField().getModifiers())) {
Class toType = item.getField().getType(); Class toType = item.getField().getType();
Object toValue = StringHelper.to(toType, value); Object toValue = StringHelper.to(toType, value);
...@@ -215,9 +219,6 @@ public class ObjectHelper { ...@@ -215,9 +219,6 @@ public class ObjectHelper {
Object toValue = StringHelper.to(toType, value); Object toValue = StringHelper.to(toType, value);
item.getSetMethod().invoke(target, toValue); item.getSetMethod().invoke(target, toValue);
} }
} catch (Exception ex) {
ExceptionHelper.handleException(ObjectHelper.class, ex, field);
}
} }
// --------------------------------------------- 对象转换、复制、字段复制 --------------------------------------------------------- // --------------------------------------------- 对象转换、复制、字段复制 ---------------------------------------------------------
......
...@@ -2,10 +2,12 @@ package com.yanzuoguang.db.impl; ...@@ -2,10 +2,12 @@ package com.yanzuoguang.db.impl;
import com.yanzuoguang.dao.DaoConst; import com.yanzuoguang.dao.DaoConst;
import com.yanzuoguang.extend.ConfigDb; import com.yanzuoguang.extend.ConfigDb;
import com.yanzuoguang.util.base.MethodField;
import com.yanzuoguang.util.base.ObjectHelper; import com.yanzuoguang.util.base.ObjectHelper;
import com.yanzuoguang.util.helper.StringHelper; import com.yanzuoguang.util.helper.StringHelper;
import com.yanzuoguang.util.log.Log; import com.yanzuoguang.util.log.Log;
import com.yanzuoguang.util.vo.MapRow; import com.yanzuoguang.util.vo.MapRow;
import jdk.internal.joptsimple.util.KeyValuePair;
import org.springframework.beans.*; import org.springframework.beans.*;
import org.springframework.dao.DataRetrievalFailureException; import org.springframework.dao.DataRetrievalFailureException;
import org.springframework.jdbc.core.RowMapper; import org.springframework.jdbc.core.RowMapper;
...@@ -36,24 +38,14 @@ public class AllBeanRowMapper<T> implements RowMapper<T> { ...@@ -36,24 +38,14 @@ public class AllBeanRowMapper<T> implements RowMapper<T> {
private Class<T> mappedClass; private Class<T> mappedClass;
/** /**
* Mappings是否属于字段s * 是否属于映射方式
*/
private Map<String, Boolean> mappedIsFields;
/**
* 需要映射的字段
*/
private Map<String, Field> mappedFields;
/**
* 需要映射的属性
*/ */
private Map<String, PropertyDescriptor> mappedPropertys; private boolean isMapping = false;
/** /**
* 是否属于映射方式 * 获取字段映射关系
*/ */
private boolean isMapping = false; private Map<String, MethodField> typeField;
/** /**
* 配置信息 * 配置信息
...@@ -77,32 +69,16 @@ public class AllBeanRowMapper<T> implements RowMapper<T> { ...@@ -77,32 +69,16 @@ public class AllBeanRowMapper<T> implements RowMapper<T> {
*/ */
protected void initialize(Class<T> mappedClass) { protected void initialize(Class<T> mappedClass) {
this.mappedClass = mappedClass; this.mappedClass = mappedClass;
this.mappedFields = new HashMap<>(DaoConst.COLLECTION_INIT_SIZE);
this.mappedPropertys = new HashMap<>(DaoConst.COLLECTION_INIT_SIZE);
this.mappedIsFields = new HashMap<>(DaoConst.COLLECTION_INIT_SIZE);
if (ObjectHelper.isSub(MapRow.class, mappedClass) || ObjectHelper.isSub(Map.class, mappedClass)) { if (ObjectHelper.isSub(MapRow.class, mappedClass) || ObjectHelper.isSub(Map.class, mappedClass)) {
isMapping = true; isMapping = true;
} else { } else {
Field[] fields = mappedClass.getFields(); Map<String, MethodField> temp = ObjectHelper.getTypeField(mappedClass);
for (Field item : fields) { typeField = new HashMap<>(temp.size());
if (Modifier.isStatic(item.getModifiers())) {
continue;
}
String name = item.getName();
String underscoredName = underscoreName(name);
this.mappedFields.put(underscoredName, item);
this.mappedIsFields.put(underscoredName, true);
}
PropertyDescriptor[] pds = BeanUtils.getPropertyDescriptors(mappedClass); for (Map.Entry<String, MethodField> item : temp.entrySet()) {
for (PropertyDescriptor item : pds) { String name = item.getKey();
if (item.getWriteMethod() != null) {
String name = item.getName();
String underscoredName = underscoreName(name); String underscoredName = underscoreName(name);
this.mappedPropertys.put(underscoredName, item); this.typeField.put(underscoredName, item.getValue());
this.mappedIsFields.put(underscoredName, false);
}
} }
} }
} }
...@@ -171,8 +147,6 @@ public class AllBeanRowMapper<T> implements RowMapper<T> { ...@@ -171,8 +147,6 @@ public class AllBeanRowMapper<T> implements RowMapper<T> {
public T mapRow(ResultSet rs, int rowNumber) throws SQLException, TypeMismatchException { public T mapRow(ResultSet rs, int rowNumber) throws SQLException, TypeMismatchException {
Assert.state(this.mappedClass != null, "Mapped class was not specified"); Assert.state(this.mappedClass != null, "Mapped class was not specified");
T mappedObject = BeanUtils.instantiate(this.mappedClass); T mappedObject = BeanUtils.instantiate(this.mappedClass);
BeanWrapper bw = PropertyAccessorFactory.forBeanPropertyAccess(mappedObject);
initBeanWrapper(bw);
ResultSetMetaData rsmd = rs.getMetaData(); ResultSetMetaData rsmd = rs.getMetaData();
int columnCount = rsmd.getColumnCount(); int columnCount = rsmd.getColumnCount();
...@@ -186,42 +160,26 @@ public class AllBeanRowMapper<T> implements RowMapper<T> { ...@@ -186,42 +160,26 @@ public class AllBeanRowMapper<T> implements RowMapper<T> {
value = JdbcUtils.getResultSetValue(rs, index, String.class); value = JdbcUtils.getResultSetValue(rs, index, String.class);
} }
((Map) mappedObject).put(getCamelCase(column), value); ((Map) mappedObject).put(getCamelCase(column), value);
} else if (!this.mappedIsFields.containsKey(underscoredName)) { } else if (!this.typeField.containsKey(underscoredName)) {
continue; continue;
} else if (!this.mappedIsFields.get(underscoredName)) {
PropertyDescriptor pd = this.mappedPropertys.get(underscoredName);
Class<?> type = pd.getPropertyType();
try {
Object value = JdbcUtils.getResultSetValue(rs, index, type);
if (configDb.isPrintMapper() && rowNumber == 0) {
Log.info(AllBeanRowMapper.class, "Mapping column '%s' to property '%s' of type %s", column, pd.getName(), type);
}
try {
bw.setPropertyValue(pd.getName(), value);
} catch (TypeMismatchException e) {
if (value == null) {
Log.info(AllBeanRowMapper.class,
"Intercepted TypeMismatchException for row %d and column '%s' with " +
"value %s when setting property '%s' of type %s on object: %s",
rowNumber, column, StringHelper.EMPTY, pd.getName(), type, mappedObject);
} else { } else {
throw e; MethodField pd = this.typeField.get(underscoredName);
} Class<?> type;
}
} catch (NotWritablePropertyException ex) { if (pd.getField() != null) {
throw new DataRetrievalFailureException("Unable to map column " + column + " to property " + pd.getName(), ex); type = pd.getField().getType();
}
} else { } else {
Field pd = this.mappedFields.get(underscoredName); type = pd.getGetMethod().getReturnType();
Class<?> type = pd.getType(); }
Object value = null;
try { try {
Object value = JdbcUtils.getResultSetValue(rs, index, type); value = JdbcUtils.getResultSetValue(rs, index, type);
if (configDb.isPrintMapper() && rowNumber == 0) { if (configDb.isPrintMapper() && rowNumber == 0) {
Log.info(AllBeanRowMapper.class, "Mapping column '%s' to property '%s' of type %s", column, pd.getName(), type); Log.info(AllBeanRowMapper.class, "Mapping column '%s' to property '%s' of type %s", column, pd.getName(), type);
} }
try { ObjectHelper.setByType(mappedObject, pd, value);
pd.set(mappedObject, value); } catch (TypeMismatchException e) {
} catch (IllegalAccessException e) {
if (value == null) { if (value == null) {
Log.info(AllBeanRowMapper.class, Log.info(AllBeanRowMapper.class,
"Intercepted TypeMismatchException for row %d and column '%s' with " + "Intercepted TypeMismatchException for row %d and column '%s' with " +
...@@ -230,9 +188,8 @@ public class AllBeanRowMapper<T> implements RowMapper<T> { ...@@ -230,9 +188,8 @@ public class AllBeanRowMapper<T> implements RowMapper<T> {
} else { } else {
throw e; throw e;
} }
} } catch (Exception ex) {
} catch (IllegalAccessException ex) { throw new DataRetrievalFailureException("Unable to map column " + column + " to property " + pd.getName(), ex);
throw new DataRetrievalFailureException("Unable to map column " + column + " to field " + pd.getName(), ex);
} }
} }
} }
...@@ -258,17 +215,6 @@ public class AllBeanRowMapper<T> implements RowMapper<T> { ...@@ -258,17 +215,6 @@ public class AllBeanRowMapper<T> implements RowMapper<T> {
return column; return column;
} }
/**
* Initialize the given BeanWrapper to be used for row mapping.
* To be called for each row.
* <p>The default implementation is empty. Can be overridden in subclasses.
*
* @param bw the BeanWrapper to initialize
*/
protected void initBeanWrapper(BeanWrapper bw) {
}
/** /**
* 缓存的处理类 * 缓存的处理类
*/ */
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment