diff --git a/diboot-core/src/main/java/com/diboot/core/binding/binder/BaseBinder.java b/diboot-core/src/main/java/com/diboot/core/binding/binder/BaseBinder.java index 71397ea..48b96e7 100644 --- a/diboot-core/src/main/java/com/diboot/core/binding/binder/BaseBinder.java +++ b/diboot-core/src/main/java/com/diboot/core/binding/binder/BaseBinder.java @@ -5,15 +5,18 @@ import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; import com.baomidou.mybatisplus.extension.service.IService; import com.diboot.core.binding.parser.MiddleTable; import com.diboot.core.config.BaseConfig; -import com.diboot.core.exception.BusinessException; import com.diboot.core.service.BaseService; -import com.diboot.core.util.*; +import com.diboot.core.util.BeanUtils; +import com.diboot.core.util.IGetter; +import com.diboot.core.util.S; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import java.lang.reflect.Field; -import java.math.BigDecimal; -import java.util.*; +import java.util.Collection; +import java.util.Collections; +import java.util.List; +import java.util.Map; /** * 关系绑定Binder父类 @@ -221,34 +224,9 @@ public abstract class BaseBinder { if(this.referencedEntityClass != null){ Field field = BeanUtils.extractField(this.referencedEntityClass, S.toLowerCaseCamel(fieldName)); if(field != null){ - String valueStr = S.valueOf(value); - String type = field.getGenericType().getTypeName(); - if(Integer.class.getName().equals(type)){ - return Integer.parseInt(valueStr); - } - else if(Long.class.getName().equals(type)){ - return Long.parseLong(valueStr); - } - else if(Double.class.getName().equals(type)){ - return Double.parseDouble(valueStr); - } - else if(BigDecimal.class.getName().equals(type)){ - return new BigDecimal(valueStr); - } - else if(Float.class.getName().equals(type)){ - return Float.parseFloat(valueStr); - } - else if(Boolean.class.getName().equals(type)){ - return V.isTrue(valueStr); - } - else if(type.contains(Date.class.getSimpleName())){ - return D.fuzzyConvert(valueStr); - } + return BeanUtils.convertValueToFieldType(value, field); } } - else{ - throw new BusinessException("dddd"); - } return value; } diff --git a/diboot-core/src/main/java/com/diboot/core/service/impl/BaseServiceImpl.java b/diboot-core/src/main/java/com/diboot/core/service/impl/BaseServiceImpl.java index 279d30d..9d837d5 100644 --- a/diboot-core/src/main/java/com/diboot/core/service/impl/BaseServiceImpl.java +++ b/diboot-core/src/main/java/com/diboot/core/service/impl/BaseServiceImpl.java @@ -365,10 +365,22 @@ public class BaseServiceImpl, T> extends ServiceImpl String[] keyValueArray = sqlSelect.split(Cons.SEPARATOR_COMMA); List keyValueList = new ArrayList<>(mapList.size()); for(Map map : mapList){ - if(map.get(keyValueArray[0]) != null){ - KeyValue kv = new KeyValue(S.valueOf(map.get(keyValueArray[0])), map.get(keyValueArray[1])); + String key = keyValueArray[0], value = keyValueArray[1], ext = null; + // 兼容oracle大写 + if(map.containsKey(key) == false && map.containsKey(key.toUpperCase())){ + key = key.toUpperCase(); + } + if(map.containsKey(value) == false && map.containsKey(value.toUpperCase())){ + value = value.toUpperCase(); + } + if(map.containsKey(key)){ + KeyValue kv = new KeyValue(S.valueOf(map.get(key)), map.get(value)); if(keyValueArray.length > 2){ - kv.setExt(map.get(keyValueArray[2])); + ext = keyValueArray[2]; + if(map.containsKey(ext) == false && map.containsKey(ext.toUpperCase())){ + ext = ext.toUpperCase(); + } + kv.setExt(map.get(ext)); } keyValueList.add(kv); } diff --git a/diboot-core/src/main/java/com/diboot/core/util/BeanUtils.java b/diboot-core/src/main/java/com/diboot/core/util/BeanUtils.java index c99756e..cfeb29f 100644 --- a/diboot-core/src/main/java/com/diboot/core/util/BeanUtils.java +++ b/diboot-core/src/main/java/com/diboot/core/util/BeanUtils.java @@ -14,13 +14,11 @@ import org.springframework.beans.PropertyAccessorFactory; import org.springframework.core.ResolvableType; import org.springframework.util.ReflectionUtils; -import java.beans.BeanInfo; -import java.beans.Introspector; -import java.beans.PropertyDescriptor; import java.io.Serializable; import java.lang.invoke.SerializedLambda; import java.lang.reflect.Field; import java.lang.reflect.Method; +import java.math.BigDecimal; import java.util.*; import java.util.concurrent.ConcurrentHashMap; import java.util.function.Function; @@ -117,74 +115,22 @@ public class BeanUtils { * @param propMap */ public static void bindProperties(Object model, Map propMap){ - try{// 获取类属性 - BeanInfo beanInfo = Introspector.getBeanInfo(model.getClass()); - // 给 JavaBean 对象的属性赋值 - PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors(); - for (PropertyDescriptor descriptor : propertyDescriptors) { - String propertyName = descriptor.getName(); - if (!propMap.containsKey(propertyName)){ - continue; - } - Object value = propMap.get(propertyName); - Class type = descriptor.getWriteMethod().getParameterTypes()[0]; - Object[] args = new Object[1]; - String fieldType = type.getName(); - // 类型不一致,需转型 - if(value != null && !value.getClass().getTypeName().equals(fieldType)){ - if(value instanceof String){ - // String to Date - if(fieldType.equals(Date.class.getName())){ - args[0] = D.fuzzyConvert((String)value); - } - // Map中的String型转换为其他型 - else if(fieldType.equals(Boolean.class.getName())){ - args[0] = V.isTrue((String)value); - } - else if (fieldType.equals(Integer.class.getName()) || "int".equals(fieldType)) { - args[0] = Integer.parseInt((String)value); - } - else if (fieldType.equals(Long.class.getName())) { - args[0] = Long.parseLong((String)value); - } - else if (fieldType.equals(Double.class.getName())) { - args[0] = Double.parseDouble((String)value); - } - else if (fieldType.equals(Float.class.getName())) { - args[0] = Float.parseFloat((String)value); - } - else{ - args[0] = value; - log.warn("类型不一致,暂无法自动绑定,请手动转型一致后调用!字段类型: {} vs {} ", value.getClass().getTypeName(), fieldType); - } - } - // boolean vs Boolean - else if(value.getClass().getTypeName().equalsIgnoreCase(Boolean.class.getName()) && fieldType.equalsIgnoreCase(Boolean.class.getName())){ - //args[0] = (Boolean).valueOf(); - } - // Integer 向上转型为 Long 绑定 - else if(value.getClass().getTypeName().equals(Integer.class.getName()) && fieldType.equals(Long.class.getName())){ - Integer intValue = (Integer)value; - args[0] = intValue.longValue(); - } - // Float 向上转型为 Double 绑定 - else if(value.getClass().getTypeName().equals(Float.class.getName()) && fieldType.equals(Double.class.getName())){ - Float floatValue = (Float)value; - args[0] = floatValue.doubleValue(); - } - else{ - args[0] = value; - log.warn("类型不一致,暂无法自动绑定,请手动转型一致后调用! {} vs {} ", value.getClass().getTypeName(), fieldType); - } - } - else{ - args[0] = value; - } - descriptor.getWriteMethod().invoke(model, args); - } + if(V.isEmpty(propMap)){ + return; } - catch (Exception e){ - log.warn("复制Map属性到Model异常: " + e.getMessage(), e); + List fields = extractAllFields(model.getClass()); + Map fieldNameMaps = convertToStringKeyObjectMap(fields, "name"); + for(Map.Entry entry : propMap.entrySet()){ + Field field = fieldNameMaps.get(entry.getKey()); + if(field != null){ + try{ + Object value = convertValueToFieldType(entry.getValue(), field); + setProperty(model, entry.getKey(), value); + } + catch (Exception e){ + log.warn("复制属性{}.{}异常: {}", model.getClass().getSimpleName(), entry.getKey(), e.getMessage()); + } + } } } @@ -224,6 +170,41 @@ public class BeanUtils { wrapper.setPropertyValue(field, value); } + /** + * 转换为field对应的类型 + * @param value + * @param field + * @return + */ + public static Object convertValueToFieldType(Object value, Field field){ + String type = field.getGenericType().getTypeName(); + if(value.getClass().getName().equals(type)){ + return value; + } + if(Integer.class.getName().equals(type)){ + return Integer.parseInt(S.valueOf(value)); + } + else if(Long.class.getName().equals(type)){ + return Long.parseLong(S.valueOf(value)); + } + else if(Double.class.getName().equals(type)){ + return Double.parseDouble(S.valueOf(value)); + } + else if(BigDecimal.class.getName().equals(type)){ + return new BigDecimal(S.valueOf(value)); + } + else if(Float.class.getName().equals(type)){ + return Float.parseFloat(S.valueOf(value)); + } + else if(Boolean.class.getName().equals(type)){ + return V.isTrue(S.valueOf(value)); + } + else if(type.contains(Date.class.getSimpleName())){ + return D.fuzzyConvert(S.valueOf(value)); + } + return value; + } + /*** * Key-Object对象Map * @param allLists diff --git a/diboot-core/src/main/java/com/diboot/core/util/ContextHelper.java b/diboot-core/src/main/java/com/diboot/core/util/ContextHelper.java index 7947589..e531f03 100644 --- a/diboot-core/src/main/java/com/diboot/core/util/ContextHelper.java +++ b/diboot-core/src/main/java/com/diboot/core/util/ContextHelper.java @@ -3,11 +3,9 @@ package com.diboot.core.util; import com.baomidou.mybatisplus.annotation.DbType; import com.baomidou.mybatisplus.annotation.TableField; import com.baomidou.mybatisplus.annotation.TableId; -import com.baomidou.mybatisplus.annotation.TableName; import com.baomidou.mybatisplus.extension.service.IService; import com.baomidou.mybatisplus.extension.toolkit.JdbcUtils; import com.diboot.core.config.Cons; -import com.diboot.core.entity.BaseEntity; import com.diboot.core.service.BaseService; import org.apache.ibatis.session.SqlSessionFactory; import org.slf4j.Logger; diff --git a/diboot-core/src/main/java/com/diboot/core/util/SqlExecutor.java b/diboot-core/src/main/java/com/diboot/core/util/SqlExecutor.java index 40d2cb9..a600aaa 100644 --- a/diboot-core/src/main/java/com/diboot/core/util/SqlExecutor.java +++ b/diboot-core/src/main/java/com/diboot/core/util/SqlExecutor.java @@ -119,9 +119,15 @@ public class SqlExecutor { Map resultMap = new HashMap<>(); if(V.notEmpty(resultSetMapList)){ for(Map row : resultSetMapList){ - String key = String.valueOf(row.get(keyName)); - Object value = row.get(valueName); - resultMap.put(key, value); + Object keyObj = row.get(keyName); + if(keyObj == null && row.get(keyName.toUpperCase()) != null){ + keyObj = row.get(keyName.toUpperCase()); + } + Object valueObj = row.get(valueName); + if(valueObj == null && row.get(valueName.toUpperCase()) != null){ + valueObj = row.get(valueName.toUpperCase()); + } + resultMap.put(S.valueOf(keyObj), valueObj); } } return resultMap; @@ -145,13 +151,21 @@ public class SqlExecutor { Map resultMap = new HashMap<>(); if(V.notEmpty(resultSetMapList)){ for(Map row : resultSetMapList){ - String key = String.valueOf(row.get(keyName)); + Object keyObj = row.get(keyName); + if(keyObj == null && row.get(keyName.toUpperCase()) != null){ + keyObj = row.get(keyName.toUpperCase()); + } + String key = S.valueOf(keyObj); List valueList = resultMap.get(key); if(valueList == null){ valueList = new ArrayList(); resultMap.put(key, valueList); } - valueList.add(row.get(valueName)); + Object valueObj = row.get(valueName); + if(valueObj == null && row.get(valueName.toUpperCase()) != null){ + valueObj = row.get(valueName.toUpperCase()); + } + valueList.add(valueObj); } } return resultMap; diff --git a/diboot-core/src/test/java/diboot/core/test/service/BaseServiceTest.java b/diboot-core/src/test/java/diboot/core/test/service/BaseServiceTest.java index ed4b1e9..f6b7ff5 100644 --- a/diboot-core/src/test/java/diboot/core/test/service/BaseServiceTest.java +++ b/diboot-core/src/test/java/diboot/core/test/service/BaseServiceTest.java @@ -240,6 +240,6 @@ public class BaseServiceTest { public void testContextHelper(){ String database = ContextHelper.getDatabaseType(); System.out.println(database); - Assert.assertTrue(database.equals("mysql")); + Assert.assertTrue(database.equals("mysql") || database.equals("oracle")); } } \ No newline at end of file diff --git a/diboot-core/src/test/java/diboot/core/test/util/BeanUtilsTest.java b/diboot-core/src/test/java/diboot/core/test/util/BeanUtilsTest.java index c8368f0..ca53bb1 100644 --- a/diboot-core/src/test/java/diboot/core/test/util/BeanUtilsTest.java +++ b/diboot-core/src/test/java/diboot/core/test/util/BeanUtilsTest.java @@ -35,9 +35,13 @@ public class BeanUtilsTest { Map map = new HashMap<>(); map.put("type", "STATUS"); map.put("itemName",itemName); + map.put("editable", true); + map.put("createTime", "2018-09-12 23:09"); Dictionary dictionary3 = new Dictionary(); BeanUtils.bindProperties(dictionary3, map); - Assert.assertTrue(dictionary2.getItemName().equals(itemName)); + Assert.assertTrue(dictionary3.getItemName().equals(itemName)); + Assert.assertTrue(dictionary3.isEditable() == true); + Assert.assertTrue(dictionary3.getCreateTime() != null); } @Test