• 数据库对象映射为java对象,不使用框架


    方法:

    public static <T> List<T> processResultSetToList(ResultSet rs, Class<T> clazz) throws Exception {
            List<T> ls = new ArrayList<T>();
            Field[] fields = clazz.getDeclaredFields();
            while (rs.next()) {
                T tt = clazz.newInstance();
                for (Field field : fields) {
                    try {
                        /**
                         * 如果Oracle数据库中的类型是 number p.getWriteMethod().invoke(tt, new
                         * Object[] { rs.getObject(p.getName()) }) 会报如下错误
                         * java.lang.IllegalArgumentException: argument type
                         * mismatch --- 另外如果mysql数据库中的bigint 在进行setXxxx(Integer
                         * val)时候也会抛出该异常 也会抛出该异常
                         */
                        Object value = null;
                        try {
                            String fieldName = getColumnName(field);
                            switch (TypeUtils.getMappedType(field)) {
                            case Long:
                                value = Long.valueOf(rs.getLong(fieldName));
                                break;
                            case Integer:
                                value = Integer.valueOf(rs.getInt(fieldName));
                                break;
                            case Double:
                                value = Double.valueOf(rs.getDouble(fieldName));
                                break;
                            case Byte:
                                InputStream ins = rs.getBinaryStream(fieldName);
                                if(ins != null){
                                    value = IOUtils.toByteArray(ins);
                                }
                                break;
                            case Date:
                                /**
                                 * 15-12-8
                                 * 转为util.Date
                                 */
                                Timestamp t = rs.getTimestamp(fieldName);
                                if(t!=null){
                                    value = new Date(t.getTime());
                                }
                                break;
                            default:
                                value = rs.getObject(fieldName);
                                break;
                            }
                        } catch (Exception e) {// 如果仅仅查询Class的部分字段
                            if (e.getMessage().matches("Column\s+'.+?'\s+not\s+found.")) {
                                switch (TypeUtils.getMappedType(field)) {
                                case Long:
                                case Integer:
                                    value = 0;
                                    break;
                                default:
                                    value = null;
                                    break;
                                }
                            }
                        }
                        field.setAccessible(true);
                        if(value != null){
                            field.set(tt, value);
                        }
                    } catch (IllegalArgumentException e) {
                        e.printStackTrace();
                    }
                }
                ls.add(tt);
            }
            DBUtils.close(rs, null, null);
            return ls;
        }
  • 相关阅读:
    每日立会2015-11-30
    Some Modern Softwares' drawbacks: User experience 12/29/2015
    Sprint 5 summary: UI 界面更新,Azure端部署和用户反馈分析 12/28/2015
    Daily Scrum 12/25/2015
    Daily Scrum 12/24/2015
    Daily Scrum 12/23/2015
    Daily Scrum 12/21/2015
    Daily Scrum 12/18/2015
    Daily Scrum 12/17/2015
    Performance standard (ALPHA release) 12/17/2015
  • 原文地址:https://www.cnblogs.com/clovem/p/5637019.html
Copyright © 2020-2023  润新知