Java递归生成省市区数据结构
@PostMapping("/test")
public ResultBean<List<SysDistrictOutDTO>> queryDistrictInfo() {
return ResultBean.returnResponse(true, ResultCode.SERVICE_OK,commonService.test());
}
@Override
public List<SysDistrictOutDTO> test() {
List<SysDistrictOutDTO> sysDistrictOutDTOList = sdpCommonMapper.queryDistrictInfo();
List<SysDistrictOutDTO> sysDistrictOutDTOTree = sysDistrictOutDTOList.stream().filter(sysDistrictOutDTO ->{
return sysDistrictOutDTO.getFtype().equals("2");
}).map(sysDistrictOutDTO ->{
sysDistrictOutDTO.setChildren(getDistrictChildrenInfo(sysDistrictOutDTO,sysDistrictOutDTOList));
return sysDistrictOutDTO;
}).sorted(Comparator.comparing(SysDistrictOutDTO::getFid)).collect(Collectors.toList());
return sysDistrictOutDTOTree;
}
private List<SysDistrictOutDTO> getDistrictChildrenInfo(SysDistrictOutDTO root, List<SysDistrictOutDTO> all) {
List<SysDistrictOutDTO> children = all.stream().filter(sysDistrictOutDTO -> sysDistrictOutDTO.getFparentid().equals(root.getFid()))
.map(sysDistrictOutDTO -> {
sysDistrictOutDTO.setChildren(getDistrictChildrenInfo(sysDistrictOutDTO,all));
return sysDistrictOutDTO;
}).sorted(Comparator.comparing(SysDistrictOutDTO::getFid)).collect(Collectors.toList());
return children;
}
<select id="queryDistrictInfo" resultType="com.ts.common.dto.SysDistrictOutDTO">
select fid,fparentid,fname as value,fname as label,ftype from test where ftype in ('2','3','4') and fisstop = 0 order by fcode asc
</select>
@Data
@AllArgsConstructor
@NoArgsConstructor
public class SysDistrictOutDTO{
private String label;
private String value;
private String fid;
private String fparentid;
private String ftype;
private List<SysDistrictOutDTO> children;
}
create table test(
fid VARCHAR(255) not null,
fparentid VARCHAR(255) null,
fcode VARCHAR(255) null,
fname VARCHAR(255) null,
ffullname VARCHAR(255) null,
ftype VARCHAR(255) null,
fisstop INT2 null,
constraint PK_test primary key (fid)
);
comment on table test is
'省市区';
comment on column test.fid is
'主键id';
comment on column test.fparentid is
'父id';
comment on column test.fcode is
'代码';
comment on column test.fname is
'名称';
comment on column test.ffullname is
'全称';
comment on column test.ftype is
'类型';
comment on column test.fisstop is
'是否停用,0否 1是';
<FormItem label="不动产地址" prop="ssqs">
<Cascader :disabled="typeCode == '2'" v-model="supplementFormConfigB.ssqs" placeholder="请选择不动产地址" change-on-select :data="CascaderFormatData" filterable transfer />
</FormItem>
formatDatahandle(row){
queryDistrictInfoList().then((res) => {
this.CascaderFormatData = res
console.log(res)
})
},