SpringMVC 中 jdbcTemplate.queryForList 返回结果 封装成 实体 对象

在调用 jdbcTemplate.queryForLis方法时,返回的结果类似于{id=xx…}{id=xx…}{id=xx…},这时我们通常希望是直接返回对象的集合,这时我们可以将结果先转成map,贴一段实例代码:

List<?> queryList = jdbcTemplate.queryForList(sql.toString(), values);
        List<User> dtoList = new ArrayList<User>();
        for (int i = 0; i < queryList.size(); i++) {
            Map<String, Object> map = (Map<String, Object>) queryList.get(i);
            User user = (User) DtoReflectionSqlUtil.getDtoObject(map, cls);
            dtoList.add(user );
        }
 pageInfo.setResult(dtoList);

===========================================

DtoReflectionSqlUtil.getDtoObject(xx,xx)这是一个工具方法,作用在于将map转化成对应的实体对象返回回来,主要思路是通过反射去调用实体对象里面每个字段的setXxxx()方法,将map里面的值取出来set到对象里,然后返回对象。实现如下:

public static Object getDtoObject(Map<String, Object> map, Class<?> cls) throws Exception {
        Constructor<?> ct = cls.getConstructor();
        Object retobj = ct.newInstance();
        Class<?> supClass = cls.getSuperclass();
        Method[] ms = cls.getDeclaredMethods();
        if (!”java.lang.Object”.equals(supClass.getName())) {
            Method[] supms = supClass.getDeclaredMethods();
            ms = (Method[]) ArrayUtils.addAll(ms, supms);
        }
        for (int i = 0; i < ms.length; i++) {
            Method m = ms[i];
            Class<?> pts[] = m.getParameterTypes();
            if (m.getReturnType().toString().equals(“void”)) {
                Object value = map.get(m.getName().replaceAll(“set”, “”).toUpperCase());
                if (value != null) {
                    if (pts[0] == String.class) {
                        m.invoke(retobj, new Object[] { value.toString() });
                    } else if (pts[0] == Integer.class||pts[0] == int.class) {
                        m.invoke(retobj, new Object[] { Integer.parseInt(value.toString()) });
                    } else if (pts[0] == Long.class||pts[0] == long.class) {
                        m.invoke(retobj, new Object[] { Long.parseLong(value.toString()) });
                    } else if (pts[0] == Double.class||pts[0] == double.class) {
                        m.invoke(retobj, new Object[] { Double.parseDouble(value.toString()) });
                    } else if (pts[0] == Calendar.class) {
                        DateFormat format = new SimpleDateFormat(“yyyy-MM-dd HH:mm:ss”);
                        Calendar calendar = Calendar.getInstance();
                        calendar.setTime(format.parse(value.toString()));
                        m.invoke(retobj, new Object[] { calendar });
                    }else if (pts[0] == BigDecimal.class) {
                        m.invoke(retobj, new Object[] { value });
                    }
                } else {
                    if (pts[0] == Integer.class) {
                        m.invoke(retobj, new Object[] { 0 });
                    } else if (pts[0] == Long.class) {
                        m.invoke(retobj, new Object[] { 0L });
                    } else if (pts[0] == Double.class) {
                        m.invoke(retobj, new Object[] { 0.00 });
                    }
                }
            }
        }
        return retobj;
    }

如此一来,便可以将jdbcTemplate.queryForLis()返回的值转换成相对应的对象集合,以便使用。




    原文作者:机智的大黄鱼
    原文地址: https://blog.csdn.net/qq_28233015/article/details/54580411
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞