由于业务需要返回一个树形结构,并且实现搜索功能,搜索子类时要显示父类信息
java
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
@Data
@ApiModel(value = "获取查询条件请求")
public class SearchConditionRequest {
    @ApiModelProperty(value = "查询条件名称")
    private String  text;
}
java
 @ApiOperation(value = "获取查询条件树形列表")
    @PostMapping("getSearchConditionListTree")
    public Result<List<SearchConditionEntity>> getRecSearchConditionListTree(@RequestBody SearchConditionRequest request) {
        List<SearchConditionEntity> list = iSearchConditionService.selectSearchConditionList(request);
        List<SearchConditionEntity> SearchConditionEntityListTree = new ArrayList<>();
        //第一步获取顶层节点
        List<RecSearchConditionEntity> top = list.stream()
                .filter(pa -> "0".equals(pa.getParentId()) || pa.getParentId()==null).collect(Collectors.toList());
        if (top != null && top.size() > 0) {
            top.forEach(p -> {
                searchConditionEntity searchConditionEntity = p;
                this.getChildrenList(list, searchConditionEntity);
                searchConditionEntityListTree.add(SearchConditionEntity);
            });
            return Result.success(SearchConditionEntityListTree);
        }
        return Result.fail("500", "没有结果");
    }
    private void getChildrenList(List<SearchConditionEntity> list, SearchConditionEntity parent) {
        List<searchConditionEntity> children = list.stream()
                .filter(pa -> parent.getId().equals(pa.getParentId())).collect(Collectors.toList());
        if (children != null && children.size() > 0) {
            children.forEach(p -> {
                this.getChildrenList(list, p);
            });
            parent.setChildren(children);
        }
        return;
    }
使用递归的方式,给树形添加上下级
java
@Data
@EqualsAndHashCode(callSuper = false)
@Accessors(chain = true)
@TableName("SEARCH_CONDITION")
@ApiModel(value="SearchConditionEntity对象", description="")
public class SearchConditionEntity implements Serializable {
private static final long serialVersionUID=1L;
    @ApiModelProperty(value = "主键ID")
    private String id;
    @ApiModelProperty(value = "条件明")
    private String text;
    @ApiModelProperty(value = "条件字段来源表")
    private String source;
    @ApiModelProperty(value = "条件字段")
    private String field;
    @ApiModelProperty(value = "条件值类型")
    private String valueType;
    @ApiModelProperty(value = "上级ID")
    private String parentId;
    @ApiModelProperty(value = "条件选项")
    private String options;
    @ApiModelProperty(value="是否为目录 1:是 0:否")
    private Integer isFolder;
    @ApiModelProperty(value = "子节点")
    @TableField(exist = false)
    private List<RecSearchConditionEntity> children;
    @ApiModelProperty(value = "上级节点名称")
    private String parentName;
}
sql
    <select id="selectSearchConditionList" parameterType="com.ewell.medqc.rec.request.rec.searchConditionRequest"
            resultType="com.ewell.medqc.rec.entity.rec.searchConditionEntity">
        select temp.*,
        a.text as parent_name
        from (select distinct t.*
        from rec_search_condition t
        start with id in (select id
        from rec_search_condition b
        <where>
            b.TEXT like '%'||#{text}||'%')
        </where>
        connect by id = prior parent_id) temp,
        rec_search_condition a
        where temp.parent_id = a.id(+)
    </select>
还有个不实现搜索版本
sql
    <select id="selectSearchConditionList" parameterType="com.ewell.medqc.rec.request.rec.searchConditionRequest"
            resultType="com.ewell.medqc.rec.entity.rec.searchConditionEntity">
        select t.*, a.text as parent_name
        from   rec_search_condition t,rec_search_condition a
        <where>
            t.parent_id =a.id(+)
        </where>
    </select>
sql
-- Create table
create table SEARCH_CONDITION
(
  id         VARCHAR2(50) not null,
  text       VARCHAR2(50),
  source     VARCHAR2(50),
  field      VARCHAR2(50),
  value_type VARCHAR2(20),
  parent_id  VARCHAR2(50),
  options    VARCHAR2(200),
  is_folder  NUMBER default 0
)
tablespace system
  pctfree 10
  initrans 1
  maxtrans 255
  storage
  (
    initial 64K
    next 1M
    minextents 1
    maxextents unlimited
  );
-- Add comments to the columns 
comment on column SEARCH_CONDITION.id
  is '主键ID';
comment on column SEARCH_CONDITION.text
  is '条件明';
comment on column SEARCH_CONDITION.source
  is '条件字段来源表';
comment on column SEARCH_CONDITION.field
  is '条件字段';
comment on column SEARCH_CONDITION.value_type
  is '条件值类型';
comment on column SEARCH_CONDITION.parent_id
  is '上级ID';
comment on column SEARCH_CONDITION.options
  is '条件选项';
comment on column SEARCH_CONDITION.is_folder
  is '是否为目录 1:是 0:否';
本文作者:Weee
本文链接:
版权声明:本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!