JdbcTemplate 接收对象

方法一:springboot提供函数,返回什么类型,对应就使用什么类型接受解析;

 List<Phone> query = jdbcTemplate.query(sql, new Object[]{mobile}, new BeanPropertyRowMapper(Phone.class));

方法二:实现  RowMapper内部类 (没处理一个实体类就得重写这个)

        Phone phone = jdbcTemplate.queryForObject(sql, new Object[]{mobile}, new RowMapper<Phone>() {
            @Override
            public Phone mapRow(ResultSet rs, int rowNum) throws SQLException {
                ResultSetMetaData metaData = rs.getMetaData();
                String name = metaData.getColumnName(1);
                Phone phone = new Phone();
                phone.setId(rs.getString("id"));
                phone.setPrefix(rs.getString("prefix"));
                phone.setMobile(rs.getString("mobile"));
                phone.setAreacode(rs.getString("areacode"));
                phone.setProvice(rs.getString("provice"));
                phone.setCiyt(rs.getString("city"));
                phone.setIsp(rs.getString("isp"));
                phone.setLocation(rs.getString("location"));
                return phone;
            }
        });

方法3:(实现通用RowMapper)

Phone phone = jdbcTemplate.queryForObject(sql, new Object[]{mobile}, new LocalRowMapper<>(Phone.class));
    public LocalRowMapper(Class<?> targetClazz) {
        this.targetClazz = targetClazz;
        fieldMap = new HashMap<>();
        Field[] fields = targetClazz.getDeclaredFields();
        for (Field field : fields) {
            fieldMap.put(field.getName(), field);
        }
    }

 @Override
    public T mapRow(ResultSet rs, int rowNum) throws SQLException {
        T obj = null;

        try {
            obj = (T) targetClazz.newInstance();
            final ResultSetMetaData metaData = rs.getMetaData();
            int columnLength = metaData.getColumnCount();
            String columnName = null;
            for (int i = 1; i <= columnLength; i++) {
                columnName = metaData.getColumnName(i).toLowerCase();
                Class fieldClazz = fieldMap.get(columnName).getType();
                Field field = fieldMap.get(columnName);
                // 当isAccessible()的结果是false时不允许通过反射访问该字段 当该字段时private修饰时isAccessible()得到的值是false,必须要改成true才可以访问
                field.setAccessible(true);
                if (fieldClazz == int.class || fieldClazz == Integer.class) { // int
                    field.set(obj, rs.getInt(columnName));
                }else if (fieldClazz == boolean.class || fieldClazz == Boolean.class) { //boolean
                    field.set(obj, rs.getBoolean(columnName));
                }else if (fieldClazz == String.class) { //string
                    field.set(obj, rs.getString(columnName));
                }else if (fieldClazz == Date.class) { //Date
                    field.set(obj, rs.getDate(columnName));
                }else if (fieldClazz == Long.class || fieldClazz == long.class) { // Long
                    field.set(obj, rs.getLong(columnName));
                }else if (fieldClazz == Timestamp.class) { //Timestamp
                    field.set(obj, rs.getTimestamp(columnName));
                }
                field.setAccessible(false);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return obj;
    }

 返回数据:

{
    "code":200,
    "msg":"操作成功",
    "data":{
        "id":"355477",
        "prefix":"182",
        "mobile":"1829212",
        "areacode":"0911",
        "provice":"陕西",
        "city":"延安",
        "isp":"移动",
        "location":"陕西延安移动"
    }
}

原文地址:https://www.cnblogs.com/setout/p/13898436.html