数据库结构:
CreateTABLE`admin_menu`(
`menu_id`int(4)NOTNULLauto_increment,
`menu_name`varchar(50)defaultNULL,
`parent_id`int(4)default'0',
`menu_value`varchar(255)defaultNULL,
`menu_sort`int(4)default'0',
`menu_description`varchar(50)defaultNULL,
`isvalid`tinyint(1)default'0',
PRIMARYKEY(`menu_id`)
)
主要看menu_id和parent_id这两个字段。
Eclipse生成的表持久映射:(说明:自己加level属性,作用:为了记录种类所在深度)
publicclassAdminMenuimplementsjava.io.Serializable{
//Fields
privateIntegermenuId;
privateStringmenuName;
privateIntegerparentId;
privateStringmenuValue;
privateIntegermenuSort;
privateStringmenuDescription;
privateShortisvalid;
privateintlevel;
//Constructors
/**defaultconstructor*/
publicAdminMenu(){
}
/**minimalconstructor*/
publicAdminMenu(IntegermenuId){
this.menuId=menuId;
}
/**fullconstructor*/
publicAdminMenu(IntegermenuId,StringmenuName,IntegerparentId,StringmenuValue,IntegermenuSort,StringmenuDescription,Shortisvalid){
this.menuId=menuId;
this.menuName=menuName;
this.parentId=parentId;
this.menuValue=menuValue;
this.menuSort=menuSort;
this.menuDescription=menuDescription;
this.isvalid=isvalid;
}
//Propertyaccessors
publicIntegergetMenuId(){
returnthis.menuId;
}
publicvoidsetMenuId(IntegermenuId){
this.menuId=menuId;
}
publicStringgetMenuName(){
returnthis.menuName;
}
publicvoidsetMenuName(StringmenuName){
this.menuName=menuName;
}
publicIntegergetParentId(){
returnthis.parentId;
}
publicvoidsetParentId(IntegerparentId){
this.parentId=parentId;
}
publicStringgetMenuValue(){
returnthis.menuValue;
}
publicvoidsetMenuValue(StringmenuValue){
this.menuValue=menuValue;
}
publicIntegergetMenuSort(){
returnthis.menuSort;
}
publicvoidsetMenuSort(IntegermenuSort){
this.menuSort=menuSort;
}
publicStringgetMenuDescription(){
returnthis.menuDescription;
}
publicvoidsetMenuDescription(StringmenuDescription){
this.menuDescription=menuDescription;
}
publicShortgetIsvalid(){
returnthis.isvalid;
}
publicvoidsetIsvalid(Shortisvalid){
this.isvalid=isvalid;
}
publicintgetLevel()
{
returnlevel;
}
publicvoidsetLevel(intlevel)
{
this.level=level;
}
}
/**
*查询管理员菜单
*
*@return
*/
publicstaticAdminMenu[]selectAdminMenu(Stringcondition)
{
AdminMenu[]adminMenu=null;
Stringhql="fromAdminMenuasam"+condition;
Listlist=select(hql);
Object[]o=list.toArray();
if(o!=null)
{
adminMenu=newAdminMenu[o.length];
for(inti=0;i<o.length;i++)
{
adminMenu[i]=(AdminMenu)o[i];
}
}
returnadminMenu;
}
/**
*树的生成(主要用数组进行递归)
*
*@return
*/
publicstaticArrayListgetTree(AdminMenu[]menu,intparentId,intlevel,ArrayListlist)
{
for(inti=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);
}
}
returnlist;
}