08-07
11

JAVA+Hibernate 无限级分类

数据库结构:


Create TABLE `admin_menu` (
`menu_id` int(4) NOT NULL auto_increment,
`menu_name` varchar(50) default NULL,
`parent_id` int(4) default '0',
`menu_value` varchar(255) default NULL,
`menu_sort` int(4) default '0',
`menu_description` varchar(50) default NULL,
`isvalid` tinyint(1) default '0',
PRIMARY KEY (`menu_id`)
)

主要看menu_id和parent_id这两个字段。

Eclipse生成的表持久映射:(说明:自己加level属性,作用:为了记录种类所在深度)


public class AdminMenu implements java.io.Serializable {


    // Fields  

     private Integer menuId;
     private String menuName;
     private Integer parentId;
     private String menuValue;
     private Integer menuSort;
     private String menuDescription;
     private Short isvalid;
     private int level;


    // Constructors

    /** default constructor */
    public AdminMenu() {
    }

/** minimal constructor */
    public AdminMenu(Integer menuId) {
        this.menuId = menuId;
    }
    
    /** full constructor */
    public AdminMenu(Integer menuId, String menuName, Integer parentId, String menuValue, Integer menuSort, String menuDescription, Short isvalid) {
        this.menuId = menuId;
        this.menuName = menuName;
        this.parentId = parentId;
        this.menuValue = menuValue;
        this.menuSort = menuSort;
        this.menuDescription = menuDescription;
        this.isvalid = isvalid;
    }

  
    // Property accessors

    public Integer getMenuId() {
        return this.menuId;
    }
    
    public void setMenuId(Integer menuId) {
        this.menuId = menuId;
    }

    public String getMenuName() {
        return this.menuName;
    }
    
    public void setMenuName(String menuName) {
        this.menuName = menuName;
    }

    public Integer getParentId() {
        return this.parentId;
    }
    
    public void setParentId(Integer parentId) {
        this.parentId = parentId;
    }

    public String getMenuValue() {
        return this.menuValue;
    }
    
    public void setMenuValue(String menuValue) {
        this.menuValue = menuValue;
    }

    public Integer getMenuSort() {
        return this.menuSort;
    }
    
    public void setMenuSort(Integer menuSort) {
        this.menuSort = menuSort;
    }

    public String getMenuDescription() {
        return this.menuDescription;
    }
    
    public void setMenuDescription(String menuDescription) {
        this.menuDescription = menuDescription;
    }

    public Short getIsvalid() {
        return this.isvalid;
    }
    
    public void setIsvalid(Short isvalid) {
        this.isvalid = isvalid;
    }

public int getLevel()
{
   return level;
}

public void setLevel(int level)
{
   this.level = level;
}

}


/**
* 查询管理员菜单
*
* @return
*/
public static AdminMenu[] selectAdminMenu(String condition)
{
   AdminMenu[] adminMenu = null;
   String hql = "from AdminMenu as am" + condition;
   List list = select(hql);
  
   Object[] o = list.toArray();
   if(o != null)
   {
    adminMenu = new AdminMenu[o.length];
    for(int i = 0; i < o.length; i++)
    {
     adminMenu[i] = (AdminMenu)o[i];
    }
   }
   return adminMenu;
}


/**
* 树的生成(主要用数组进行递归)
*
* @return
*/
public static ArrayList getTree(AdminMenu[] menu, int parentId, int level, ArrayList list)
{
   for(int i = 0; i < menu.length; i++)
   {
    if(menu[i].getParentId().intValue() == parentId)
    {
     menu[i].setLevel(level);
     list.add(menu[i]);
     getTree(menu, menu[i].getMenuId(), level+1, list);
    }
   }
   return list;
}


文章来自: 本站原创
引用通告: 查看所有引用 | 我要引用此文章
Tags: 无限级分类
相关日志:
评论: 0 | 引用: 0 | 查看次数: 738
发表评论
昵 称:
密 码: 游客发言不需要密码.
内 容:
验证码: 验证码
选 项:
虽然发表评论不用注册,但是为了保护您的发言权,建议您注册帐号.
字数限制 1000 字 | UBB代码 开启 | [img]标签 关闭