数据库读取树,并处理成json格式_JAVA_编程开发_程序员俱乐部

中国优秀的程序员网站程序员频道CXYCLUB技术地图
热搜:
更多>>
 
您所在的位置: 程序员俱乐部 > 编程开发 > JAVA > 数据库读取树,并处理成json格式

数据库读取树,并处理成json格式

 2013/7/10 15:35:36  xfy142305  程序员俱乐部  我要评论(0)
  • 摘要:表结构:tableNameparentTableABACBDBECFCG展示树:ABCDEFG@ControllerpublicclassViewLevelMetaController{privateLoglogger=LogFactory.getLog(this.getClass());privateStringBuffernodeStr=newStringBuffer();@AutowiredprivateViewLevelMetaServiceviewLevelMetaService
  • 标签:数据库 数据 JSON JS

表结构:

tableName? parentTable

? A????????????????? B

? A????????????????? C

? B????????????????? D

? B??????????????????E

? C????????????????? F

? C??????????????????G

?

展示树:

??????????? A

???B?????????????? C

D???? E????? ?F?????? G

?

?

@Controller
public class ViewLevelMetaController {
???
?private Log logger = LogFactory.getLog(this.getClass());
?private StringBuffer nodeStr=new StringBuffer();

?@Autowired
?private ViewLevelMetaService viewLevelMetaService;
?@Autowired
?private TableTreeService tableTreeService;


?@RequestMapping("/metadata/getViewLevelTableCreateInfo.do")
?public String getViewLevelTableCreateInfo(ModelMap model, String table) {
??logger.info("getViewLevelTableCreateInfo");
??
??List<treeNode> nodeList = tableTreeService.getTreeNodeList();
??//当json字符串为空时才输出,防止重复输出
??if("[]".equals(modifyStr(nodeStr.toString()))){
???getTreeNode(nodeList, new treeNode("A","A"));
??}
??
??model.put("treeNodeStr",modifyStr(nodeStr.toString()));
??
??return "metadata/main";
?}

?/**
? * 修饰一下才能满足Json格式?
? * @param nodeStr
? * @return
? */
?public String modifyStr(String nodeStr){
??????? return ("["+nodeStr+"]").replaceAll(",]", "]");????
??? }?

?/**
? * 把树组装成json格式数据
? *???? A
? * B????? C
? *
? * [{tableName:'A',url:'null',children:[
? *? {tableName:'B',url:'null',children:null},
? *? {tableName:'C',url:'null',children:null}
? *? ]}]
? *?
? * @param treeNodeList
? * @param node
? */
?private void getTreeNode(List<treeNode> treeNodeList, treeNode node){
?? if(hasChild(treeNodeList,node)){???
???????????? nodeStr.append("{tableName:");?
???????????? nodeStr.append("'"+node.getTableName()+"'");?
???????????? nodeStr.append(",url:");?
???????????? nodeStr.append("'"+node.getUrl()+"'");?
???????????? nodeStr.append(",children:[");???
???????????? List<treeNode> childNodeList = getChildList(treeNodeList,node);???
???????????? for(treeNode childNode:childNodeList){
???????????? ?getTreeNode(treeNodeList,childNode);
???????????? }
???????????? nodeStr.append("]},");???
???????? }else{???
???????????? nodeStr.append("{tableName:");?
???????????? nodeStr.append("'"+node.getTableName()+"'");
???????????? nodeStr.append(",url:");?
???????????? nodeStr.append("'"+node.getUrl()+"'");?
???????????? nodeStr.append(",children:null},");?
???????? }?
?}

?/**
? * 判断是否有子节点?
? * @param list
? * @param node
? * @return
? */
?private boolean hasChild(List<treeNode> list, treeNode node){
???? return getChildList(list,node).size()>0?true:false;?
? }

?/**
? * 得到子节点列表?
? * @param list
? * @param node
? * @return
? */
?private List<treeNode> getChildList(List<treeNode> list , treeNode node){?
??List<treeNode> li = new ArrayList<treeNode>();??
???? for(treeNode l:list){
???? ?if(l.getParentTable().equals(node.getTableName())){
???? ??li.add(l);
???? ?}
???? }
???? return li;????
???? }?



}

?

发表评论
用户名: 匿名