java 处理mysql获取树形的数据

java 处理mysql获取树形的数据

目录

处理一: 使用对象的方式

TreeModel

TreeModelList

TreeModelListTest

结果:

处理二: Map对象的方式

CommonTree:

CommonTreeTest

总结:

 


在使用mysql数据库,要获取到省市区的数据(以福建的数据为例),返回树型结构的数据。 Mysql 是没办法用oracle的connect by prior了,直接数据库进行查询,因此考虑用java递归遍历,获取到树形的数据。

处理一: 使用对象的方式

TreeModel

public class TreeModel {

    private String name;
    private String code;
    private String parentCode;
    private List children;


    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getCode() {
        return code;
    }

    public void setCode(String code) {
        this.code = code;
    }

    public String getParentCode() {
        return parentCode;
    }

    public void setParentCode(String parentCode) {
        this.parentCode = parentCode;
    }

    public void setChildren(List children) {
        if (children.size() == 0) {
            this.children = new ArrayList<>();
        } else {
            this.children = children;

        }
    }

    @Override
    public String toString() {
        return "{" +
                "name='" + name + '\'' +
                ", code='" + code + '\'' +
                ", parentCode='" + parentCode + '\'' +
                ", children=" + children +
                '}';
    }
}

TreeModelList

public class TreeModelList {

    private List treeNodeList;

    public TreeModelList(List treeNodeList) {
        this.treeNodeList = treeNodeList;
    }

    //建立树形结构
    public List builTree(String value) {
        List treeNodeList = new ArrayList<>();
        List rootNodeList = getRootNode(value);
        for (TreeModel e : ListUtils.emptyIfNull(rootNodeList)) {
            e = buildChilTree(e);
            treeNodeList.add(e);
        }
        return treeNodeList;
    }

    //递归,建立子树形结构
    private TreeModel buildChilTree(TreeModel pNode) {
        List childLeafList = new ArrayList<>();
        for (TreeModel e : ListUtils.emptyIfNull(treeNodeList)) {
            if (StringUtils.equals(e.getParentCode(),
                    pNode.getCode())) {
                childLeafList.add(buildChilTree(e));
            }
        }
        pNode.setChildren(childLeafList);
        return pNode;
    }

    //获取根节点
    private List getRootNode(String value) {
        List rootNodeLists = new ArrayList<>();
        for (TreeModel e : ListUtils.emptyIfNull(treeNodeList)) {
            if (StringUtils.equals(e.getParentCode(), value)) {
                rootNodeLists.add(e);
            }
        }
        return rootNodeLists;
    }

}

TreeModelListTest

public class TreeModelListTest {

    public static void main(String[] args){
//        initData();
        List areaList = initData();
        // 构建树形
        TreeModelList treeModelList = new TreeModelList(areaList);
        List result = treeModelList.builTree( "-1");
        System.out.println("result: "+result);
    }

    private static List initData(){
        String areaInfo = "[{\"code\":59,\"name\":\"福建\",\"parentCode\":-1},{\"code\":591,\"name\":\"福州\",\"parentCode\":59},{\"code\":59101,\"name\":\"鼓楼区\",\"parentCode\":591},{\"code\":59102,\"name\":\"福清\",\"parentCode\":591},{\"code\":59103,\"name\":\"长乐\",\"parentCode\":591},{\"code\":59104,\"name\":\"闽侯\",\"parentCode\":591},{\"code\":59105,\"name\":\"闽清\",\"parentCode\":591},{\"code\":59106,\"name\":\"永泰\",\"parentCode\":591},{\"code\":59107,\"name\":\"平潭\",\"parentCode\":591},{\"code\":59108,\"name\":\"罗源\",\"parentCode\":591},{\"code\":59109,\"name\":\"连江\",\"parentCode\":591},{\"code\":59110,\"name\":\"罗源县\",\"parentCode\":591},{\"code\":59111,\"name\":\"闽清县\",\"parentCode\":591},{\"code\":59112,\"name\":\"永泰县\",\"parentCode\":591},{\"code\":59113,\"name\":\"平潭县\",\"parentCode\":591},{\"code\":59195,\"name\":\"晋安区\",\"parentCode\":591},{\"code\":59196,\"name\":\"马尾区\",\"parentCode\":591},{\"code\":59197,\"name\":\"台江区\",\"parentCode\":591},{\"code\":59198,\"name\":\"金山区\",\"parentCode\":591},{\"code\":592,\"name\":\"厦门\",\"parentCode\":59},{\"code\":59201,\"name\":\"思明区\",\"parentCode\":592},{\"code\":59202,\"name\":\"同安\",\"parentCode\":592},{\"code\":59203,\"name\":\"杏林\",\"parentCode\":592},{\"code\":59204,\"name\":\"海沧区\",\"parentCode\":592},{\"code\":59205,\"name\":\"同安区\",\"parentCode\":592},{\"code\":59206,\"name\":\"翔安区\",\"parentCode\":592},{\"code\":593,\"name\":\"宁德\",\"parentCode\":59},{\"code\":59301,\"name\":\"宁德\",\"parentCode\":593},{\"code\":59302,\"name\":\"古田\",\"parentCode\":593},{\"code\":59303,\"name\":\"屏南\",\"parentCode\":593},{\"code\":59304,\"name\":\"寿宁\",\"parentCode\":593},{\"code\":59305,\"name\":\"周宁\",\"parentCode\":593},{\"code\":59306,\"name\":\"福安\",\"parentCode\":593},{\"code\":59307,\"name\":\"福鼎\",\"parentCode\":593},{\"code\":59308,\"name\":\"霞浦\",\"parentCode\":593},{\"code\":59309,\"name\":\"柘荣\",\"parentCode\":593},{\"code\":59398,\"name\":\"东侨\",\"parentCode\":593},{\"code\":594,\"name\":\"莆田\",\"parentCode\":59},{\"code\":59401,\"name\":\"莆田\",\"parentCode\":594},{\"code\":59402,\"name\":\"仙游\",\"parentCode\":594},{\"code\":59403,\"name\":\"涵江\",\"parentCode\":594},{\"code\":59404,\"name\":\"秀屿\",\"parentCode\":594},{\"code\":59405,\"name\":\"仙游县\",\"parentCode\":594},{\"code\":595,\"name\":\"泉州\",\"parentCode\":59},{\"code\":59501,\"name\":\"泉州\",\"parentCode\":595},{\"code\":59502,\"name\":\"德化\",\"parentCode\":595},{\"code\":59503,\"name\":\"永春\",\"parentCode\":595},{\"code\":59504,\"name\":\"晋江\",\"parentCode\":595},{\"code\":59505,\"name\":\"石狮\",\"parentCode\":595},{\"code\":59506,\"name\":\"南安\",\"parentCode\":595},{\"code\":59507,\"name\":\"惠安\",\"parentCode\":595},{\"code\":59508,\"name\":\"安溪\",\"parentCode\":595},{\"code\":59509,\"name\":\"泉港\",\"parentCode\":595},{\"code\":59510,\"name\":\"永春县\",\"parentCode\":595},{\"code\":59511,\"name\":\"德化县\",\"parentCode\":595},{\"code\":59512,\"name\":\"金门县\",\"parentCode\":595},{\"code\":596,\"name\":\"漳州\",\"parentCode\":59},{\"code\":59601,\"name\":\"漳州\",\"parentCode\":596},{\"code\":59602,\"name\":\"龙海\",\"parentCode\":596},{\"code\":59603,\"name\":\"长泰\",\"parentCode\":596},{\"code\":59604,\"name\":\"南靖\",\"parentCode\":596},{\"code\":59605,\"name\":\"平和\",\"parentCode\":596},{\"code\":59606,\"name\":\"云霄\",\"parentCode\":596},{\"code\":59607,\"name\":\"漳浦\",\"parentCode\":596},{\"code\":59608,\"name\":\"诏安\",\"parentCode\":596},{\"code\":59609,\"name\":\"东山\",\"parentCode\":596},{\"code\":59610,\"name\":\"华安\",\"parentCode\":596},{\"code\":59611,\"name\":\"诏安县\",\"parentCode\":596},{\"code\":59612,\"name\":\"华安县\",\"parentCode\":596},{\"code\":597,\"name\":\"龙岩\",\"parentCode\":59},{\"code\":59701,\"name\":\"龙岩\",\"parentCode\":597},{\"code\":59702,\"name\":\"上杭\",\"parentCode\":597},{\"code\":59703,\"name\":\"武平\",\"parentCode\":597},{\"code\":59704,\"name\":\"永定\",\"parentCode\":597},{\"code\":59705,\"name\":\"长汀\",\"parentCode\":597},{\"code\":59706,\"name\":\"漳平\",\"parentCode\":597},{\"code\":59707,\"name\":\"连城\",\"parentCode\":597},{\"code\":598,\"name\":\"三明\",\"parentCode\":59},{\"code\":59801,\"name\":\"三明\",\"parentCode\":598},{\"code\":59802,\"name\":\"沙县\",\"parentCode\":598},{\"code\":59803,\"name\":\"尤溪\",\"parentCode\":598},{\"code\":59804,\"name\":\"大田\",\"parentCode\":598},{\"code\":59805,\"name\":\"永安\",\"parentCode\":598},{\"code\":59806,\"name\":\"清流\",\"parentCode\":598},{\"code\":59807,\"name\":\"宁化\",\"parentCode\":598},{\"code\":59808,\"name\":\"明溪\",\"parentCode\":598},{\"code\":59809,\"name\":\"建宁\",\"parentCode\":598},{\"code\":59810,\"name\":\"泰宁\",\"parentCode\":598},{\"code\":59811,\"name\":\"将乐\",\"parentCode\":598},{\"code\":59812,\"name\":\"永安市\",\"parentCode\":598},{\"code\":599,\"name\":\"南平\",\"parentCode\":59},{\"code\":59901,\"name\":\"南平\",\"parentCode\":599},{\"code\":59902,\"name\":\"政和\",\"parentCode\":599},{\"code\":59903,\"name\":\"松溪\",\"parentCode\":599},{\"code\":59904,\"name\":\"浦城\",\"parentCode\":599},{\"code\":59905,\"name\":\"建瓯\",\"parentCode\":599},{\"code\":59906,\"name\":\"武夷山\",\"parentCode\":599},{\"code\":59907,\"name\":\"建阳\",\"parentCode\":599},{\"code\":59908,\"name\":\"邵武\",\"parentCode\":599},{\"code\":59909,\"name\":\"顺昌\",\"parentCode\":599},{\"code\":59910,\"name\":\"光泽\",\"parentCode\":599}]";
        JSONArray areaArr = JSONArray.parseArray(areaInfo);

        return ListUtils.emptyIfNull(areaArr).stream().map(e -> (JSONObject) e)
                .map(e -> JSONObject.parseObject(e.toString(), TreeModel.class)).collect(Collectors.toList());

    }

}

结果:

result: [{name='福建', code='59', parentCode='-1', children=[{name='福州', code='591', parentCode='59', children=[{name='鼓楼区', code='59101', parentCode='591', children=[]}, {name='福清', code='59102', parentCode='591', children=[]}, {name='长乐', code='59103', parentCode='591', children=[]}, {name='闽侯', code='59104', parentCode='591', children=[]}, {name='闽清', code='59105', parentCode='591', children=[]}, {name='永泰', code='59106', parentCode='591', children=[]}, {name='平潭', code='59107', parentCode='591', children=[]}, {name='罗源', code='59108', parentCode='591', children=[]}, {name='连江', code='59109', parentCode='591', children=[]}, {name='罗源县', code='59110', parentCode='591', children=[]}, {name='闽清县', code='59111', parentCode='591', children=[]}, {name='永泰县', code='59112', parentCode='591', children=[]}, {name='平潭县', code='59113', parentCode='591', children=[]}, {name='晋安区', code='59195', parentCode='591', children=[]}, {name='马尾区', code='59196', parentCode='591', children=[]}, {name='台江区', code='59197', parentCode='591', children=[]}, {name='金山区', code='59198', parentCode='591', children=[]}]}, {name='厦门', code='592', parentCode='59', children=[{name='思明区', code='59201', parentCode='592', children=[]}, {name='同安', code='59202', parentCode='592', children=[]}, {name='杏林', code='59203', parentCode='592', children=[]}, {name='海沧区', code='59204', parentCode='592', children=[]}, {name='同安区', code='59205', parentCode='592', children=[]}, {name='翔安区', code='59206', parentCode='592', children=[]}]}, {name='宁德', code='593', parentCode='59', children=[{name='宁德', code='59301', parentCode='593', children=[]}, {name='古田', code='59302', parentCode='593', children=[]}, {name='屏南', code='59303', parentCode='593', children=[]}, {name='寿宁', code='59304', parentCode='593', children=[]}, {name='周宁', code='59305', parentCode='593', children=[]}, {name='福安', code='59306', parentCode='593', children=[]}, {name='福鼎', code='59307', parentCode='593', children=[]}, {name='霞浦', code='59308', parentCode='593', children=[]}, {name='柘荣', code='59309', parentCode='593', children=[]}, {name='东侨', code='59398', parentCode='593', children=[]}]}, {name='莆田', code='594', parentCode='59', children=[{name='莆田', code='59401', parentCode='594', children=[]}, {name='仙游', code='59402', parentCode='594', children=[]}, {name='涵江', code='59403', parentCode='594', children=[]}, {name='秀屿', code='59404', parentCode='594', children=[]}, {name='仙游县', code='59405', parentCode='594', children=[]}]}, {name='泉州', code='595', parentCode='59', children=[{name='泉州', code='59501', parentCode='595', children=[]}, {name='德化', code='59502', parentCode='595', children=[]}, {name='永春', code='59503', parentCode='595', children=[]}, {name='晋江', code='59504', parentCode='595', children=[]}, {name='石狮', code='59505', parentCode='595', children=[]}, {name='南安', code='59506', parentCode='595', children=[]}, {name='惠安', code='59507', parentCode='595', children=[]}, {name='安溪', code='59508', parentCode='595', children=[]}, {name='泉港', code='59509', parentCode='595', children=[]}, {name='永春县', code='59510', parentCode='595', children=[]}, {name='德化县', code='59511', parentCode='595', children=[]}, {name='金门县', code='59512', parentCode='595', children=[]}]}, {name='漳州', code='596', parentCode='59', children=[{name='漳州', code='59601', parentCode='596', children=[]}, {name='龙海', code='59602', parentCode='596', children=[]}, {name='长泰', code='59603', parentCode='596', children=[]}, {name='南靖', code='59604', parentCode='596', children=[]}, {name='平和', code='59605', parentCode='596', children=[]}, {name='云霄', code='59606', parentCode='596', children=[]}, {name='漳浦', code='59607', parentCode='596', children=[]}, {name='诏安', code='59608', parentCode='596', children=[]}, {name='东山', code='59609', parentCode='596', children=[]}, {name='华安', code='59610', parentCode='596', children=[]}, {name='诏安县', code='59611', parentCode='596', children=[]}, {name='华安县', code='59612', parentCode='596', children=[]}]}, {name='龙岩', code='597', parentCode='59', children=[{name='龙岩', code='59701', parentCode='597', children=[]}, {name='上杭', code='59702', parentCode='597', children=[]}, {name='武平', code='59703', parentCode='597', children=[]}, {name='永定', code='59704', parentCode='597', children=[]}, {name='长汀', code='59705', parentCode='597', children=[]}, {name='漳平', code='59706', parentCode='597', children=[]}, {name='连城', code='59707', parentCode='597', children=[]}]}, {name='三明', code='598', parentCode='59', children=[{name='三明', code='59801', parentCode='598', children=[]}, {name='沙县', code='59802', parentCode='598', children=[]}, {name='尤溪', code='59803', parentCode='598', children=[]}, {name='大田', code='59804', parentCode='598', children=[]}, {name='永安', code='59805', parentCode='598', children=[]}, {name='清流', code='59806', parentCode='598', children=[]}, {name='宁化', code='59807', parentCode='598', children=[]}, {name='明溪', code='59808', parentCode='598', children=[]}, {name='建宁', code='59809', parentCode='598', children=[]}, {name='泰宁', code='59810', parentCode='598', children=[]}, {name='将乐', code='59811', parentCode='598', children=[]}, {name='永安市', code='59812', parentCode='598', children=[]}]}, {name='南平', code='599', parentCode='59', children=[{name='南平', code='59901', parentCode='599', children=[]}, {name='政和', code='59902', parentCode='599', children=[]}, {name='松溪', code='59903', parentCode='599', children=[]}, {name='浦城', code='59904', parentCode='599', children=[]}, {name='建瓯', code='59905', parentCode='599', children=[]}, {name='武夷山', code='59906', parentCode='599', children=[]}, {name='建阳', code='59907', parentCode='599', children=[]}, {name='邵武', code='59908', parentCode='599', children=[]}, {name='顺昌', code='59909', parentCode='599', children=[]}, {name='光泽', code='59910', parentCode='599', children=[]}]}]}]

处理二: Map对象的方式

CommonTree:

import org.apache.commons.collections4.ListUtils;
import org.apache.commons.collections4.MapUtils;
import org.apache.commons.lang3.StringUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class CommonTree {

    private List> treeNodeList;

    public CommonTree(List> treeNodeList) {
        this.treeNodeList = treeNodeList;
    }

    //建立树形结构
    public List> builTree(String parentTag, String childTag,String rootValue) {
        List> treeNodeList = new ArrayList<>();
        List> rootNodeList = getRootNode(parentTag, rootValue);
        for (Map e : ListUtils.emptyIfNull(rootNodeList)) {
            e = buildChilTree(e, parentTag, childTag);
            treeNodeList.add(e);
        }
        return treeNodeList;
    }

    //递归,建立子树形结构
    private Map buildChilTree(Map pNode, String parentTag, String childTag) {
        List> childLeafList = new ArrayList<>();
        for (Map e : ListUtils.emptyIfNull(treeNodeList)) {
            // 跟上一个进行匹配
            if (StringUtils.equals(MapUtils.getString(e, parentTag),
                    MapUtils.getString(pNode, childTag))) {
                childLeafList.add(buildChilTree(e, parentTag, childTag));
            }
        }
        pNode.put("children", childLeafList);
        return pNode;
    }

    //获取根节点
    private List> getRootNode(String parentTag, String rootValue) {
        List> rootNodeLists = new ArrayList<>();
        for (Map e : ListUtils.emptyIfNull(treeNodeList)) {
            if (StringUtils.equals(MapUtils.getString(e, parentTag), rootValue)) {
                rootNodeLists.add(e);
            }
        }

        return rootNodeLists;
    }


}

CommonTreeTest

public class CommonTreeTest {

    public static void main(String[] args){
        List> areaList = changeFormat(initData());
        // 构建树形
        CommonTree commonTree = new CommonTree(areaList);
        List> result = commonTree.builTree("PARENT_REGION_ID", "REGION_ID", "-1");
        System.out.println("result: "+result);
    }

    private static List> changeFormat(String areaInfo){
        JSONArray areaArr = JSONArray.parseArray(areaInfo);
        return ListUtils.emptyIfNull(areaArr).stream().map(e -> (JSONObject) e)
                .map(e -> (Map)JSONObject.parseObject( e.toJSONString())).collect(Collectors.toList());
    }

    private static String initData(){
        return "[{\"REGION_ID\":59,\"REGION_NAME\":\"福建\",\"PARENT_REGION_ID\":-1},{\"REGION_ID\":591,\"REGION_NAME\":\"福州\",\"PARENT_REGION_ID\":59},{\"REGION_ID\":59101,\"REGION_NAME\":\"鼓楼区\",\"PARENT_REGION_ID\":591},{\"REGION_ID\":59102,\"REGION_NAME\":\"福清\",\"PARENT_REGION_ID\":591},{\"REGION_ID\":59103,\"REGION_NAME\":\"长乐\",\"PARENT_REGION_ID\":591},{\"REGION_ID\":59104,\"REGION_NAME\":\"闽侯\",\"PARENT_REGION_ID\":591},{\"REGION_ID\":59105,\"REGION_NAME\":\"闽清\",\"PARENT_REGION_ID\":591},{\"REGION_ID\":59106,\"REGION_NAME\":\"永泰\",\"PARENT_REGION_ID\":591},{\"REGION_ID\":59107,\"REGION_NAME\":\"平潭\",\"PARENT_REGION_ID\":591},{\"REGION_ID\":59108,\"REGION_NAME\":\"罗源\",\"PARENT_REGION_ID\":591},{\"REGION_ID\":59109,\"REGION_NAME\":\"连江\",\"PARENT_REGION_ID\":591},{\"REGION_ID\":59110,\"REGION_NAME\":\"罗源县\",\"PARENT_REGION_ID\":591},{\"REGION_ID\":59111,\"REGION_NAME\":\"闽清县\",\"PARENT_REGION_ID\":591},{\"REGION_ID\":59112,\"REGION_NAME\":\"永泰县\",\"PARENT_REGION_ID\":591},{\"REGION_ID\":59113,\"REGION_NAME\":\"平潭县\",\"PARENT_REGION_ID\":591},{\"REGION_ID\":59195,\"REGION_NAME\":\"晋安区\",\"PARENT_REGION_ID\":591},{\"REGION_ID\":59196,\"REGION_NAME\":\"马尾区\",\"PARENT_REGION_ID\":591},{\"REGION_ID\":59197,\"REGION_NAME\":\"台江区\",\"PARENT_REGION_ID\":591},{\"REGION_ID\":59198,\"REGION_NAME\":\"金山区\",\"PARENT_REGION_ID\":591},{\"REGION_ID\":592,\"REGION_NAME\":\"厦门\",\"PARENT_REGION_ID\":59},{\"REGION_ID\":59201,\"REGION_NAME\":\"思明区\",\"PARENT_REGION_ID\":592},{\"REGION_ID\":59202,\"REGION_NAME\":\"同安\",\"PARENT_REGION_ID\":592},{\"REGION_ID\":59203,\"REGION_NAME\":\"杏林\",\"PARENT_REGION_ID\":592},{\"REGION_ID\":59204,\"REGION_NAME\":\"海沧区\",\"PARENT_REGION_ID\":592},{\"REGION_ID\":59205,\"REGION_NAME\":\"同安区\",\"PARENT_REGION_ID\":592},{\"REGION_ID\":59206,\"REGION_NAME\":\"翔安区\",\"PARENT_REGION_ID\":592},{\"REGION_ID\":593,\"REGION_NAME\":\"宁德\",\"PARENT_REGION_ID\":59},{\"REGION_ID\":59301,\"REGION_NAME\":\"宁德\",\"PARENT_REGION_ID\":593},{\"REGION_ID\":59302,\"REGION_NAME\":\"古田\",\"PARENT_REGION_ID\":593},{\"REGION_ID\":59303,\"REGION_NAME\":\"屏南\",\"PARENT_REGION_ID\":593},{\"REGION_ID\":59304,\"REGION_NAME\":\"寿宁\",\"PARENT_REGION_ID\":593},{\"REGION_ID\":59305,\"REGION_NAME\":\"周宁\",\"PARENT_REGION_ID\":593},{\"REGION_ID\":59306,\"REGION_NAME\":\"福安\",\"PARENT_REGION_ID\":593},{\"REGION_ID\":59307,\"REGION_NAME\":\"福鼎\",\"PARENT_REGION_ID\":593},{\"REGION_ID\":59308,\"REGION_NAME\":\"霞浦\",\"PARENT_REGION_ID\":593},{\"REGION_ID\":59309,\"REGION_NAME\":\"柘荣\",\"PARENT_REGION_ID\":593},{\"REGION_ID\":59398,\"REGION_NAME\":\"东侨\",\"PARENT_REGION_ID\":593},{\"REGION_ID\":594,\"REGION_NAME\":\"莆田\",\"PARENT_REGION_ID\":59},{\"REGION_ID\":59401,\"REGION_NAME\":\"莆田\",\"PARENT_REGION_ID\":594},{\"REGION_ID\":59402,\"REGION_NAME\":\"仙游\",\"PARENT_REGION_ID\":594},{\"REGION_ID\":59403,\"REGION_NAME\":\"涵江\",\"PARENT_REGION_ID\":594},{\"REGION_ID\":59404,\"REGION_NAME\":\"秀屿\",\"PARENT_REGION_ID\":594},{\"REGION_ID\":59405,\"REGION_NAME\":\"仙游县\",\"PARENT_REGION_ID\":594},{\"REGION_ID\":595,\"REGION_NAME\":\"泉州\",\"PARENT_REGION_ID\":59},{\"REGION_ID\":59501,\"REGION_NAME\":\"泉州\",\"PARENT_REGION_ID\":595},{\"REGION_ID\":59502,\"REGION_NAME\":\"德化\",\"PARENT_REGION_ID\":595},{\"REGION_ID\":59503,\"REGION_NAME\":\"永春\",\"PARENT_REGION_ID\":595},{\"REGION_ID\":59504,\"REGION_NAME\":\"晋江\",\"PARENT_REGION_ID\":595},{\"REGION_ID\":59505,\"REGION_NAME\":\"石狮\",\"PARENT_REGION_ID\":595},{\"REGION_ID\":59506,\"REGION_NAME\":\"南安\",\"PARENT_REGION_ID\":595},{\"REGION_ID\":59507,\"REGION_NAME\":\"惠安\",\"PARENT_REGION_ID\":595},{\"REGION_ID\":59508,\"REGION_NAME\":\"安溪\",\"PARENT_REGION_ID\":595},{\"REGION_ID\":59509,\"REGION_NAME\":\"泉港\",\"PARENT_REGION_ID\":595},{\"REGION_ID\":59510,\"REGION_NAME\":\"永春县\",\"PARENT_REGION_ID\":595},{\"REGION_ID\":59511,\"REGION_NAME\":\"德化县\",\"PARENT_REGION_ID\":595},{\"REGION_ID\":59512,\"REGION_NAME\":\"金门县\",\"PARENT_REGION_ID\":595},{\"REGION_ID\":596,\"REGION_NAME\":\"漳州\",\"PARENT_REGION_ID\":59},{\"REGION_ID\":59601,\"REGION_NAME\":\"漳州\",\"PARENT_REGION_ID\":596},{\"REGION_ID\":59602,\"REGION_NAME\":\"龙海\",\"PARENT_REGION_ID\":596},{\"REGION_ID\":59603,\"REGION_NAME\":\"长泰\",\"PARENT_REGION_ID\":596},{\"REGION_ID\":59604,\"REGION_NAME\":\"南靖\",\"PARENT_REGION_ID\":596},{\"REGION_ID\":59605,\"REGION_NAME\":\"平和\",\"PARENT_REGION_ID\":596},{\"REGION_ID\":59606,\"REGION_NAME\":\"云霄\",\"PARENT_REGION_ID\":596},{\"REGION_ID\":59607,\"REGION_NAME\":\"漳浦\",\"PARENT_REGION_ID\":596},{\"REGION_ID\":59608,\"REGION_NAME\":\"诏安\",\"PARENT_REGION_ID\":596},{\"REGION_ID\":59609,\"REGION_NAME\":\"东山\",\"PARENT_REGION_ID\":596},{\"REGION_ID\":59610,\"REGION_NAME\":\"华安\",\"PARENT_REGION_ID\":596},{\"REGION_ID\":59611,\"REGION_NAME\":\"诏安县\",\"PARENT_REGION_ID\":596},{\"REGION_ID\":59612,\"REGION_NAME\":\"华安县\",\"PARENT_REGION_ID\":596},{\"REGION_ID\":597,\"REGION_NAME\":\"龙岩\",\"PARENT_REGION_ID\":59},{\"REGION_ID\":59701,\"REGION_NAME\":\"龙岩\",\"PARENT_REGION_ID\":597},{\"REGION_ID\":59702,\"REGION_NAME\":\"上杭\",\"PARENT_REGION_ID\":597},{\"REGION_ID\":59703,\"REGION_NAME\":\"武平\",\"PARENT_REGION_ID\":597},{\"REGION_ID\":59704,\"REGION_NAME\":\"永定\",\"PARENT_REGION_ID\":597},{\"REGION_ID\":59705,\"REGION_NAME\":\"长汀\",\"PARENT_REGION_ID\":597},{\"REGION_ID\":59706,\"REGION_NAME\":\"漳平\",\"PARENT_REGION_ID\":597},{\"REGION_ID\":59707,\"REGION_NAME\":\"连城\",\"PARENT_REGION_ID\":597},{\"REGION_ID\":598,\"REGION_NAME\":\"三明\",\"PARENT_REGION_ID\":59},{\"REGION_ID\":59801,\"REGION_NAME\":\"三明\",\"PARENT_REGION_ID\":598},{\"REGION_ID\":59802,\"REGION_NAME\":\"沙县\",\"PARENT_REGION_ID\":598},{\"REGION_ID\":59803,\"REGION_NAME\":\"尤溪\",\"PARENT_REGION_ID\":598},{\"REGION_ID\":59804,\"REGION_NAME\":\"大田\",\"PARENT_REGION_ID\":598},{\"REGION_ID\":59805,\"REGION_NAME\":\"永安\",\"PARENT_REGION_ID\":598},{\"REGION_ID\":59806,\"REGION_NAME\":\"清流\",\"PARENT_REGION_ID\":598},{\"REGION_ID\":59807,\"REGION_NAME\":\"宁化\",\"PARENT_REGION_ID\":598},{\"REGION_ID\":59808,\"REGION_NAME\":\"明溪\",\"PARENT_REGION_ID\":598},{\"REGION_ID\":59809,\"REGION_NAME\":\"建宁\",\"PARENT_REGION_ID\":598},{\"REGION_ID\":59810,\"REGION_NAME\":\"泰宁\",\"PARENT_REGION_ID\":598},{\"REGION_ID\":59811,\"REGION_NAME\":\"将乐\",\"PARENT_REGION_ID\":598},{\"REGION_ID\":59812,\"REGION_NAME\":\"永安市\",\"PARENT_REGION_ID\":598},{\"REGION_ID\":599,\"REGION_NAME\":\"南平\",\"PARENT_REGION_ID\":59},{\"REGION_ID\":59901,\"REGION_NAME\":\"南平\",\"PARENT_REGION_ID\":599},{\"REGION_ID\":59902,\"REGION_NAME\":\"政和\",\"PARENT_REGION_ID\":599},{\"REGION_ID\":59903,\"REGION_NAME\":\"松溪\",\"PARENT_REGION_ID\":599},{\"REGION_ID\":59904,\"REGION_NAME\":\"浦城\",\"PARENT_REGION_ID\":599},{\"REGION_ID\":59905,\"REGION_NAME\":\"建瓯\",\"PARENT_REGION_ID\":599},{\"REGION_ID\":59906,\"REGION_NAME\":\"武夷山\",\"PARENT_REGION_ID\":599},{\"REGION_ID\":59907,\"REGION_NAME\":\"建阳\",\"PARENT_REGION_ID\":599},{\"REGION_ID\":59908,\"REGION_NAME\":\"邵武\",\"PARENT_REGION_ID\":599},{\"REGION_ID\":59909,\"REGION_NAME\":\"顺昌\",\"PARENT_REGION_ID\":599},{\"REGION_ID\":59910,\"REGION_NAME\":\"光泽\",\"PARENT_REGION_ID\":599}]";
    }


}


}

总结:

     使用对象的方式处理比较简洁,也好理解,但是不怎么通用,数据的类型比较多。第二种会更通用些。这边主要是用递归的方法进行遍历。

     更简易处理树形数据的方法《java computeIfAbsent 处理mysql获取树形的数据 》

    关于递归的理解请看 另一篇文章 《递归的理解和例子》

  

你可能感兴趣的:(java,mysql,java,递归算法)