学习总结,学习路上不迷路 此次内容是关于 mapper 实现菜单的多级查询
需求
- 写一个后台管理系统
- 需要实现菜单的多级导航
实现核心代码
- 实体类
package com.yk.po;
import java.util.List;
/**
* @author yk.ddm
* @createDate 2021-05-31 11:29
*
* 菜单实体类 管理员菜单 普通用户菜单
*/
public class Menu {
private int id;
private int parent_id; // 父 id
private String name; // 菜单名称
private String path; // 跳转地址
private String icon; // 菜单icon图标
private List<Menu> children; // 子菜单
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public int getParent_id() {
return parent_id;
}
public void setParent_id(int parent_id) {
this.parent_id = parent_id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPath() {
return path;
}
public void setPath(String path) {
this.path = path;
}
public String getIcon() {
return icon;
}
public void setIcon(String icon) {
this.icon = icon;
}
public List<Menu> getChildren() {
return children;
}
public void setChildren(List<Menu> children) {
this.children = children;
}
}
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<!-- 命名空间 -->
<mapper namespace="com.yk.dao.MenuDao">
<!-- 用户登录 -->
<select id="getMenusByRoleId" parameterType="Integer" resultMap="menusResult">
SELECT *
FROM menu
WHERE parent_id = 0
AND id IN
(SELECT menu_id
FROM authority
WHERE role_id = #{role_id})
</select>
<select id="getItemMenuByParentId" resultType="menu" >
select * from menu
where parent_id = #{id}
</select>
<resultMap type="Menu" id="menusResult">
<id property="id" column="id" />
<result property="name" column="name" />
<result property="icon" column="icon" />
<collection property="children" column="id" select="getItemMenuByParentId" />
</resultMap>
</mapper>
在此记录一下,哈哈哈, 这个collection的使用,利用属性select 实现子查询