① Database design:
Pojo Class for database mapping
@JsonIgnoreProperties(ignoreUnknown = true)@Table(name="MENU")public class Menu { @Id private String menuNumber; //菜单编号 private String menuName; //菜单名称 private String menuUrl; //菜单URL private String parentMenu; //父级菜单 private Integer menuPermissions; //菜单访问权限 //Setter和Getter及toString()省略 }
② Mapper class for accessing data through Mybaites
Public interface Menumapper {//Gets the menu from the menu's parent menu and menu (to get all the first-level menus when initializing) @Select ("Select * from menu where LENGTHB (paren T_menu) = #{menulevel} and Menu_permissions=#{permissions} ") List<menu> Selectmenubylevelandper (@Param (" MenuLevel ") int menulevel, @Param (" Permissions ") int permissions);} ③ Service Layer Services ' @Servicepublic class Menuserviceimp implements menuservice{@Autowired private Menumapper menumapper; @Override public list<menu> getmenubyparent (String parentmenu,integer menupermissions) {//TODO auto-g enerated method Stub Menu Menu = new menu (); Menu.setparentmenu (Parentmenu); Menu.setmenupermissions (menupermissions); list<menu> menulist = Menumapper.select (Menu); The "Menumapper.select (menu)" function implements the return menulist through JPA; } @Override Public list<menu> getmenubylevel (Integer menulevel,integer menupermissions) {//TODO auto- Generated method stub if (menulevel<=0) return null;Query System.out.println by parent menu (menulevel+ "= = =" + menupermissions); if (menupermissions==null) return Menumapper.selectmenubylevel (MenuLevel); else return Menumapper.selectmenubylevelandper (MenuLevel, menupermissions); }}
④ Controller Controllers
@RequestMapping("/getMenuInit") public @ResponseBody List<Menu> getMenuInit(Integer perminsion){ System.out.println("getMenuInit" +"=="+ perminsion); //System.out.println(menuService.getAllMenu().toString()); return menuService.getMenuByLevel(1, perminsion); } @RequestMapping("/getMenu") public @ResponseBody List<Menu> getMenu(String parentMenu,Integer perminsion){ System.out.println(" getMenu:" + parentMenu+" == "+ perminsion); //System.out.println(menuService.getAllMenu().toString()); return menuService.getMenuByParent(parentMenu, perminsion); }
⑤ Front-end page design
Initialize the jquery function: Access all the first level menus
' var flag=0; $ ("#showDiv"). HTML ("") $.ajax ({url: "/menu/getmenuinit", data:{"Perminsion": "2"}, Type: "Post", DataType: "JSON", success:function (data) {for (Var i=0;i<dat a.length;i++) {var menu = data[i]; $ ("#showDiv"). Append ("<li class= ' Nav-item ' >" + "<a href= ' javascript:; '" + "onclick= ' GetMenu (" +menu.menunumber+ "," +menu.menupermissions+ "," +i+ ") ' ><i class= ' My-icon nav-icon icon_ ' + ((i+1)%3+1) + ' ></i><span> ' + M enu.menuname+ "</span><i class= ' My-icon nav-more ' ></i></a></li>") } } })
When clicked, the function GetMenu (parentmenu,perminsion,divid) {var div = document.getElementById ("Showdiv") is called. Getele Mentsbytagname ("Li") [divID] if (parentmenu.length%2!=0) parentmenu= "0" +parentmenu; if (div.getelementsbytagname ("Li"). length>1) {for (Var i=0;i<div.getelementsbytagname ("Li"). length;i++) Div.getelementsbytagname ("Li") [i].innerhtml= "" "Return False} $.ajax ({url:"/menu/getme Nu ", data:{" Parentmenu ":p arentmenu," Perminsion ":p erminsion}, type:" Post ", DataType:" JSON ", Success:function (data) {var html=div.innerhtml for (Var i=0;i<data.lengt h;i++) {var menu = data[i]; menunumber=menu.menunumber.substring (1,menu.menunumber.len GTH); html=html+ "<li><a><span>" +menu.menuname+ "</span></a></li>"} div.innerhtml = HTML; } });
}
```
Database control dynamic Menu display (SSM framework)