Sprting boot2+mybatis3实现树形结构查询

原型图

 业务需求:查询出工作组下面的工位

树形查询

数据库sql

-- ----------------------------
-- Table structure for working_center
-- ----------------------------
DROP TABLE IF EXISTS "public"."working_center";
CREATE TABLE "public"."working_center" (
"id" serial DEFAULT nextval('working_center_id_seq'::regclass) NOT NULL,
"name" varchar(50) COLLATE "default",
"code" varchar(30) COLLATE "default",
"parent_id" int4,
"status" char(1) COLLATE "default",
"type" varchar(50) COLLATE "default",
"remarks" varchar(255) COLLATE "default"
)
WITH (OIDS=FALSE)

;
COMMENT ON TABLE "public"."working_center" IS '工作中心信息';
COMMENT ON COLUMN "public"."working_center"."id" IS '唯一标识';
COMMENT ON COLUMN "public"."working_center"."name" IS '名称';
COMMENT ON COLUMN "public"."working_center"."code" IS '编码';
COMMENT ON COLUMN "public"."working_center"."parent_id" IS '父级中心';
COMMENT ON COLUMN "public"."working_center"."status" IS '状态';
COMMENT ON COLUMN "public"."working_center"."type" IS '类型';
COMMENT ON COLUMN "public"."working_center"."remarks" IS '备注';

-- ----------------------------
-- Alter Sequences Owned By 
-- ----------------------------

-- ----------------------------
-- Primary Key structure for table working_center
-- ---------------------------- ALTER TABLE "public"."working_center" ADD PRIMARY KEY ("id");      

 主要字段工作组id等于工位的parent_id,一个工作组对应多个工位属于一对多关系。

所以使用一对多查询

pojo层代码

@Data
@ApiModel(description = "工作中心")
public class WorkingCenter {

    @ApiModelProperty(notes = "唯一标识,新增时不需要传参")
    private Integer id;

    @ApiModelProperty(notes = "名称")
    @NotNull(message = "name is null")
    @Length(max = 50, message = "名称过长")
    private String name;

    @ApiModelProperty(notes = "编码")
    @NotNull(message = "code is null")
    @Length(max = 30, message = "编码过长")
    private String code;

    @ApiModelProperty(notes = "父级ID")
    private Integer parentId;

    @ApiModelProperty(hidden = true, notes = "父类名称")
    private String parentName;

    @ApiModelProperty(notes = "状态 0停用 1启用 新增时不需要传参 ")
    private Character status;

    @ApiModelProperty(notes = "类型 (车间 产线 工作组 工位)")
    @NotNull(message = "type is null")
    private CenterType type;

    @ApiModelProperty(notes = "备注")
    @Length(max = 255, message = "备注过长")
    private String remarks;

    @ApiModelProperty(notes = "关联人员")
    @NotNull(message = "userIds is null")
    private List<Integer> userIds;

    @ApiModelProperty(notes = "关联设备")
    private List<Integer> equipmentIds;

    @ApiModelProperty(notes = "工位关联设备(仅工位时使用)")
    private EquipmentVO equipment;

    @ApiModelProperty(notes = "子集")
    private List<WorkingCenter> children;


}

 service层:

/**
     * 查询指定工作组下面的工位(根据id,树形结构)
     */
    public List<WorkingCenter> getChildrenByCenterIdWithStation(){
        List<WorkingCenter> workLineId = workingCenterMapper.getGroupTree();

        return workLineId;
    }

controller层:

@ApiOperation("树形结构查询工位")
    @GetMapping("/station")
    private Return getWrokingStatus(){
        List<WorkingCenter> childrenByCenterIdWithStation = workingCenterService.getChildrenByCenterIdWithStation();

        return Return.success(childrenByCenterIdWithStation);
    }

mapper文件:

<resultMap id="workingCenterTreeWithStationResultMap" type="net.vtstar.cloudmes.business.workingcenter.domain.WorkingCenter">
        <id column="id" property="id" javaType="java.lang.Integer"/>
        <result column="name" property="name" javaType="java.lang.String"/>
        <result column="code" property="code" javaType="java.lang.String"/>
        <result column="parent_id" property="parentId" javaType="java.lang.Integer"/>
        <result column="status" property="status" javaType="java.lang.Character"/>
        <result column="type" property="type"/>
        <result column="remarks" property="remarks" javaType="java.lang.String"/>
        <collection property="equipmentIds" column="id" select="getEquipmentByCenterId"/>
        <collection property="children" column="id" select="getChildrenByCenterIdWithStation"/>
</resultMap>



<!--树形查找工作组下面的工位-->
    <select id="getChildrenByCenterIdWithStation" resultMap="workingCenterTreeWithStationResultMap">
        select
        <include refid="SQL_WORKING_CENTER_COLUMN"/>
        from WORKING_CENTER wc_
        <where>
            wc_.parent_id = #{centerId}
            and wc_.type = 'STATION'
            and wc_.status = '1'
            order by wc_.id desc
        </where>
    </select>


    <!--树形查询工作组下面的工位-->
    <select id="getGroupTree" resultMap="workingCenterTreeWithStationResultMap">
        select
        <include refid="SQL_WORKING_CENTER_COLUMN" />
        from
        WORKING_CENTER wc_
        <where>
            wc_.type = 'WORKGROUP'
            and wc_.status = '1'
        </where>
    </select>
</mapper>

实现树形查询

原文地址:https://www.cnblogs.com/xuehu666/p/11699240.html