代码片-下拉树实现
Posted niaonao
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了代码片-下拉树实现相关的知识,希望对你有一定的参考价值。
1.获取部门树的核心方法
/**
* @return java.util.List<BusinessCodeVO>
* @Description: 获取部门树
* @author niaonao
* @Date 2023/3/7 9:04
**/
public List<BusinessCodeVO> getDeptList()
String redisKey = "system_dept_list";
Long expire = 60 * 60 * 24L;
String redisMasterDeptCode = redisUtil.getString(redisKey);
List<BusinessCodeVO> businessCodeList = new ArrayList<>();
if (!Objects.isNull(redisMasterDeptCode))
businessCodeList = JSON.parseArray(redisMasterDeptCode).toJavaList(BusinessCodeVO.class);
return businessCodeList;
List<BusinessCodeVO> list = aafOrganManager.getCascadeOrganList();
if (CollectionUtil.isNotEmpty(list))
businessCodeList = this.list2Tree(list);
String businessCodeListJson = JSON.toJSONString(businessCodeList);
redisUtil.set(redisKey, businessCodeListJson, expire);
return businessCodeList;
/**
* @return java.util.List<BusinessCodeVO>
* @Description: list to tree
* @param: treeList
* @author niaonao
* @Date 2023/3/7 9:04
**/
private List<BusinessCodeVO> list2Tree(List<BusinessCodeVO> treeList)
List<BusinessCodeVO> retList = new ArrayList<>();
for (BusinessCodeVO parent : treeList)
if ("0".equals(parent.getParentId()))
retList.add(findChildren(parent, treeList));
return retList;
/**
* @return java.util.List<com.topnet.eic.vo.common.BusinessCodeVO>
* @Description: list to tree
* @param: treeList
* @author niaonao
* @Date 2023/3/7 9:04
**/
private BusinessCodeVO findChildren(BusinessCodeVO parent, List<BusinessCodeVO> treeList)
for (BusinessCodeVO child : treeList)
if (parent.getId().equals(child.getParentId()))
if (parent.getChildren() == null)
parent.setChildren(new ArrayList<>());
parent.getChildren().add(findChildren(child, treeList));
return parent;
2.响应结构示例
"code": "",
"msg": "",
"data":
"deptList": [
"children": [
"children": [
],
"codeName": "",
"codeValue": "",
"id": "",
"parentId": "",
"sort": ""
],
"codeName": "",
"codeValue": "",
"id": "",
"parentId": "",
"sort": ""
]
3.响应数据示例
"code":"00000",
"data":
"deptList":[
"codeName":"市场监督管理局",
"codeValue":"520000",
"sort":"766",
"id":"999",
"parentId":"0",
"children":[
"codeName":"经济检查总队",
"codeValue":"520000022",
"sort":"185",
"id":"152000002200000",
"parentId":"999",
"children":null
,
"codeName":"申诉举报受理处(12315申诉举报指挥中心)",
"codeValue":"520000013",
"sort":"186",
"id":"152000001300000",
"parentId":"999",
"children":[
"codeName":"测试子部门",
"codeValue":"00011111",
"sort":"1658818281289",
"id":"2c5985c57a40417ab69284817c272c74",
"parentId":"152000001300000",
"children":null
]
,
"codeName":"注册分局",
"codeValue":"520000021",
"sort":"191",
"id":"152000002100000",
"parentId":"999",
"children":null
]
]
4. 树结构 Model 类
package com.common;
/**
* 下拉项 ModelVO 类
*/
@Data
public class BusinessCodeVO implements Serializable
private static final long serialVersionUID = -7384649360629865178L;
@ApiModelProperty("下拉项名称")
private String codeName;
@ApiModelProperty("下拉项编码")
private String codeValue;
@ApiModelProperty("下拉项排序")
private String sort;
@ApiModelProperty("ID")
private String id;
@ApiModelProperty("父级ID")
private String parentId;
@ApiModelProperty("子级下拉项")
private List<BusinessCodeVO> children;
5. 部门表结构及 SQL 方法补充说明
CREATE TABLE AAF_ORGAN (
ID VARCHAR2 ( 36 CHAR ) NOT NULL,
CREATE_TIME TIMESTAMP ( 6 ),
OPERATE_USER_ID VARCHAR2 ( 36 CHAR ),
STATE VARCHAR2 ( 2 CHAR ),
UPDATE_TIME TIMESTAMP ( 6 ),
ADDRESS VARCHAR2 ( 300 CHAR ),
CODE VARCHAR2 ( 36 BYTE ) NOT NULL,
DISPLAY_ORDER NUMBER ( 19, 0 ),
GRADE VARCHAR2 ( 2 CHAR ) NOT NULL,
MANAGE_DUTY VARCHAR2 ( 2 CHAR ),
NAME VARCHAR2 ( 300 CHAR ) NOT NULL,
ORGAN_TYPE_CODE VARCHAR2 ( 2 CHAR ) NOT NULL,
PARENT_ID VARCHAR2 ( 36 CHAR ),
PRINT_TEXT VARCHAR2 ( 50 CHAR ),
SHORT_NAME VARCHAR2 ( 300 CHAR ),
TEL VARCHAR2 ( 13 CHAR ),
ADMINISTRATIVE_DIVISION_CODE VARCHAR2 ( 20 BYTE ),
CREDIT_CODE VARCHAR2 ( 36 BYTE ),
FUNCTION_CODE VARCHAR2 ( 36 BYTE ),
DEPT_TYPE_CODE VARCHAR2 ( 36 BYTE ),
REGION VARCHAR2 ( 32 BYTE )
) TABLESPACE USERS LOGGING NOCOMPRESS PCTFREE 10 INITRANS 1 STORAGE ( INITIAL 65536 NEXT 1048576 MINEXTENTS 1 MAXEXTENTS 2147483645 BUFFER_POOL DEFAULT ) PARALLEL 1 NOCACHE DISABLE ROW MOVEMENT;
COMMENT ON COLUMN AAF_ORGAN.ADMINISTRATIVE_DIVISION_CODE IS '行政区划代码';
COMMENT ON COLUMN AAF_ORGAN.CREDIT_CODE IS '统一信用代码';
COMMENT ON COLUMN AAF_ORGAN.FUNCTION_CODE IS '职能分类代码';
COMMENT ON COLUMN AAF_ORGAN.DEPT_TYPE_CODE IS '部门类型代码';
COMMENT ON COLUMN AAF_ORGAN.REGION IS '领域';
COMMENT ON COLUMN AAF_ORGAN.ID IS '主键';
COMMENT ON COLUMN AAF_ORGAN.CREATE_TIME IS '创建时间';
COMMENT ON COLUMN AAF_ORGAN.OPERATE_USER_ID IS '操作人ID';
COMMENT ON COLUMN AAF_ORGAN.STATE IS '数据状态';
COMMENT ON COLUMN AAF_ORGAN.UPDATE_TIME IS '更新时间';
COMMENT ON COLUMN AAF_ORGAN.ADDRESS IS '地址';
COMMENT ON COLUMN AAF_ORGAN.CODE IS '编码';
COMMENT ON COLUMN AAF_ORGAN.DISPLAY_ORDER IS '排序';
COMMENT ON COLUMN AAF_ORGAN.GRADE IS 'GRADE';
COMMENT ON COLUMN AAF_ORGAN.MANAGE_DUTY IS 'MANAGE_DUTY';
COMMENT ON COLUMN AAF_ORGAN.NAME IS '机构名称';
COMMENT ON COLUMN AAF_ORGAN.ORGAN_TYPE_CODE IS '机构类型编码';
COMMENT ON COLUMN AAF_ORGAN.PARENT_ID IS '父级ID';
COMMENT ON COLUMN AAF_ORGAN.PRINT_TEXT IS 'PRINT_TEXT';
COMMENT ON COLUMN AAF_ORGAN.SHORT_NAME IS '简短机构名称';
COMMENT ON COLUMN AAF_ORGAN.TEL IS '电话';
COMMENT ON TABLE AAF_ORGAN IS '机构表';
aafOrganManager.getCascadeOrganList() 方法对应的 mapper.xml 如下
<!-- 获取全部机构数据 -->
<select id="getCascadeOrganList" parameterType="java.lang.String" resultType="com.common.BusinessCodeVO">
select
ID ,CODE codeValue,DISPLAY_ORDER sort,NAME codeName,PARENT_ID parentId
from AAF_ORGAN
where STATE = '01'
</select>
以上是关于代码片-下拉树实现的主要内容,如果未能解决你的问题,请参考以下文章