• CommonUtils


    package com.utils;
    
    import java.lang.reflect.Field;
    import java.math.BigDecimal;
    import java.sql.Timestamp;
    import java.text.SimpleDateFormat;
    import java.util.Date;
    import java.util.Map;
    import java.util.Set;
    
    public class CommonUtils {
        /**
         * 将map中的值赋值到po中
         * 
         * @param map
         *            前台传过来的值封装成的map对象
         * @param obj
         *            po实体类
         */
        public static void getLatestObjectByMap(Map<String, ?> map, Object obj) {
            Set<String> keys = map.keySet();
            for (String key : keys) {
                Object value = map.get(key);
                Class<? extends Object> clazz = obj.getClass();
                try {
                    Field field = clazz.getDeclaredField(key);
                    field.setAccessible(true);
                    field.set(obj, getValue(key, field.getType().getName(), value));
                } catch (NoSuchFieldException e) {
                    e.printStackTrace();
                } catch (IllegalArgumentException e) {
                    e.printStackTrace();
                } catch (IllegalAccessException e) {
                    e.printStackTrace();
                }
            }
        }
    
        /**
         * 将fieldValue转为dataType类型的值
         * 
         * @param key
         * @param dataType
         * @param fieldValue
         * @return
         */
        private static Object getValue(String key, String dataType,
                Object fieldValue) {
            String errorMessage = key + "为[" + dataType + "]而设置的值为["
                    + fieldValue.getClass().getName() + "]";
            String numberFormatErrorMessage = key + "  [" + fieldValue + "]转换为["
                    + dataType + "]出错";
            System.out.println(dataType);
            if ("java.sql.Date".equals(dataType)) {
                if (fieldValue instanceof java.sql.Date) {
                    return fieldValue;
                } else if (fieldValue instanceof java.lang.String) {
                    String formatString = "yyyy-MM-dd";
                    try {
                        SimpleDateFormat sdf = new SimpleDateFormat(formatString);
                        Date date = sdf.parse((String) fieldValue);
                        return new java.sql.Date(date.getTime());
                    } catch (Exception e) {
                        throw new RuntimeException(key + "[" + fieldValue
                                + "]格式错误,正确格式为[" + formatString + "]");
                    }
                } else {
                    throw new RuntimeException(errorMessage);
                }
            } else if ("java.util.Date".equals(dataType)) {
                if (fieldValue instanceof java.util.Date) {
                    return fieldValue;
                } else if (fieldValue instanceof java.lang.String) {
                    String formatString = "yyyy-MM-dd";
                    try {
                        SimpleDateFormat sdf = new SimpleDateFormat(formatString);
                        java.util.Date date = sdf.parse((String) fieldValue);
                        return date;
                    } catch (Exception e) {
                        throw new RuntimeException(key + "[" + fieldValue
                                + "]格式错误,正确格式为[" + formatString + "]");
                    }
                } else {
                    throw new RuntimeException(errorMessage);
                }
            } else if ("java.sql.Timestamp".equals(dataType)) {
                if (fieldValue instanceof java.sql.Timestamp) {
                    return fieldValue;
                } else if (fieldValue instanceof java.lang.String) {
                    String formatString = "yyyy-MM-dd HH:mm:ss";
                    try {
                        SimpleDateFormat sdf = new SimpleDateFormat(formatString);
                        Date date = sdf.parse((String) fieldValue);
                        return new Timestamp(date.getTime());
                    } catch (Exception e) {
                        throw new RuntimeException(key + "[" + fieldValue
                                + "]格式错误,正确格式为[" + formatString + "]");
                    }
                } else {
                    throw new RuntimeException(errorMessage);
                }
            } else if ("java.lang.String".equals(dataType)) {
                if (fieldValue instanceof java.lang.String) {
                    return fieldValue;
                } else {
                    throw new RuntimeException(errorMessage);
                }
            } else if ("java.math.BigDecimal".equals(dataType)) {
                if (fieldValue instanceof java.math.BigDecimal) {
                    return fieldValue;
                } else if (fieldValue instanceof java.lang.String) {
                    try {
                        return new BigDecimal(String.valueOf(fieldValue));
                    } catch (Exception e) {
                        throw new RuntimeException(numberFormatErrorMessage);
                    }
                } else {
                    throw new RuntimeException(errorMessage);
                }
            } else if (("java.lang.Integer".equals(dataType))
                    || ("int".equals(dataType))) {
                if (fieldValue instanceof java.lang.Integer) {
                    return fieldValue;
                } else if (fieldValue instanceof java.lang.String) {
                    try {
                        return new Integer(String.valueOf(fieldValue));
                    } catch (Exception e) {
                        throw new RuntimeException(numberFormatErrorMessage);
                    }
                } else {
                    throw new RuntimeException(errorMessage);
                }
            } else if (("java.lang.Long".equals(dataType))
                    || ("long".equals(dataType))) {
                if (fieldValue instanceof java.lang.Long) {
                    return fieldValue;
                } else if (fieldValue instanceof java.lang.String) {
                    try {
                        return new Long(String.valueOf(fieldValue));
                    } catch (Exception e) {
                        throw new RuntimeException(numberFormatErrorMessage);
                    }
                } else {
                    throw new RuntimeException(errorMessage);
                }
    
            } else if (("java.lang.Short".equals(dataType))
                    || ("short".equals(dataType))) {
                if (fieldValue instanceof java.lang.Short) {
                    return fieldValue;
                } else if (fieldValue instanceof java.lang.String) {
                    try {
                        return new Short(String.valueOf(fieldValue));
                    } catch (Exception e) {
                        throw new RuntimeException(numberFormatErrorMessage);
                    }
                } else {
                    throw new RuntimeException(errorMessage);
                }
            } else if (("java.lang.Byte".equals(dataType))
                    || ("byte".equals(dataType))) {
                if (fieldValue instanceof java.lang.Byte) {
                    return fieldValue;
                } else if (fieldValue instanceof java.lang.String) {
                    try {
                        return new Byte(String.valueOf(fieldValue));
                    } catch (Exception e) {
                        throw new RuntimeException(numberFormatErrorMessage);
                    }
                } else {
                    throw new RuntimeException(errorMessage);
                }
            } else if (("java.lang.Float".equals(dataType))
                    || ("float".equals(dataType))) {
                if (fieldValue instanceof java.lang.Float) {
                    return fieldValue;
                } else if (fieldValue instanceof java.lang.String) {
                    try {
                        return new Float(String.valueOf(fieldValue));
                    } catch (Exception e) {
                        throw new RuntimeException(numberFormatErrorMessage);
                    }
                } else {
                    throw new RuntimeException(errorMessage);
                }
            } else if (("java.lang.Double".equals(dataType))
                    || ("double".equals(dataType))) {
                if (fieldValue instanceof java.lang.Double) {
                    return fieldValue;
                } else if (fieldValue instanceof java.lang.String) {
                    try {
                        return new Double(String.valueOf(fieldValue));
                    } catch (Exception e) {
                        throw new RuntimeException(numberFormatErrorMessage);
                    }
                } else {
                    throw new RuntimeException(errorMessage);
                }
            } else if (("java.lang.Boolean".equals(dataType))
                    || ("boolean".equals(dataType))) {
                if (fieldValue instanceof java.lang.Boolean) {
                    return fieldValue;
                } else if (fieldValue instanceof java.lang.String) {
                    if ("true".equals(fieldValue)) {
                        return new Boolean(true);
                    } else if ("false".equals(fieldValue)) {
                        return new Boolean(false);
                    } else {
                        throw new RuntimeException(numberFormatErrorMessage);
                    }
                } else {
                    throw new RuntimeException(errorMessage);
                }
            } else if (("java.lang.Character".equals(dataType))
                    || ("char".equals(dataType))) {
                {
                    return new Character(String.valueOf(fieldValue).charAt(0));
                }
            }
            return fieldValue;
        }
    }
  • 相关阅读:
    Asp.Net Core 轻松学-被低估的过滤器
    Asp.Net Core 轻松学-利用文件监视进行快速测试开发
    Asp.Net Core 轻松学-利用xUnit进行主机级别的网络集成测试
    Asp.Net Core 轻松学-HttpClient的演进和避坑
    Asp.Net Core 轻松学-基于微服务的后台任务调度管理器
    Asp.Net Core 轻松学-一行代码搞定文件上传
    .NET Core 2.2 新增部分功能使用尝鲜
    Asp.NetCore轻松学-业务重点-实现一个简单的手机号码验证
    Asp.Net Core 轻松学-实现跨平台的自定义Json数据包
    Asp.Net Core 轻松学-利用 Swagger 自动生成接口文档
  • 原文地址:https://www.cnblogs.com/ahjx1628/p/4170114.html
Copyright © 2020-2023  润新知