mybatisplus递归传递多个参数 | mybatisplus传递多个参数获取层级数据 | mybatisplus传递多个参数获取树形数据

搜索关键字:

mybatisplus关联查询传递参数|+"@select"+"树形结构"|+"@select"+"树形结构"+"传参"| +"@select"+"many"+"传参"| +"@select"+"column"+"传参"

1、效果图:

http://localhost:3000/menu/l1

mybatisplus递归传递多个参数 | mybatisplus传递多个参数获取层级数据 | mybatisplus传递多个参数获取树形数据_第1张图片

2、接口定义:

关键点:id=id,userID = USER_ID表示参数=数据库列名。

package com.xdy.springboot4vue.mapper;


import com.xdy.springboot4vue.DTO.MenuDTO;
import org.apache.ibatis.annotations.Result;
import org.apache.ibatis.annotations.Results;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Many;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public interface MenuMapper {

    @Results({
           
@Result(column = "id",property = "ID"),
           
@Result(column = "NAME",property = "NAME"),
           
@Result(column = "CODE",property = "CODE"),
           
@Result(column = "ORDERS",property = "ORDERS"),
           
@Result(column = "URL",property = "URL"),
           
@Result(column = "id=id,userID = USER_ID",property = "children",many = @Many(select = "com.xdy.springboot4vue.mapper.MenuMapper.getMenus"))
    })
   
@Select("SELECT\n" +
           
"\tTBL_MENU.*,TBL_USER_ROLE.USER_ID\n" +
           
"FROM\n" +
            "\ttbl_user\n" +
           
"INNER JOIN TBL_USER_ROLE ON TBL_USER_ROLE.USER_ID = tbl_user.id\n" +
           
"INNER JOIN TBL_ROLE_PRIVILEGE ON TBL_USER_ROLE.ROLE_ID = TBL_ROLE_PRIVILEGE.ROLE_ID\n" +
           
"INNER JOIN TBL_MENU ON TBL_MENU.ID = TBL_ROLE_PRIVILEGE.MENU_ID " +
           
" where tbl_user.id = #{id} and PARENT_ID is null")
   
List<MenuDTO> getCats(Long id);
   
// 获取指定目录的子菜单
    @Results({
           
@Result(column = "id",property = "ID"),
            
@Result(column = "NAME",property = "NAME"),
           
@Result(column = "CODE",property = "CODE"),
           
@Result(column = "ORDERS",property = "ORDERS"),
           
@Result(column = "URL",property = "URL")

    })
   
@Select("SELECT\n" +
           
"\tTBL_MENU.*,TBL_USER_ROLE.USER_ID\n" +
            
"FROM\n" +
            "\ttbl_user\n" +
           
"INNER JOIN TBL_USER_ROLE ON TBL_USER_ROLE.USER_ID = tbl_user.id\n" +
           
"INNER JOIN TBL_ROLE_PRIVILEGE ON TBL_USER_ROLE.ROLE_ID = TBL_ROLE_PRIVILEGE.ROLE_ID\n" +
           
"INNER JOIN TBL_MENU ON TBL_MENU.ID = TBL_ROLE_PRIVILEGE.MENU_ID WHERE PARENT_ID = #{id} and TBL_USER_ROLE.USER_ID=#{userID}  ")
   
List<MenuDTO> getMenus(Long id,Long userID);
}

3、菜单数据库表数据

 

4、DTO定义

package com.xdy.springboot4vue.DTO;


import com.baomidou.mybatisplus.annotation.TableField;

import java.util.List;

public class MenuDTO {
   
private Long ID;
   
private String NAME;
   
private String CODE;
   
private Long ORDERS;
   
private String URL;
   
@TableField(exist = false)
   
private List<MenuDTO> children;
//
省略get\set\toString定义

}

5、控制器定义:

package com.xdy.springboot4vue.controller;

import com.alibaba.fastjson.JSONObject;
import com.xdy.springboot4vue.DTO.MenuDTO;
import com.xdy.springboot4vue.mapper.MenuMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping
("/menu")
public class MenuController {

   
@Autowired
   
private MenuMapper menuMapper;

   
@GetMapping("/l1")
   
public JSONObject getMenus(){
       
JSONObject result = new JSONObject();
       
List<MenuDTO> menus = menuMapper.getCats(8L);
       
result.put("data",menus);
       
return  result;
    }

}

参考网址:

https://blog.csdn.net/qq_41047376/article/details/107021244

你可能感兴趣的:(java,服务器,数据库)