mysql怎么查询树形结构的所有上级,你这个问题是怎么解决的?求指导
1个回答
2016-06-21
展开全部
java版的实际例子。类同你说的情况
private void findChildList(AssetType parent,List<AssetType> list){
String hql = "from AssetType a where a.parentAssetType.assetTypeId=? ORDER BY a.sort,a.assetTypeName asc";
List<AssetType> childList = this.assetTypeDao
.getEntityManager()
.createQuery(hql)
.setParameter(1, parent.getAssetTypeId())
.getResultList();
int size = childList.size();
if(size>0){
for (int i = 0; i < size; i++) {
AssetType assetType = childList.get(i);
List<AssetType> childs = assetType.getChildAssetType();
if(childs.size()>0){
list.addAll(childs);
this.findChildList(assetType, list);//递归查询节点的子节点
}
private void findChildList(AssetType parent,List<AssetType> list){
String hql = "from AssetType a where a.parentAssetType.assetTypeId=? ORDER BY a.sort,a.assetTypeName asc";
List<AssetType> childList = this.assetTypeDao
.getEntityManager()
.createQuery(hql)
.setParameter(1, parent.getAssetTypeId())
.getResultList();
int size = childList.size();
if(size>0){
for (int i = 0; i < size; i++) {
AssetType assetType = childList.get(i);
List<AssetType> childs = assetType.getChildAssetType();
if(childs.size()>0){
list.addAll(childs);
this.findChildList(assetType, list);//递归查询节点的子节点
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询