Commit d38d4566 authored by yanzg's avatar yanzg

常规BUG的修改

parent 9fad4ad8
......@@ -5,10 +5,7 @@ import com.yanzuoguang.util.exception.CodeException;
import com.yanzuoguang.util.exception.ExceptionHelper;
import com.yanzuoguang.util.helper.StringHelper;
import java.lang.reflect.Array;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.lang.reflect.*;
import java.util.*;
/**
......@@ -206,17 +203,21 @@ public class ObjectHelper {
return;
}
try {
if (item.getField() != null && Modifier.isPublic(item.getField().getModifiers())) {
Class toType = item.getField().getType();
Object toValue = StringHelper.to(toType, value);
item.getField().set(target, toValue);
} else if (item.getSetMethod() != null && Modifier.isPublic(item.getSetMethod().getModifiers())) {
Class toType = item.getSetMethod().getParameterTypes()[0];
Object toValue = StringHelper.to(toType, value);
item.getSetMethod().invoke(target, toValue);
}
setByType(target, item, value);
} catch (Exception ex) {
ExceptionHelper.handleException(ObjectHelper.class, ex, field);
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())) {
Class toType = item.getField().getType();
Object toValue = StringHelper.to(toType, value);
item.getField().set(target, toValue);
} else if (item.getSetMethod() != null && Modifier.isPublic(item.getSetMethod().getModifiers())) {
Class toType = item.getSetMethod().getParameterTypes()[0];
Object toValue = StringHelper.to(toType, value);
item.getSetMethod().invoke(target, toValue);
}
}
......
......@@ -2,10 +2,12 @@ package com.yanzuoguang.db.impl;
import com.yanzuoguang.dao.DaoConst;
import com.yanzuoguang.extend.ConfigDb;
import com.yanzuoguang.util.base.MethodField;
import com.yanzuoguang.util.base.ObjectHelper;
import com.yanzuoguang.util.helper.StringHelper;
import com.yanzuoguang.util.log.Log;
import com.yanzuoguang.util.vo.MapRow;
import jdk.internal.joptsimple.util.KeyValuePair;
import org.springframework.beans.*;
import org.springframework.dao.DataRetrievalFailureException;
import org.springframework.jdbc.core.RowMapper;
......@@ -36,24 +38,14 @@ public class AllBeanRowMapper<T> implements RowMapper<T> {
private Class<T> mappedClass;
/**
* Mappings是否属于字段s
*/
private Map<String, Boolean> mappedIsFields;
/**
* 需要映射的字段
* 是否属于映射方式
*/
private Map<String, Field> mappedFields;
private boolean isMapping = false;
/**
* 需要映射的属性
* 获取字段映射关系
*/
private Map<String, PropertyDescriptor> mappedPropertys;
/**
* 是否属于映射方式
*/
private boolean isMapping = false;
private Map<String, MethodField> typeField;
/**
* 配置信息
......@@ -77,32 +69,16 @@ public class AllBeanRowMapper<T> implements RowMapper<T> {
*/
protected void initialize(Class<T> 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)) {
isMapping = true;
} else {
Field[] fields = mappedClass.getFields();
for (Field item : fields) {
if (Modifier.isStatic(item.getModifiers())) {
continue;
}
String name = item.getName();
String underscoredName = underscoreName(name);
this.mappedFields.put(underscoredName, item);
this.mappedIsFields.put(underscoredName, true);
}
Map<String, MethodField> temp = ObjectHelper.getTypeField(mappedClass);
typeField = new HashMap<>(temp.size());
PropertyDescriptor[] pds = BeanUtils.getPropertyDescriptors(mappedClass);
for (PropertyDescriptor item : pds) {
if (item.getWriteMethod() != null) {
String name = item.getName();
String underscoredName = underscoreName(name);
this.mappedPropertys.put(underscoredName, item);
this.mappedIsFields.put(underscoredName, false);
}
for (Map.Entry<String, MethodField> item : temp.entrySet()) {
String name = item.getKey();
String underscoredName = underscoreName(name);
this.typeField.put(underscoredName, item.getValue());
}
}
}
......@@ -171,8 +147,6 @@ public class AllBeanRowMapper<T> implements RowMapper<T> {
public T mapRow(ResultSet rs, int rowNumber) throws SQLException, TypeMismatchException {
Assert.state(this.mappedClass != null, "Mapped class was not specified");
T mappedObject = BeanUtils.instantiate(this.mappedClass);
BeanWrapper bw = PropertyAccessorFactory.forBeanPropertyAccess(mappedObject);
initBeanWrapper(bw);
ResultSetMetaData rsmd = rs.getMetaData();
int columnCount = rsmd.getColumnCount();
......@@ -186,53 +160,36 @@ public class AllBeanRowMapper<T> implements RowMapper<T> {
value = JdbcUtils.getResultSetValue(rs, index, String.class);
}
((Map) mappedObject).put(getCamelCase(column), value);
} else if (!this.mappedIsFields.containsKey(underscoredName)) {
} else if (!this.typeField.containsKey(underscoredName)) {
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 {
throw e;
}
}
} catch (NotWritablePropertyException ex) {
throw new DataRetrievalFailureException("Unable to map column " + column + " to property " + pd.getName(), ex);
}
} else {
Field pd = this.mappedFields.get(underscoredName);
Class<?> type = pd.getType();
MethodField pd = this.typeField.get(underscoredName);
Class<?> type;
if (pd.getField() != null) {
type = pd.getField().getType();
} else {
type = pd.getGetMethod().getReturnType();
}
Object value = null;
try {
Object value = JdbcUtils.getResultSetValue(rs, index, type);
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 {
pd.set(mappedObject, value);
} catch (IllegalAccessException 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 {
throw e;
}
ObjectHelper.setByType(mappedObject, pd, 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 {
throw e;
}
} catch (IllegalAccessException ex) {
throw new DataRetrievalFailureException("Unable to map column " + column + " to field " + pd.getName(), ex);
} catch (Exception ex) {
throw new DataRetrievalFailureException("Unable to map column " + column + " to property " + pd.getName(), ex);
}
}
}
......@@ -258,17 +215,6 @@ public class AllBeanRowMapper<T> implements RowMapper<T> {
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