Sprting boot2+mybatis3實現樹形結構查詢

原型圖java

 業務需求:查詢出工做組下面的工位sql

樹形查詢數據庫

 

 

數據庫sqlapp

-- ----------------------------
-- 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,一個工做組對應多個工位屬於一對多關係。dom

因此使用一對多查詢ui

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層:spa

/**
     * 查詢指定工做組下面的工位(根據id,樹形結構)
     */
    public List<WorkingCenter> getChildrenByCenterIdWithStation(){
        List<WorkingCenter> workLineId = workingCenterMapper.getGroupTree();

        return workLineId;
    }

controller層:3d

@ApiOperation("樹形結構查詢工位")
    @GetMapping("/station")
    private Return getWrokingStatus(){
        List<WorkingCenter> childrenByCenterIdWithStation = workingCenterService.getChildrenByCenterIdWithStation();

        return Return.success(childrenByCenterIdWithStation);
    }

mapper文件:code

<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>

實現樹形查詢

相關文章
相關標籤/搜索