有時候頁面需要使用jQuery easy ui中的combotree,需要給combotree提供一個JSON數據,使用如下方法(遞歸)實現(下面是dao層的實現層):
/** * 根據表名和父id拼裝樹形JSON數據 * @param tableName * @param parentId * @return * @throws Exception */ @Override public String createTreeJsonBytableNameAndParentId(String tableName,String parentId) throws Exception { Map<String, Object> map = new HashMap<String, Object>(); List<Map<String, Object>> list = new ArrayList<Map<String,Object>>(); String sql = " select * from " + tableName + " where 1 = 1 "; if (StringUtils.isNotEmpty(parentId)) { sql = sql + " and parent_id = '" + parentId + "'"; }else { sql = sql + " and parent_id is null"; } Session session = getSession(); Query query = session.createSQLQuery(sql); //存儲過程鍵值對應 query.setResultTransformer(Transformers.ALIAS_TO_ENTITY_MAP); List<Map<String,String>> lists = query.list(); for (Map<String,String> msp : lists) { map = new HashMap<String, Object>(); List<Map<String, Object>> list_children = new ArrayList<Map<String,Object>>(); String id = msp.get("id"); map.put("id",msp.get("code")); map.put("text",msp.get("name")); list_children = createTreeChildJson(tableName,id); if (list_children.size()>0) { map.put("state","closed"); map.put("children", list_children); } list.add(map); } JSONArray arry = JSONArray.fromObject(list); return arry.toString(); } /** * 根據表名和父id拼裝樹形LIST數據遞歸 * @param tableName * @param parentId * @return * @throws Exception */ private List<Map<String, Object>> createTreeChildJson(String tableName,String parentId) throws Exception { Map<String, Object> map = new HashMap<String, Object>(); List<Map<String, Object>> list = new ArrayList<Map<String,Object>>(); String sql = " select * from " + tableName + " where 1 = 1 "; if (StringUtils.isNotEmpty(parentId)) { sql = sql + " and parent_id = '" + parentId + "'"; }else { sql = sql + " and parent_id is null"; } Session session = getSession(); Query query = session.createSQLQuery(sql); //存儲過程鍵值對應 query.setResultTransformer(Transformers.ALIAS_TO_ENTITY_MAP); List<Map<String,String>> lists = query.list(); for (Map<String,String> msp : lists) { map = new HashMap<String, Object>(); List<Map<String, Object>> list_children = new ArrayList<Map<String,Object>>(); String id = msp.get("id"); map.put("id",msp.get("code")); map.put("text",msp.get("name")); list_children = createTreeChildJson(tableName,id); if (list_children.size()>0) { map.put("state","closed"); map.put("children", list_children); } list.add(map); } return list; }
拼裝好的數據如下:
[ { "id": "09DE010000", "text": "漳衛新河", "state": "closed", "children": [ { "id": "09DE010100", "text": "四女寺減河" }, { "id": "09DE010200", "text": "岔河" }, { "id": "09DE010300", "text": "六五河" } ] }, { "id": "09DE020000", "text": "馬頰河", "state": "closed", "children": [ { "id": "09DE020100", "text": "篤馬河", "state": "closed", "children": [ { "id": "09DE020101", "text": "趙王河" } ] }, { "id": "09DE020200", "text": "二股河" }, { "id": "09DE020300", "text": "朱龍河" }, { "id": "09DE020400", "text": "德惠新河", "state": "closed", "children": [ { "id": "09DE020401", "text": "幸福河" }, { "id": "09DE020402", "text": "商東河" }, { "id": "09DE020403", "text": "臨商河" } ] } ] }, { "id": "09DE030000", "text": "沾利河" }, { "id": "09DE040000", "text": "秦口河", "state": "closed", "children": [ { "id": "09DE040100", "text": "清坡河" }, { "id": "09DE040200", "text": "溝盤河" } ] }, { "id": "09DE050000", "text": "徒駭河", "state": "closed", "children": [ { "id": "09DE050100", "text": "沙河" }, { "id": "09DE050200", "text": "潘庄總干渠" }, { "id": "09DE050300", "text": "溫聰河" }, { "id": "09DE050400", "text": "倪倫河" }, { "id": "09DE050500", "text": "老趙牛河" }, { "id": "09DE050600", "text": "趙牛河", "state": "closed", "children": [ { "id": "09DE050601", "text": "中心河" }, { "id": "09DE050602", "text": "巴公河" } ] }, { "id": "09DE050700", "text": "葦河" }, { "id": "09DE050800", "text": "土馬河" } ] }, { "id": "09DE060000", "text": "大沙河" }, { "id": "09DE070000", "text": "淄脈河" }, { "id": "09DE080000", "text": "小清河", "state": "closed", "children": [ { "id": "09DE080100", "text": "杏花河" }, { "id": "09DE080200", "text": "孝婦河" }, { "id": "09DE080300", "text": "綉河" }, { "id": "09DE080400", "text": "淄河" } ] }, { "id": "09DE090000", "text": "新塌河", "state": "closed", "children": [ { "id": "09DE090100", "text": "織女河" }, { "id": "09DE090200", "text": "北陽河" } ] }, { "id": "09DE100000", "text": "彌河", "state": "closed", "children": [ { "id": "09DE100100", "text": "堯河" } ] }, { "id": "09DE110000", "text": "白浪河" }, { "id": "09DE120000", "text": "虞河" }, { "id": "09DE130000", "text": "濰河", "state": "closed", "children": [ { "id": "09DE130100", "text": "汶河" }, { "id": "09DE130200", "text": "渠河" } ] }, { "id": "09DE140000", "text": "五龍河", "state": "closed", "children": [ { "id": "09DE140100", "text": "富水河" }, { "id": "09DE140200", "text": "清水河" }, { "id": "09DE140300", "text": "硯河" } ] }, { "id": "09DE150000", "text": "嶗山水庫" }, { "id": "09DE160000", "text": "墨水河" }, { "id": "09DE170000", "text": "王河" }, { "id": "09DE180000", "text": "界河" }, { "id": "09DE190000", "text": "黃水河", "state": "closed", "children": [ { "id": "09DE190100", "text": "內夾河" } ] }, { "id": "09DE200000", "text": "大沽夾河", "state": "closed", "children": [ { "id": "09DE200100", "text": "清陽河" }, { "id": "09DE200200", "text": "門樓水庫" } ] }, { "id": "09DE210000", "text": "老母豬河", "state": "closed", "children": [ { "id": "09DE210100", "text": "母豬河" } ] }, { "id": "09DE220000", "text": "黃壘河" }, { "id": "09DE230000", "text": "乳山河" }, { "id": "09DE240000", "text": "大沽河", "state": "closed", "children": [ { "id": "09DE240100", "text": "小沽河" }, { "id": "09DE240200", "text": "五沽河" }, { "id": "09DE240300", "text": "瀦河" }, { "id": "09DE240400", "text": "膠萊河", "state": "closed", "children": [ { "id": "09DE240401", "text": "膠河" }, { "id": "09DE240402", "text": "澤河" } ] } ] } ]
數據庫結構如下:
第一級,parent_id為null
第二級,parent_id為第一級數據的id
第三級,parent_id為第二級數據的id
......
以上僅供參考