使用递归实现地址数据菜单

2023-10-20 10:54:20 浏览数 (1)

跟网上的不同的是,我这种方式是以时间换空间的做法,不会一次性把数据全查出来再慢慢遍历,而是每次查询是否存在子级,有就递归下去查。 数据量大时,查库的次数比较多,数据量少时对数据库查询次数少,压力较小,但是多次查询不会出现一次查询数据量很大卡住的情况。

思路

  1. 先获取一级菜单,对每个一级菜单设子区域。
  2. 如果子区域还有子区域,就递归查询,直到查不到子区域返回。
  3. 递归的核心就是在方法中设置一个返回条件,防止无限递归下去。
代码语言:javascript复制
 * @author liukai
 * @since 2019/6/12 16:14.
 */
public class SignTree {

    private static RegionDao regionDao = new RegionDao();

    public static void main(String[] args) {

        //1. 先拿到第一级地址
        //2. 递归查每一级下面的子级:
        //      如果子级下面,还有子级就递归
        //      如果子级下面,没有子级就返回退出递归
        List<RegionBean> firstRegion = regionDao.queryParentId(0);
        for (RegionBean regionBean : firstRegion) {
            //设置子区域
            regionBean.setRegionBeanList(setChild(regionBean.getId()));
        }
        String jsonObject = JSONObject.toJSONString(firstRegion);

        System.out.println(jsonObject);
    }

    /**
     * 递归查询所有子级
     * @param parentId
     * @return
     */
    private static List<RegionBean> setChild(int parentId) {
        List<RegionBean> childList = new ArrayList<>();
        List<RegionBean> childTemp = regionDao.queryParentId(parentId);
        if (childTemp != null && childTemp.size() > 0) {
            for (RegionBean regionBean : childTemp) {
                childList.add(regionBean);
                regionBean.setRegionBeanList(setChild(regionBean.getId()));
            }
        } else {
            return childList;
        }
        return childList;
    }




}

class RegionDao {

    /**
     * 查询父级
     * @param parentId
     * @return
     */
    public List<RegionBean> queryParentId(int parentId) {
        List<RegionBean> regionBeans = dataBaseRegion();
        List<RegionBean> firstRegion = new ArrayList<>();
        for (RegionBean regionBean : regionBeans) {
            if (regionBean.getParentId() == parentId) {
                firstRegion.add(regionBean);
            }
        }
        return firstRegion;
    }

    public static List<RegionBean> dataBaseRegion() {
        List<RegionBean> regionBeans = Arrays.asList(
                new RegionBean(2, "北京市", "北京市", 0, 1),
                new RegionBean(22, "天津市", "天津市", 0, 2),
                new RegionBean(783, "上海市", "上海市", 0, 3),
                new RegionBean(457, "辽宁省", "辽宁省", 0, 8),
                new RegionBean(458, "沈阳市", "辽宁省-沈阳市", 457, 1),
                new RegionBean(472, "铁岭市", "辽宁省-铁岭市", 457, 2),
                new RegionBean(480, "大连市", "辽宁省-大连市", 457, 3),
                new RegionBean(491, "鞍山市", "辽宁省-鞍山市", 457, 4),
                new RegionBean(499, "抚顺市", "辽宁省-抚顺市", 457, 5),
                new RegionBean(507, "本溪市", "辽宁省-本溪市", 457, 6),
                new RegionBean(514, "丹东市", "辽宁省-丹东市", 457, 7),
                new RegionBean(521, "锦州市", "辽宁省-锦州市", 457, 8),
                new RegionBean(529, "营口市", "辽宁省-营口市", 457, 9),
                new RegionBean(536, "阜新市", "辽宁省-阜新市", 457, 10),
                new RegionBean(544, "辽阳市", "辽宁省-辽阳市", 457, 11),
                new RegionBean(552, "朝阳市", "辽宁省-朝阳市", 457, 12),
                new RegionBean(560, "盘锦市", "辽宁省-盘锦市", 457, 13),
                new RegionBean(561, "三级数据", "辽宁省-三级数据", 560, 13),
                new RegionBean(562, "三级数据", "辽宁省-三级数据", 560, 13),
                new RegionBean(563, "三级数据", "辽宁省-三级数据", 560, 13),
                new RegionBean(564, "三级数据", "辽宁省-三级数据", 560, 13),
                new RegionBean(565, "三级数据", "辽宁省-三级数据", 560, 13),
                new RegionBean(566, "三级数据", "辽宁省-三级数据", 560, 13),
                new RegionBean(666, "四级数据", "辽宁省-四级数据", 566, 13)
        );
        return regionBeans;
    }

}
代码语言:javascript复制

public class RegionBean implements java.io.Serializable {

  private static final long serialVersionUID = 1L;

  private int id;

  private String label;

  private String fullLabel;

  private int parentId;

  private int sortId;

  private List<RegionBean> regionBeanList = new ArrayList<>();

结果

代码语言:javascript复制
[{"fullLabel":"北京市","id":2,"label":"北京市","parentId":0,"regionBeanList":[],"sortId":1},{"fullLabel":"天津市","id":22,"label":"天津市","parentId":0,"regionBeanList":[],"sortId":2},{"fullLabel":"上海市","id":783,"label":"上海市","parentId":0,"regionBeanList":[],"sortId":3},{"fullLabel":"辽宁省","id":457,"label":"辽宁省","parentId":0,"regionBeanList":[{"fullLabel":"辽宁省-沈阳市","id":458,"label":"沈阳市","parentId":457,"regionBeanList":[],"sortId":1},{"fullLabel":"辽宁省-铁岭市","id":472,"label":"铁岭市","parentId":457,"regionBeanList":[],"sortId":2},{"fullLabel":"辽宁省-大连市","id":480,"label":"大连市","parentId":457,"regionBeanList":[],"sortId":3},{"fullLabel":"辽宁省-鞍山市","id":491,"label":"鞍山市","parentId":457,"regionBeanList":[],"sortId":4},{"fullLabel":"辽宁省-抚顺市","id":499,"label":"抚顺市","parentId":457,"regionBeanList":[],"sortId":5},{"fullLabel":"辽宁省-本溪市","id":507,"label":"本溪市","parentId":457,"regionBeanList":[],"sortId":6},{"fullLabel":"辽宁省-丹东市","id":514,"label":"丹东市","parentId":457,"regionBeanList":[],"sortId":7},{"fullLabel":"辽宁省-锦州市","id":521,"label":"锦州市","parentId":457,"regionBeanList":[],"sortId":8},{"fullLabel":"辽宁省-营口市","id":529,"label":"营口市","parentId":457,"regionBeanList":[],"sortId":9},{"fullLabel":"辽宁省-阜新市","id":536,"label":"阜新市","parentId":457,"regionBeanList":[],"sortId":10},{"fullLabel":"辽宁省-辽阳市","id":544,"label":"辽阳市","parentId":457,"regionBeanList":[],"sortId":11},{"fullLabel":"辽宁省-朝阳市","id":552,"label":"朝阳市","parentId":457,"regionBeanList":[],"sortId":12},{"fullLabel":"辽宁省-盘锦市","id":560,"label":"盘锦市","parentId":457,"regionBeanList":[{"fullLabel":"辽宁省-三级数据","id":561,"label":"三级数据","parentId":560,"regionBeanList":[],"sortId":13},{"fullLabel":"辽宁省-三级数据","id":562,"label":"三级数据","parentId":560,"regionBeanList":[],"sortId":13},{"fullLabel":"辽宁省-三级数据","id":563,"label":"三级数据","parentId":560,"regionBeanList":[],"sortId":13},{"fullLabel":"辽宁省-三级数据","id":564,"label":"三级数据","parentId":560,"regionBeanList":[],"sortId":13},{"fullLabel":"辽宁省-三级数据","id":565,"label":"三级数据","parentId":560,"regionBeanList":[],"sortId":13},{"fullLabel":"辽宁省-三级数据","id":566,"label":"三级数据","parentId":560,"regionBeanList":[{"fullLabel":"辽宁省-四级数据","id":666,"label":"四级数据","parentId":566,"regionBeanList":[],"sortId":13}],"sortId":13}],"sortId":13}],"sortId":8}]

0 人点赞