獲取JSON格式的樹形


需求:前端需要一個JSON格式的Tree,例如組織機構維護的時候使用到的,需要一次性返回給前端。所以編寫了一個算是半通用的查詢方法

1、數據庫Dao層:

  /**
     * 根據父ID獲取子數據
     * @param tableName 表名
     * @param pColName 父字段名
     * @param pColValue 父字段名的值
     * @return
     */
    @Override
    public List<Map<String, Object>> getList(String tableName, String pColName, String pColValue) {
        String sql = "select * from "+tableName+" where "+pColName+"=:pColValue";

        Map<String, String> paramMap = new HashMap<String, String>();
        paramMap.put("pColValue",pColValue);
        return getNamedParameterJdbcTemplate().queryForList(sql, paramMap);
    }

2、通用Service

/**
     * 獲取JSONArray
     * @param tableName 表名
     * @param pColName 父字段名
     * @param colName 編號對應字段名
     * @param pColValue 父字段名的值
     * @return
     * @throws Exception
     */
    public JSONArray getJSONArray(String tableName, String pColName,String colName,String pColValue) throws Exception{
        JSONArray array = new JSONArray();

        List<Map<String, Object>> areaList = getList(tableName,pColName,pColValue);
        for (Map<String, Object> map : areaList){
            String newPvalue = map.get(colName).toString();
            Map<String,Object> newMap = MapUtils.transToLowerCase(map);
            JSONObject object = JSONObject.parseObject(JSON.toJSONString(newMap));
            object.put("children",getAreaArrayByPAreaNo(tableName,pColName,colName,newPvalue));
            array.add(object);
        }
        return array;
    }

    /**
     * 
     * @param tableName
     * @param pColName
     * @param colName
     * @param pColValue
     * @return
     * @throws IllegalAccessException
     */
    List<Map<String,Object>> getAreaArrayByPAreaNo(String tableName, String pColName,String colName, String pColValue) throws IllegalAccessException{
        JSONObject object = new JSONObject();
        List<Map<String,Object>> list = new ArrayList<>();
        JSONArray tmp = new JSONArray();
        List<Map<String,Object>> resultList = getList(tableName,pColName,pColValue);
        if(resultList != null && resultList.size()>0){
            for (Map<String,Object> map : resultList){
                String newPvalue = map.get(colName).toString();
                Map<String,Object> newMap = MapUtils.transToLowerCase(map);
List
<Map<String, Object>> nodeList = getAreaArrayByPAreaNo(tableName,pColName,colName,newPvalue); if (nodeList != null && nodeList.size()>0) { newMap.put("children", nodeList); } list.add(newMap); } } return list; }

3、Action調用

/**
     * 加載行政區划
     * @param request
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/list_area", method = RequestMethod.GET)
    @ResponseBody
    public ResponseResult listarea(HttpServletRequest request) throws Exception {
        String tableName = "BC_AREA";
        String pColName = "P_AREA_NO";
        String pColValue = "0";
        String colName = "AREA_NO";

        //獲取樹形
        JSONArray array = commonService.getJSONArray(tableName,pColName,colName,pColValue);

        return ResultUtil.success(array);
    }

  最終返回的結果類似:

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM