zoukankan      html  css  js  c++  java
  • 映射篇:request-String-Object-Map之间相互转化(程序员的成长之路---第5篇)

    为什么写这一篇

    问题一:jdbc连接数据库返回的对象是ResultSet,如何把ResultSet对象中的值转换为我们自建的各种实体类?

    我估计,80%的程序员会写jdbc数据库连接,但开发项目依然用spring、hibernate、 mybatis等开源框架封装的数据库连接。

    在你能写一套足够好的数据库连接池进行数据库增删改查的时候,我并不认为开源框架会比你写的数据库连接效率快多少。那大部分人为什么没有用自己写的数据库连接池?

    “拿来主义”,确实让我们的工作轻松了很多。

    当我们被简单的“拿来主义”充满头脑的时候,一旦出现开源框架的数据库连接不能满足我们的项目需求的时候,谁来负责底层框架的修改?

    ...其他的不多说了,总之每个人都有不同的推脱理由。

    jdbc连接数据库返回的对象是ResultSet,如何能把ResultSet对象中的值转换为我们自建的各种实体类的值呢?

    问题二:

    User user = new User();
    user.setName("张三");
    user.setAge(20);
    user.setAdress("中国山东济南");
    System.out.println(user);
    以上代码,
    希望打印出的結果:name=张三,age=20,adress=中国山东济南
    实际打印出的結果:com.keji10.core.commons.User@7852e922

      总之说了这么多。只是为了体现映射在我们日常敲代码的过程中的重要性 --<-<-<@

     类方法:

    类代码:

    package com.keji10.core.commons;
    
    import java.lang.reflect.Field;
    import java.lang.reflect.Method;
    import java.util.Enumeration;
    import java.util.HashMap;
    import java.util.List;
    import java.util.Map;
    
    import javax.servlet.http.HttpServletRequest;
    
    import com.keji10.core.exception.RefertException;
    
    /**
     * 类描述:
     * 
     * @since 1.5
     * @version 1.0
     * @author xuanly
     * 
     */
    public abstract class BeanHelper {
    
        /**
         * 把对象toString
         * 
         * @param object 对象
         * @return 对象转换为String的类型
         * @throws Exception
         */
        public static String toString(Object object) throws RefertException {
            if (object == null) {
                return null;
            }
            StringBuffer sb = new StringBuffer();
            Class c = object.getClass();
            Method[] methods = c.getMethods();
            Field[] fields = c.getDeclaredFields();
            String fieldName, methodName;
            Method method;
            Field field;
            for (int i = 0; i < fields.length; i++) {
                field = fields[i];
                fieldName = field.getName();
                for (int j = 0; j < methods.length; j++) {
                    method = methods[j];
                    methodName = method.getName();
                    if (methodName.toUpperCase().endsWith("GET" + fieldName.toUpperCase())) {
                        try {
                            sb.append(fieldName + "=" + method.invoke(object));
                        } catch (Exception e) {
                            throw new RefertException("映射新对象" + c + "失败,列名:" + fieldName + ",方法名:" + methodName + "/n" + e);
                        }
                        break;
                    }
                }
                if (i < fields.length - 1) {
                    sb.append(",");
                }
            }
            return sb.toString();
        }
    
        /**
         * 把String类型转换成map(只需键值对)
         * 
         * @param stringByMap 键值对的形式,中间用英文逗号分隔
         * @return map
         */
        public static Map refertForMap(String stringByMap) {
            // 验证输入,若是空,返回null
            if (Validate.isNull(stringByMap)) {
                return null;
            }
            // 根据“,”分隔开每一组数值
            String[] keyValues = stringByMap.split(",");
            // 定义返回类型
            Map map = new HashMap();
            for (int i = 0; i < keyValues.length; i++) {
                // 根据“=”分隔开key和value值
                // 存放每一组数值
                String[] key_value = keyValues[i].split("=");
                // 如果不存在key或value则继续下次组装
                if (key_value.length != 2) {
                    continue;
                }
                // 存放key和value
                String key;
                // 把key值去除空格,并转成大写
                try {
                    key = key_value[0].trim().toUpperCase();
                } catch (RuntimeException e) {
                    continue;
                }
                // 获取value值
                String value = key_value[1];
                // 存入map
                map.put(key, value);
            }
            // 返回map
            return map;
        }
    
        /**
         * 把String类型转换成对象
         * 
         * @param stringByObject 键值对的形式,中间用英文逗号分隔
         * @param obj            要映射的对象
         * @return Object 映射后的对象
         * @throws Exception
         */
        public static Object refertForObject(String stringByObject, Object obj) throws RefertException {
            if ("java.lang.Class".equals(obj.getClass().getName())) {
                throw new RefertException("Object不应为java.lang.Class类型");
            }
            // 验证输入,若是空,返回null
            if (Validate.isNull(stringByObject)) {
                return null;
            }
            // 根据“,”分隔开每一组数值
            String[] keyValues = stringByObject.split(",");
            // 存放key和value
            String key, value;
            // 定义返回类型
            for (int i = 0; i < keyValues.length; i++) {
                // 根据“=”分隔开key和value值
                // 存放每一组数值
                String[] key_value = keyValues[i].split("=");
                // 如果不存在key或value则继续下次组装
                if (key_value.length != 2) {
                    continue;
                }
                // 把key值去除空格,并转成大写
                try {
                    key = key_value[0].trim().toUpperCase();
                } catch (RuntimeException e) {
                    continue;
                }
                // 获取value值
                value = key_value[1];
                // 存入map
                setObject(key, value, obj);
            }
            // 返回map
            return obj;
        }
    
        /**
         * 把request映射成map
         * 
         * @param request
         * @return
         * @throws RefertException
         */
        public static Map refertForMapByRequest(HttpServletRequest request) throws RefertException {
            try {
                // 获取页面上的所有name值
                Enumeration pNames = request.getParameterNames();
                // 存放符合条件的name-value映射值
                Map initMap = new HashMap();
                // 获取页面的操作类型
                // 遍历页面所有的name值
                while (pNames.hasMoreElements()) {
                    // 获取当前要验证的name值
                    String name = (String) pNames.nextElement();
                    String value = request.getParameter(name);
                    initMap.put(name.toUpperCase(), value);
                }
                // 返回map
                return initMap;
            } catch (Exception e) {
                throw new RefertException("获取页面信息失败
    " + e);
            }
        }
    
        /**
         * 把request映射成Object
         * 
         * @param request
         * @param obj
         * @return
         * @throws RefertException
         */
        public static Object refertForObjectByRequest(HttpServletRequest request, Object obj) throws RefertException {
            try {
                Map map = refertForMapByRequest(request);
                refertForObjectByMap(map, obj);
                return obj;
            } catch (Exception e) {
                throw new RefertException(e);
            }
        }
    
        /**
         * 把Object映射成Map
         * 
         * @param object
         * @return
         * @throws RefertException
         */
        public static Map<String, Object> refertForMapByObject(Object object) throws RefertException {
            if (object == null) {
                return null;
            }
            Map<String, Object> map = new HashMap<String, Object>();
            Class c = object.getClass();
            Method[] methods = c.getMethods();
            Field[] fields = c.getDeclaredFields();
            String fieldName, methodName;
            Method method;
            Field field;
            for (int i = 0; i < fields.length; i++) {
                field = fields[i];
                fieldName = field.getName();
                for (int j = 0; j < methods.length; j++) {
                    method = methods[j];
                    methodName = method.getName();
                    if (methodName.toUpperCase().endsWith("GET" + fieldName.toUpperCase())) {
                        try {
                            map.put(fieldName, method.invoke(object));
                        } catch (Exception e) {
                            throw new RefertException("映射新对象" + c + "失败,列名:" + fieldName + ",方法名:" + methodName + "/n" + e);
                        }
                        break;
                    }
                }
            }
            return map;
        }
    
        /**
         * 把map映射成对象
         * 
         * @param map 映射源内容
         * @param obj 映射成的对象
         * @return obj
         * @throws RefertException
         */
        public static Object refertForObjectByMap(Map<Object, Object> map, Object obj) throws RefertException {
            try {
                if (Validate.isNull(map))
                    return null;
                if (Validate.isNull(obj))
                    return null;
                if ("java.lang.Class".equals(obj.getClass().getName()))
                    throw new RefertException("Object不应为java.lang.Class类型");
                for (Map.Entry<Object, Object> e : map.entrySet()) {
                    String key = e.getKey().toString();
                    Object value = e.getValue();
                    setObject(key, value, obj);
                }
                return obj;
            } catch (Exception e) {
                throw new RefertException(e);
            }
        }
    
        /**
         * 映射对象
         * 
         * @param name  映射的名称
         * @param value 映射的值
         * @param obj   映射的对象
         * @return 映射完成的对象
         * @throws RefertException
         */
        private static Object setObject(String name, Object value, Object obj) throws RefertException {
            Class c = obj.getClass();
            String setName;
            name = "SET" + name;
            name = name.toUpperCase();
            Method[] methods = c.getDeclaredMethods();
            Method method;
            String valueType = value.getClass().getName();
            for (int i = 0; i < methods.length; i++) {
                method = methods[i];
                setName = method.getName().toUpperCase();
                if (name.equals(setName)) {
                    Class[] pts = method.getParameterTypes();
                    if (pts.length != 1) {
                        throw new RefertException("映射新对象" + c + "失败,name=" + name + "的传入参数不唯一,映射失败
    ");
                    }
                    String parameterType = pts[0].getTypeName();
                    if (parameterType.equals(valueType)) {
                        // 对象接收值类型与传入值类型相同
                    } else if ("java.util.Map".equals(parameterType) && Map.class.isAssignableFrom(value.getClass())) {
                        // 对象接收值类型map是传入值的父类
                    } else if ("java.util.List".equals(parameterType) && List.class.isAssignableFrom(value.getClass())) {
                        // 对象接收值类型list是传入值的父类
                    } else if ("java.lang.Object".equals(parameterType)) {
                        // 对象接收值类型object
                    } else if ("java.lang.String".equals(value.getClass().getName())) {
                        try {
                            if ("byte".equals(parameterType)) {
                                value = Byte.valueOf((String) value);
                            } else if ("short".equals(parameterType)) {
                                value = Short.valueOf((String) value);
                            } else if ("int".equals(parameterType)) {
                                value = Integer.valueOf((String) value);
                            } else if ("long".equals(parameterType)) {
                                value = Long.valueOf((String) value);
                            } else if ("float".equals(parameterType)) {
                                value = Float.valueOf((String) value);
                            } else if ("double".equals(parameterType)) {
                                value = Double.valueOf((String) value);
                            } else if ("char[]".equals(parameterType)) {
                                value = value.toString().toCharArray();
                            } else if ("boolean".equals(parameterType)) {
                                value = Boolean.valueOf((String) value);
                            } else if ("double".equals(parameterType)) {
                                value = Double.valueOf((String) value);
                            } else {
                                // map不支持(map内嵌套map,对map根据逗号进行拆分时出错)
                                throw new RefertException();
                            }
                        } catch (Exception e) {
                            StringBuffer refertLogger = new StringBuffer();
                            refertLogger.append("映射新对象" + c + "失败,");
                            refertLogger.append("name=" + name + ",");
                            refertLogger.append(valueType + "强转为" + parameterType + "失败");
                            throw new RefertException(refertLogger.toString());
                        }
                    } else {
                        StringBuffer refertLogger = new StringBuffer();
                        refertLogger.append("映射新对象" + c + "失败,");
                        refertLogger.append("name=" + name + ",");
                        refertLogger.append(valueType + "强转为" + parameterType + "失败");
                        throw new RefertException(refertLogger.toString());
                    }
                    try {
                        method.invoke(obj, value);
                        break;
                    } catch (Exception e) {
                        StringBuffer refertLogger = new StringBuffer();
                        refertLogger.append("映射新对象" + c + "失败,");
                        refertLogger.append("name=" + name + "。");
                        throw new RefertException(refertLogger.toString());
                    }
                }
            }
            return obj;
        }
    }

    生命不息,奋斗不止

  • 相关阅读:
    DICOMDIR结构
    给文件夹添加Everyone用户
    关于Predicate<T>委托
    开发者必备的6款源码搜索引擎
    Create XML Files Out Of SQL Server With SSIS And FOR XML Syntax
    create xml file from sql script
    DICOM中的入门概念
    小米note开启调试模式
    [转] Java基础知识——Java语言基础
    Java语言基本语法
  • 原文地址:https://www.cnblogs.com/ltlm/p/10263023.html
Copyright © 2011-2022 走看看