zoukankan      html  css  js  c++  java
  • Model与Record转换适配

    /**
     * 
     * Model属性名称转换适配
     * @author Administrator
     *
     */
    public class BsModelPropAdapter {
    
        private BsModelPropAdapter(){}
        
        /**
         * 适配转换单个属性
         */
        public static <T> T adapter(Record record,Class<T> modelClass){
            /**
             * record Map 转换 model 属性
             */
            Record r = (Record)record;
            T model = null;
            try {
                model = modelClass.newInstance();
                Field[] fields = model.getClass().getDeclaredFields();
                
                /* 遍历映射关系  */
                for (Field field : fields) {
                    field.setAccessible(true);
                    if(field.getName().equals("serialVersionUID"))continue;
                    if(field.getAnnotation(Ignore.class) != null)continue;
                    
                    if(field.getType() == int.class || field.getType() == Integer.class){
                        field.set(model, NumberUtils.toInt(StringUtil.toString(r.get(field.getName()))));
                    }else{
                        field.set(model, r.get(field.getName()));
                    }
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
            return model;
        }
        
        /**
         * 适配转换集合
         */
        @SuppressWarnings("unchecked")
        public static <T> List<T> adapterList(List<?> records,Class<T> modelClass){
            List<T> models = Lists.newArrayList();
            T model = null;
            List<Record> rs = (List<Record>)records;
            for (Record r : rs) {
                model = adapter(r,modelClass);
                models.add(model);
            }
            return models;
        }
        
        /**
         * 适配转换分页对象
         */
        @SuppressWarnings("unchecked")
        public static <T> BsPageRow adapterPageRow(BsPageRow pg,Class<T> modelClass){
            List<T> models = Lists.newArrayList();
            List<Record> rs = (List<Record>)pg.getRows();
            models = adapterList(rs, modelClass);
            pg.setRows(models);
            return pg;
        }
        
        /**
         * 表单数据适配转换到对象属性
         * @param <T>
         */
        public static <T> Record adapterByModel(Record record,Class<T> modelClass){
            Record r = (Record)record;
            T model = null;
            Record recordTo = new Record();
            try {
                model = modelClass.newInstance();
                Field[] fields = model.getClass().getDeclaredFields();
                /* 遍历映射关系  */
                for (Field field : fields) {
                    field.setAccessible(true);
                    if(field.getName().equals("serialVersionUID"))continue;
                    if(field.getAnnotation(Ignore.class) != null)continue;
                    recordTo.set(field.getName(), r.get(field.getName()));
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
            return recordTo;
        }
        
        /**
         * 表单数据适配转换到对象属性
         * @param <T>
         */
        public static <T> Record adapterByRecord(Class<T> modelClass){
            T model = null;
            Record recordTo = new Record();
            try {
                model = modelClass.newInstance();
                Field[] fields = model.getClass().getDeclaredFields();
                /* 遍历映射关系  */
                for (Field field : fields) {
                    field.setAccessible(true);
                    if(field.getName().equals("serialVersionUID"))continue;
                    if(field.getAnnotation(Ignore.class) != null)continue;
                    recordTo.set(field.getName(), field.get(field.getName()));
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
            return recordTo;
        }
        
        /**
         * 表单数据适配转换到对象属性
         * @param <T>
         */
        @SuppressWarnings("rawtypes")
        public static <T> Record adapterModelToModel(Object object,Class<T> modelClass){
            T model = null;
            Record recordTo = new Record();
            Class c = object.getClass();
            try {
                model = modelClass.newInstance();
                Field[] fields = model.getClass().getDeclaredFields();
                /* 遍历映射关系  */
                for (Field field : fields) {
                    field.setAccessible(true);
                    if(!checkType(field.getType()) || field.getAnnotation(Ignore.class) != null 
                            || field.getName().equals("serialVersionUID")) 
                        continue;
                    PropertyDescriptor pd = new PropertyDescriptor(field.getName(), c);
                    Method getMethod = pd.getReadMethod();// 获得get方法
                    Object o = getMethod.invoke(object);// 执行get方法返回一个Object
                    if(field.getType() == int.class || field.getType() == Integer.class){
                        recordTo.set(field.getName(),NumberUtils.toInt(StringUtil.toString(o)));
                    }else{
                        recordTo.set(field.getName(), o==null?"":o.toString());
                    }
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
            return recordTo;
        }
        
        public static boolean checkType(Object type) {
            if ((type == String.class) || (type == Integer.class) || (type == Integer.TYPE) || (type == Double.class)
                    || (type == Double.TYPE) || (type == Float.class) || (type == Float.TYPE)) {
                return true;
            }
            return false;
        }
        
    }
  • 相关阅读:
    prometheus对硬盘的监控指标
    zabbix高级用法-Zabbix Tags获取到对应的触发器的核心代码段
    Confluence rest api接口
    zabbix官方模板库
    路由追踪程序Traceroute分析与科普
    C语言进制之间转换
    ubuntu挂载目录在windows10下权限问题
    C语言之原码、反码和补码
    centos下软件的安装与卸载
    PHP设计模式
  • 原文地址:https://www.cnblogs.com/zt528/p/5438848.html
Copyright © 2011-2022 走看看