1、我们先来看看处理后的格式:
一:第一步(创建实体类):提示:shortname是简称可以忽略
/**
* 中国 地区表
* @author ruoyi
*/
@Data
public class TableArea {
private static final long serialVersionUID = 1L;
/** ID */
private Long id;
/** 父id */
private Long pid;
/** 地区名称*/
private String name;
/** 子分类 */
private List<TableArea> children = new ArrayList<TableArea>();
}
二、在通用工具类common创建(Treeselect树结构实体类)
package com.ruoyi.common.core.domain;
import java.io.Serializable;
import java.util.List;
import java.util.stream.Collectors;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.ruoyi.common.core.domain.entity.region.TableArea;
/**
* Treeselect树结构实体类
*
* @author ruoyi
*/
public class TreeSelect implements Serializable
{
private static final long serialVersionUID = 1L;
/** 节点ID */
private Long id;
/** 节点名称 */
private String label;
/** 子节点 */
@JsonInclude(JsonInclude.Include.NON_EMPTY)
private List<TreeSelect> children;
public TreeSelect()
{
}
public TreeSelect(TableArea tableArea)
{
this.id = tableArea.getId();
this.label = tableArea.getName();
this.children = tableArea.getChildren().stream().map(TreeSelect::new).collect(Collectors.toList());
}
public Long getId()
{
return id;
}
public void setId(Long id)
{
this.id = id;
}
public String getLabel()
{
return label;
}
public void setLabel(String label)
{
this.label = label;
}
public List<TreeSelect> getChildren()
{
return children;
}
public void setChildren(List<TreeSelect> children)
{
this.children = children;
}
}
三、TableAreaMapper.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<!-- 将下列SQL语句绑定 dao层对应接口 -->
<mapper namespace="com.ruoyi.enterprise.mapper.TableAreaMapper">
<resultMap type="TableArea" id="TableAreaResult">
<result property="id" column="id" />
<result property="pid" column="pid" />
<result property="shortname" column="shortname" />
<result property="name" column="name" />
<result property="mergename" column="mergename" />
<result property="level" column="level" />
<result property="pinyin" column="pinyin" />
<result property="code" column="code" />
<result property="zip" column="zip" />
<result property="first" column="first" />
<result property="lng" column="lng" />
<result property="lat" column="lat" />
</resultMap>
<sql id="selectTableAreaVo">
select id, pid, shortname, `name`, mergename, `level`, pinyin, code, zip, `first`, lng, lat from table_area
</sql>
<select id="selectTableAreaList" resultMap="TableAreaResult">
<include refid="selectTableAreaVo"/> //引入<sql id="selectTableAreaVo"> 里的查询
</select> </mapper>
四、地区Service接口 (忽略mapper接口)
package com.ruoyi.enterprise.service;
import com.ruoyi.common.core.domain.TreeSelect;
import com.ruoyi.common.core.domain.entity.region.TableArea;
import java.util.List;
/**
* 地区Service接口
*
* @author ruoyi
* @date 2022-05-27
*/
public interface ITableAreaService {
/**
* 查询地区列表集合
* @return
*/
public List<TableArea> selectTableAreaList();
/**
* 构建前端所需要树结构
*
* @param tableAreas 地区列表
* @return 树结构列表
*/
public List<TableArea> buildTableAreaTree(List<TableArea> tableAreas);
/**
* 构建前端所需要下拉树结构
*
* @param tableAreas 地区列表
* @return 下拉树结构列表
*/
public List<TreeSelect> buildTableAreaTreeSelect(List<TableArea> tableAreas);
}
五、地区Service业务层处理:
package com.ruoyi.enterprise.service.impl;
import com.ruoyi.common.core.domain.TreeSelect;
import com.ruoyi.common.core.domain.entity.region.TableArea;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.enterprise.mapper.TableAreaMapper;
import com.ruoyi.enterprise.service.ITableAreaService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.stream.Collectors;
/**
* 地区Service业务层处理
* @author ruoyi
* @date 2022-05-27
*/
@Service
public class TableAreaServiceImpl implements ITableAreaService {
private static final Logger log = LoggerFactory.getLogger(TableAreaServiceImpl.class);
@Autowired
private TableAreaMapper tableAreaMapper;
/**
* 查询地区列表集合
* @return
*/
@Override
public List<TableArea> selectTableAreaList() {
return tableAreaMapper.selectTableAreaList();
}
/**
* 构建前端所需要树结构
*
* @param tableAreas 地区列表
* @return 树结构列表
*/
@Override
public List<TableArea> buildTableAreaTree(List<TableArea> tableAreas) {
List<TableArea> returnList = new ArrayList<>();
List<Long> tempList = new ArrayList<>();
for (TableArea a : tableAreas)
{
tempList.add(a.getId());
}
for (Iterator<TableArea> iterator = tableAreas.iterator(); iterator.hasNext();)
{
TableArea tableArea = (TableArea) iterator.next();
//如果是顶级节点,遍历该父节点的所有子节点
if (!tempList.contains(tableArea.getPid())){
recursionDFn(tableAreas, tableArea);
returnList.add(tableArea);
}
}
if (returnList.isEmpty())
{
returnList = tableAreas;
}
return returnList;
}
/**
* 构建前端所需要下拉树结构
*
* @param tableAreas 地区列表
* @return 下拉树结构列表
*/
@Override
public List<TreeSelect> buildTableAreaTreeSelect(List<TableArea> tableAreas) {
List<TableArea> tableAreaTees = buildTableAreaTree(tableAreas);
return tableAreaTees.stream().map(TreeSelect::new).collect(Collectors.toList());
}
/**
* 递归列表
* @param list
* @param t
*/
private void recursionDFn(List<TableArea> list, TableArea t){
//得到子节点列表
List<TableArea> tableAreaList = getChildList(list, t);
t.setChildren(tableAreaList);
for(TableArea tChild : tableAreaList){
if (hasChild(list,tChild)){
recursionDFn(list, tChild);
}
}
}
private List<TableArea> getChildList(List<TableArea> list, TableArea t){
List<TableArea> tList = new ArrayList<>();
Iterator<TableArea> it = list.iterator();
while (it.hasNext()){
TableArea n = (TableArea) it.next();
if (StringUtils.isNotNull(n.getPid()) && n.getPid().longValue() == t.getId().longValue()){
tList.add(n);
}
}
return tList;
}
private boolean hasChild(List<TableArea> list, TableArea t){
return getChildList(list, t).size() > 0;
}
}
六、地区控制层
package com.ruoyi.enterprise.controller;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.region.TableArea;
import com.ruoyi.enterprise.service.ITableAreaService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
/**
* (中国)地区信息控制层
*/
@Api(tags = "中国地区==》接口")
@RestController
@RequestMapping("/tableArea")
public class TableAreaController extends BaseController {
@Autowired
private ITableAreaService tableAreaService;
/**
* 获取地区下拉树列表
* @return
*/
@ApiOperation("地区下拉树接口")
@GetMapping("/treeSelect")
public AjaxResult treeSelect(){
List<TableArea> tableAreas = tableAreaService.selectTableAreaList();
return AjaxResult.success(tableAreaService.buildTableAreaTreeSelect(tableAreas));
}
}