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;
}
 
歡迎訪問愛步stop
相關文章
相關標籤/搜索