Mybatis自查询递归查找子菜单(三级)

实体类:

import java.util.List;

public class TaskTypeConcatenationDto {

    private Integer id;

    private String title;

    private List<TaskTypeConcatenationDto> taskTypeList;

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public List<TaskTypeConcatenationDto> getTaskTypeList() {
        return taskTypeList;
    }

    public void setTaskTypeList(List<TaskTypeConcatenationDto> taskTypeList) {
        this.taskTypeList = taskTypeList;
    }
}

 mapper.xml文件:

<!--无条件或根据typeName模糊查询TaskType表的级联列表(一二三级)-->
    <resultMap id="taskTypeConcatenationThreeDtoMap" type="com.studentHarbor.backstage.system.pojo.dto.TaskTypeConcatenationDto">
        <id column="id" property="id"/>
        <result column="title" property="title"/>
        <collection property="taskTypeList" ofType="com.studentHarbor.backstage.system.pojo.dto.TaskTypeConcatenationDto" select="getTaskTypeTwoConcatenationListOfThree" column="id">
        </collection>
    </resultMap>
    <!--无条件或根据typeName模糊查询TaskType表的级联列表(一二三级)-->
    <resultMap id="taskTypeConcatenationThreeDtoMap2" type="com.studentHarbor.backstage.system.pojo.dto.TaskTypeConcatenationDto">
        <id column="id" property="id"/>
        <result column="title" property="title"/>
        <collection property="taskTypeList" ofType="com.studentHarbor.backstage.system.pojo.dto.TaskTypeConcatenationDto" select="getTaskTypeThreeConcatenationListOfThree" column="id">
        </collection>
    </resultMap>

    <!--一级-->
    <select id="getTaskTypeOneConcatenationListOfThree" resultMap="taskTypeConcatenationThreeDtoMap" parameterType="com.studentHarbor.backstage.system.pojo.qo.TaskTypeQo">
        SELECT
        task_type.id,
        task_type.typeName AS title
        FROM
        task_type
        WHERE
        task_type.deleted = 0 AND task_type.level = 1
        <if test=" typeName != null and typeName != ''">
            AND task_type.typeName LIKE CONCAT('%',#{typeName},'%')
        </if>
    </select>

    <!--二级-->
    <select id="getTaskTypeTwoConcatenationListOfThree" parameterType="int" resultMap="taskTypeConcatenationThreeDtoMap2">
        SELECT
        task_type.id,
        task_type.typeName AS title
        FROM
        task_type
        WHERE
        task_type.deleted = 0 AND task_type.level = 2 AND task_type.parentId = #{id}
    </select>

    <!--三级-->
    <select id="getTaskTypeThreeConcatenationListOfThree" parameterType="int" resultType="com.studentHarbor.backstage.system.pojo.dto.TaskTypeConcatenationDto">
        SELECT
        task_type.id,
        task_type.typeName AS title
        FROM
        task_type
        WHERE
        task_type.deleted = 0 AND task_type.level = 3 AND task_type.parentId = #{id}
    </select>

在swagger-ui.html上测试的结果如下:

{
  "StatusCode": 0,
  "Succeed": true,
  "Message": "查询成功!",
  "Data": {
    "data": [
      {
        "id": 155,
        "title": "高级管理",
        "taskTypeList": [
          {
            "id": 156,
            "title": "高级管理职位",
            "taskTypeList": [
              {
                "id": 157,
                "title": "CEO/总裁/总经理",
                "taskTypeList": null
              },
              {
                "id": 158,
                "title": "副总裁/副总经理",
                "taskTypeList": null
              },
              {
                "id": 159,
                "title": "事业部负责人",
                "taskTypeList": null
              },
              {
                "id": 160,
                "title": "区域/分公司/代表处负责人",
                "taskTypeList": null
              },
              {
                "id": 161,
                "title": "总裁/总经理/董事长助理",
                "taskTypeList": null
              },
              {
                "id": 162,
                "title": "合伙人",
                "taskTypeList": null
              },
              {
                "id": 163,
                "title": "创始人",
                "taskTypeList": null
              },
              {
                "id": 164,
                "title": "董事会秘书",
                "taskTypeList": null
              }
            ]
          }
        ]
      }
    ],
    "dataTotal": 1,
    "pageTotal": 1
  }
}

 

 

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值