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;
}
补充:软件开发 , Java ,