數據庫控制動態菜單顯示(SSM框架)
阿新 • • 發佈:2018-01-14
fun ppi mapping menu 數據庫 ring int ride success ① 數據庫設計:
數據庫映射的POJO類
@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()省略 }
②通過mybaites訪問數據的Mapper類
public interface MenuMapper { //通過菜單的父級菜單和菜單的權限獲取菜單(初始化時要獲取所有的一級菜單) @Select("select * from MENU where LENGTHB(Parent_Menu) = #{menulevel} and Menu_Permissions=#{permissions}") List<Menu> selectMenuByLevelAndPer(@Param("menulevel")int menulevel,@Param("permissions")int permissions); } ③服務層Service `` @Service public class MenuServiceImp implements MenuService{ @Autowired private MenuMapper menuMapper; @Override public List<Menu> getMenuByParent(String parentMenu,Integer menuPermissions) { // TODO Auto-generated method stub Menu menu = new Menu(); menu.setParentMenu(parentMenu); menu.setMenuPermissions(menuPermissions); List<Menu> menuList = menuMapper.select(menu); //“menuMapper.select(menu)” 函數通過JPA實現 return menuList; } @Override public List<Menu> getMenuByLevel(Integer menulevel,Integer menuPermissions) { // TODO Auto-generated method stub if(menulevel<=0) return null; //通過父級菜單查詢 System.out.println(menulevel+" ==== " + menuPermissions); if(menuPermissions==null) return menuMapper.selectMenuByLevel(menulevel); else return menuMapper.selectMenuByLevelAndPer(menulevel, menuPermissions); } }
④控制器Controller
@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); }
⑤前端頁面設計
初始化調 jQuery函數:訪問所有的一級菜單
` var flag=0;
$("#showDiv").html("")
$.ajax(
{
url:"/menu/getMenuInit",
data:{"perminsion":"2"},
type:"post",
dataType:"json",
success:function(data)
{
for(var i=0;i<data.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>"+
menu.menuName+
"</span><i class=‘my-icon nav-more‘></i></a></li>")
}
}
})
單擊時調用函數
```
function getMenu(parentMenu,perminsion,divid){
var div = document.getElementById("showDiv").getElementsByTagName("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/getMenu",
data:{"parentMenu":parentMenu,"perminsion":perminsion},
type:"post",
dataType:"json",
success:function(data)
{
var HTML=div.innerHTML
for(var i=0;i<data.length;i++){
var menu = data[i];
menuNumber=menu.menuNumber.substring(1,menu.menuNumber.length);
HTML=HTML+"<li><a><span>"+menu.menuName+"</span></a></li>"
}
div.innerHTML = HTML;
}
});
}
```
數據庫控制動態菜單顯示(SSM框架)