数据库中的数据是与树形方式存储的,比如:
id parentid
1 -1
2 1
3 1
4 3
可以无限级分类;
如果能得到一个根节点bean(已经存储了孩子节点) ,就得到了这棵树,就可以序列化成json或xml格式,这是我想要的;
使用hibernate 的@onetomany 建好关联,级联查询 是可以达到上面的效果,获得整个树形bean,以前一直采用过这种方式;
这里介绍如何使用递归的方法取得一颗大树;
1.数据库表语句:树形结构存储(mysql数据库)
CREATE TABLE `usg_fdp_tree` (
`Id` int(11) NOT NULL AUTO_INCREMENT,
`DataPointId` int(11) DEFAULT NULL,
`USGId` varchar(11) CHARACTER SET latin1 DEFAULT NULL,
`Label` varchar(255) CHARACTER SET latin1 NOT NULL,
`IsAbstract` int(11) DEFAULT NULL,
`Depth` int(11) DEFAULT NULL,
`Order` decimal(4,1) DEFAULT NULL,
`ParentId` int(11) DEFAULT NULL,
`Prefix` varchar(30) CHARACTER SET latin1 DEFAULT NULL,
`IndustryType` int(11) DEFAULT NULL,
`TableType` int(11) DEFAULT NULL
PRIMARY KEY (`Id`)
) ENGINE=InnoDB AUTO_INCREMENT=5805 DEFAULT CHARSET=utf8
`Id` int(11) NOT NULL AUTO_INCREMENT,
`DataPointId` int(11) DEFAULT NULL,
`USGId` varchar(11) CHARACTER SET latin1 DEFAULT NULL,
`Label` varchar(255) CHARACTER SET latin1 NOT NULL,
`IsAbstract` int(11) DEFAULT NULL,
`Depth` int(11) DEFAULT NULL,
`Order` decimal(4,1) DEFAULT NULL,
`ParentId` int(11) DEFAULT NULL,
`Prefix` varchar(30) CHARACTER SET latin1 DEFAULT NULL,
`IndustryType` int(11) DEFAULT NULL,
`TableType` int(11) DEFAULT NULL
PRIMARY KEY (`Id`)
) ENGINE=InnoDB AUTO_INCREMENT=5805 DEFAULT CHARSET=utf8
2. 对应的javabean(树形结构,不是hibernate bean) 去掉了一些字段:
带有simpleframework的注解
import java.util.ArrayList;
import java.util.List;
import org.simpleframework.xml.Attribute;
import org.simpleframework.xml.ElementList;
import org.simpleframework.xml.Root;
import java.util.List;
import org.simpleframework.xml.Attribute;
import org.simpleframework.xml.ElementList;
import org.simpleframework.xml.Root;
@Root(name="Item")
public class UsgIndustryTree
{
public class UsgIndustryTree
{
//@Attribute(name="id",required=false)
private Integer id;
//@Attribute(name="dataPointId",required=false)
private Integer dataPointId;
@Attribute(name="UsgId",required=false)
private String usgid;
@Attribute(name="Tag",required=false)
private String label;
@Attribute(name="ReportName",required=false)
private String reportName;
@Attribute(name="Value",required=false)
private String value;
@Attribute(name="Matched",required=false)
private String matched;
//@Attribute(name="isAbstract",required=false)
private Integer isAbstract;
private Integer id;
//@Attribute(name="dataPointId",required=false)
private Integer dataPointId;
@Attribute(name="UsgId",required=false)
private String usgid;
@Attribute(name="Tag",required=false)
private String label;
@Attribute(name="ReportName",required=false)
private String reportName;
@Attribute(name="Value",required=false)
private String value;
@Attribute(name="Matched",required=false)
private String matched;
//@Attribute(name="isAbstract",required=false)
private Integer isAbstract;
//@Attribute(name="depth",required=false)
private Integer depth;
//private BigDecimal order;
//@Attribute(name="parentId",required=false)
private Integer parentId;
//private String prefix;
//@Attribute(name="industryType",required=false)
private Integer industryType;
//private Integer tableType;
//private Integer creatorId;
//private Date createDate;
//private Integer updaterId;
//private Date updateDate;
//private Integer isValid;
@ElementList(name="children",required=false,inline=true)
private List<UsgIndustryTree> children =new ArrayList<UsgIndustryTree>();
private Integer depth;
//private BigDecimal order;
//@Attribute(name="parentId",required=false)
private Integer parentId;
//private String prefix;
//@Attribute(name="industryType",required=false)
private Integer industryType;
//private Integer tableType;
//private Integer creatorId;
//private Date createDate;
//private Integer updaterId;
//private Date updateDate;
//private Integer isValid;
@ElementList(name="children",required=false,inline=true)
private List<UsgIndustryTree> children =new ArrayList<UsgIndustryTree>();
public Integer getId()
{
return id;
}
..................
{
return id;
}
..................
..........
3. 主方法:得到树形java bean 数据
public UsgIndustryTree createTree(int isabstract,int industry,int tableType, int parentid) throws DALException
{
{
//得到实体
UsgIndustryTree industryTree = getTree(industry, parentid);
String sql = "select * from usg_fdp_tree where isabstract=? and industrytype=? and tableType=? and parentid =? order by id ";
Object[] params = new Object[]{isabstract, industry, tableType,parentid };
List<UsgFdpTree> list = Global.getResourceDB().executeSQL(sql, UsgFdpTree.class, params);
if (list == null || list.size() == 0)
{
UsgIndustryTree industryTree = getTree(industry, parentid);
String sql = "select * from usg_fdp_tree where isabstract=? and industrytype=? and tableType=? and parentid =? order by id ";
Object[] params = new Object[]{isabstract, industry, tableType,parentid };
List<UsgFdpTree> list = Global.getResourceDB().executeSQL(sql, UsgFdpTree.class, params);
if (list == null || list.size() == 0)
{
}
else
{
List<UsgIndustryTree> set = new ArrayList<UsgIndustryTree>();
for (UsgFdpTree tmp : list)
{
else
{
List<UsgIndustryTree> set = new ArrayList<UsgIndustryTree>();
for (UsgFdpTree tmp : list)
{
//递归调用
set.add(createTree(isabstract,industry, tableType,tmp.getId()));
}
industryTree.setChildren(set);
}
return industryTree;
set.add(createTree(isabstract,industry, tableType,tmp.getId()));
}
industryTree.setChildren(set);
}
return industryTree;
}
得到了bean 就相当于得到了json或者xml ,通过上一遍文件介绍的jsontools 工具 得到的庞大json数据: