Database Structure:
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 ')
)
Mainly look at the menu_id and parent_id fields.
Persistent Eclipse ing of tables generated by Eclipse: (Note: add the level Attribute by yourself. purpose: to record the depth of the type)
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;
}
}
/**
* Query the Administrator menu
*
* @ 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;
}
/**
* Tree generation (mainly using Arrays for recursion)
*
* @ 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;
}