查询结果
[
{
"id": 1,
"menuName": "菜单01(1层)",
"pid": 0,
"list": [
{
"id": 3,
"menuName": "菜单01(2层)",
"pid": 1,
"list": [
{
"id": 5,
"menuName": "菜单01(3层)",
"pid": 3,
"list": [
{
"id": 7,
"menuName": "菜单01(4层)",
"pid": 5,
"list": []
}
]
}
]
}
]
},
{
"id": 2,
"menuName": "菜单02(1层)",
"pid": 0,
"list": [
{
"id": 4,
"menuName": "菜单02(2层)",
"pid": 2,
"list": [
{
"id": 6,
"menuName": "菜单02(3层)",
"pid": 4,
"list": [
{
"id": 8,
"menuName": "菜单02(4层)",
"pid": 6,
"list": []
}
]
}
]
}
]
}
]
数据库截图
返回模型
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import java.util.List;
/**
* @author yang
* @Title: WsMenuVO.java
* @Description: 菜单模型
* @date 2019年03月18日 9:35
*/
@Data
@Builder
@AllArgsConstructor
@NoArgsConstructor
public class WsMenuVO {
//菜单ID
private Integer id;
//菜单名称
private String menuName;
//父菜单ID
private Integer pid;
private List<WsMenuVO> list;
}
mybatis 语句
<!--级联查询返回模型-->
<resultMap type="com.cloudtop.demo19.VO.WsMenuVO" id="fatherMap">
<id column="id" property="id"/>
<id column="menuName" property="menuName"/>
<collection property="list" ofType="com.cloudtop.demo19.VO.WsMenuVO" column="id" select="findMenuByPid"/>
</resultMap>
<!--级联查询父菜单-->
<select id="selectAllMenu" resultMap="fatherMap" >
select * from ws_menu where pid = 0
</select>
<!--级联查询子菜单-->
<select id="findMenuByPid" resultMap="fatherMap" >
select * from ws_menu where pid = #{id}
</select>
controller
@RestController
@RequestMapping("/menu")
@Api(value = "菜单级联查询-WsMenuController",tags = "menu")
public class WsMenuController {
@Autowired
WsMenuService wsMenuService;
private Logger log = LoggerFactory.getLogger(getClass());
/**
* 查询所有菜单
*
* @return
*/
@ApiImplicitParams({
@ApiImplicitParam(paramType = "query", name = "pid", value = "父菜单ID",defaultValue = "0", required = false, dataType = "int")
})
@ApiOperation(value = "查询所有菜单-返回树形列表", httpMethod = "GET", notes = "能显示出层级多层列表")
@RequestMapping(value = "/selectAll",method = RequestMethod.GET)
public List<WsMenuVO> selectAllMenu(@RequestParam(value = "pid",required = false)Integer pid){
if (pid == null){
pid = 0;
}
List<WsMenuVO> result = wsMenuService.selectAllMenu(pid);
return result;
}
}
service
@Service
public class WsMenuService {
private Logger log = LoggerFactory.getLogger(getClass());
@Autowired
private WsMenuMapper wsMenuMapper;
public List<WsMenuVO> selectAllMenu(Integer pid) {
return wsMenuMapper.selectAllMenu(pid);
}
}
mapper
public interface WsMenuMapper extends BaseMapper<WsMenu> {
List<WsMenuVO> selectAllMenu(@Param("pid") Integer pid);
}