• 通用RowMapper封装查询结果到自定义类中


    package object;

    import java.lang.reflect.Field;
    import java.sql.ResultSet;
    import java.sql.SQLException;
    import org.springframework.jdbc.core.RowMapper;

    public class ObjectRowMapper implements RowMapper{
        private Class className;
       
        public ObjectRowMapper(Class className){
            this.className = className;
        }
       
       
        public Object mapRow(ResultSet rs, int rowNum) throws SQLException {
           
           
            Object nt = new Object();
            Field[] fields = className.getDeclaredFields();
            try {
                nt = className.newInstance();
                for (Field field : fields) {
                    //如果结果中没有改field项则跳过
                    try {
                        rs.findColumn(field.getName());
                    } catch (Exception e) {
                        continue;
                    }
                    //修改相应filed的权限
                    boolean accessFlag = field.isAccessible();
                    field.setAccessible(true);
                    String value = rs.getString(field.getName());
                    value = value==null?"":value;
                    setFieldValue(nt, field, value);
                   
                    //恢复相应field的权限
                    field.setAccessible(accessFlag);
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
            return nt;
        }
       
       
        public static void setFieldValue(Object form, Field field, String value) {

            String elemType = field.getType().toString();

            if (elemType.indexOf("boolean") != -1||elemType.indexOf("Boolean") != -1) {
                try {
                    field.set(form, Boolean.valueOf(value));
                } catch (IllegalAccessException e) {
                    e.printStackTrace();
                }
            } else if (elemType.indexOf("byte") != -1||elemType.indexOf("Byte") != -1) {
                try {
                    field.set(form, Byte.valueOf(value));
                } catch (IllegalAccessException e) {
                    e.printStackTrace();
                }
            } else if (elemType.indexOf("char") != -1||elemType.indexOf("Character") != -1) {
                try {
                    field.set(form, Character.valueOf(value.charAt(0)));
                } catch (IllegalAccessException e) {
                    e.printStackTrace();
                }
            } else if (elemType.indexOf("double") != -1||elemType.indexOf("Double") != -1) {
                try {
                    field.set(form, Double.valueOf(value));
                } catch (IllegalAccessException e) {
                    e.printStackTrace();
                }
            } else if (elemType.indexOf("float") != -1||elemType.indexOf("Float") != -1) {
                try {
                    field.set(form, Float.valueOf(value));
                } catch (IllegalAccessException e) {
                    e.printStackTrace();
                }
            } else if (elemType.indexOf("int") != -1||elemType.indexOf("Integer") != -1) {
                try {
                    field.set(form, Integer.valueOf(value));
                } catch (IllegalAccessException e) {
                    e.printStackTrace();
                }
            } else if (elemType.indexOf("long") != -1||elemType.indexOf("Long") != -1) {
                try {
                    field.set(form, Long.valueOf(value));
                } catch (IllegalAccessException e) {
                    e.printStackTrace();
                }
            } else if (elemType.indexOf("short") != -1||elemType.indexOf("Short") != -1) {
                try {
                    field.set(form, Short.valueOf(value));
                } catch (IllegalAccessException e) {
                    e.printStackTrace();
                }
            } else {
                try {
                    field.set(form, (Object) value);
                } catch (IllegalAccessException e) {
                    e.printStackTrace();
                }
            }
        }
    }

     
  • 相关阅读:
    我叫mt3.0更新公告
    gcc 编译器常用的命令行参数一览
    C++两个类相互引用错误留影
    C++中的声明与定义
    C++ 杂记
    C++中两个类相互包含引用的相关问题
    Ogre 中使用OIS的两种模式
    Ogre 渲染队列(二)
    Ogre 渲染队列(一)
    Ogre 场景管理器
  • 原文地址:https://www.cnblogs.com/eyesmoon/p/7503403.html
Copyright © 2020-2023  润新知