龙空技术网

Java使用迭代器生成树形结构

Java点滴 1148

前言:

现在同学们对“枣庄java培训”大体比较珍视,看官们都需要知道一些“枣庄java培训”的相关知识。那么小编也在网络上汇集了一些关于“枣庄java培训””的相关知识,希望咱们能喜欢,同学们快快来学习一下吧!

在我们开发接口的时候,经常返给前端下拉列表,特别是以树形结构的类型进行返回。

我们以省市区举例。

首先,创建实体类:

@Datapublic class CitysInfo {    /**     * ID     */    private int id;    /**     * 父ID     */    private int pid;    /**     * 名称     */    private String cityName;    /**     * 类型     */    private int type;    /**     * 子级城市列表     */    List<CitysInfo> chilren;}

然后,对应的是持久层

@Mapperpublic interface CityInfoMapper {    List<CitysInfo> getCity();}
<mapper namespace="com.example.demo.mapper.CityInfoMapper">    <resultMap id="BaseResultMap" type="com.example.demo.entity.po.CitysInfo">        <result column="id" property="id" />        <result column="pid" property="pid" />        <result column="cityname" property="cityName" />        <result column="type" property="type" />    </resultMap>    <select id="getCity" resultMap="BaseResultMap">        SELECT * FROM t_city    </select></mapper>

接着service层

public interface CityService {    /**     * 获取城市列表     * @return     */    List<CitysInfo> getCity();}
@Servicepublic class CityServiceImpl implements CityService {    @Resource    private CityInfoMapper cityInfoMapper;    @Override    public List<CitysInfo> getCity() {        //  所有地区        List<CitysInfo> list = cityInfoMapper.getCity();       //	pid 可根据自己的业务进行修改        List<CitysInfo> jsonByList = getTreeByList(list, 1);        return jsonByList;    }    //  递归获取树    private List<CitysInfo> getTreeByList(List<CitysInfo> list, int pid) {        List<CitysInfo> newList = new ArrayList<>();        for (Iterator iterator = list.iterator(); iterator.hasNext();) {            CitysInfo city = (CitysInfo) iterator.next();            if (city.getPid() == pid) {                CitysInfo treeCity = new CitysInfo();                treeCity.setId(city.getId());                treeCity.setPid(city.getPid());                treeCity.setCityName(city.getCityName());                treeCity.setType(city.getType());               	//	进行递归                List<CitysInfo> child = getTreeByList(list, city.getId());                treeCity.setChilren(child);                newList.add(treeCity);            }        }        return newList;    }}

最后,controller层

   @Resource    private CityService cityService;    @GetMapping(value = "/getCity",produces = MediaType.APPLICATION_JSON_UTF8)    public ResponseResult getCity() {        List<CitysInfo> city = cityService.getCity();        return ResponseResult.success(city);    }

为了,方便大家测试,数据库表结构如下

SET NAMES utf8mb4;SET FOREIGN_KEY_CHECKS = 0;-- ------------------------------ Table structure for t_city-- ----------------------------DROP TABLE IF EXISTS `t_city`;CREATE TABLE `t_city`  (  `id` int(0) NOT NULL DEFAULT 0,  `pid` int(0) NULL DEFAULT NULL,  `cityname` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,  `type` int(0) NULL DEFAULT NULL,  PRIMARY KEY (`id`) USING BTREE) ENGINE = InnoDB CHARACTER SET = utf8 COLLATE = utf8_unicode_ci ROW_FORMAT = Dynamic;SET FOREIGN_KEY_CHECKS = 1;

导入数据,方便测试

INSERT INTO `t_city`(`id`, `pid`, `cityname`, `type`) VALUES (22, 1, '山东', 1);INSERT INTO `t_city`(`id`, `pid`, `cityname`, `type`) VALUES (283, 22, '济南', 2);INSERT INTO `t_city`(`id`, `pid`, `cityname`, `type`) VALUES (284, 22, '青岛', 2);INSERT INTO `t_city`(`id`, `pid`, `cityname`, `type`) VALUES (285, 22, '滨州', 2);INSERT INTO `t_city`(`id`, `pid`, `cityname`, `type`) VALUES (286, 22, '德州', 2);INSERT INTO `t_city`(`id`, `pid`, `cityname`, `type`) VALUES (287, 22, '东营', 2);INSERT INTO `t_city`(`id`, `pid`, `cityname`, `type`) VALUES (288, 22, '菏泽', 2);INSERT INTO `t_city`(`id`, `pid`, `cityname`, `type`) VALUES (289, 22, '济宁', 2);INSERT INTO `t_city`(`id`, `pid`, `cityname`, `type`) VALUES (290, 22, '莱芜', 2);INSERT INTO `t_city`(`id`, `pid`, `cityname`, `type`) VALUES (291, 22, '聊城', 2);INSERT INTO `t_city`(`id`, `pid`, `cityname`, `type`) VALUES (292, 22, '临沂', 2);INSERT INTO `t_city`(`id`, `pid`, `cityname`, `type`) VALUES (293, 22, '日照', 2);INSERT INTO `t_city`(`id`, `pid`, `cityname`, `type`) VALUES (294, 22, '泰安', 2);INSERT INTO `t_city`(`id`, `pid`, `cityname`, `type`) VALUES (295, 22, '威海', 2);INSERT INTO `t_city`(`id`, `pid`, `cityname`, `type`) VALUES (296, 22, '潍坊', 2);INSERT INTO `t_city`(`id`, `pid`, `cityname`, `type`) VALUES (297, 22, '烟台', 2);INSERT INTO `t_city`(`id`, `pid`, `cityname`, `type`) VALUES (298, 22, '枣庄', 2);INSERT INTO `t_city`(`id`, `pid`, `cityname`, `type`) VALUES (299, 22, '淄博', 2);INSERT INTO `t_city`(`id`, `pid`, `cityname`, `type`) VALUES (2332, 283, '市中区', 3);INSERT INTO `t_city`(`id`, `pid`, `cityname`, `type`) VALUES (2333, 283, '历下区', 3);INSERT INTO `t_city`(`id`, `pid`, `cityname`, `type`) VALUES (2334, 283, '天桥区', 3);INSERT INTO `t_city`(`id`, `pid`, `cityname`, `type`) VALUES (2335, 283, '槐荫区', 3);INSERT INTO `t_city`(`id`, `pid`, `cityname`, `type`) VALUES (2336, 283, '历城区', 3);INSERT INTO `t_city`(`id`, `pid`, `cityname`, `type`) VALUES (2337, 283, '长清区', 3);INSERT INTO `t_city`(`id`, `pid`, `cityname`, `type`) VALUES (2338, 283, '章丘市', 3);INSERT INTO `t_city`(`id`, `pid`, `cityname`, `type`) VALUES (2339, 283, '平阴县', 3);INSERT INTO `t_city`(`id`, `pid`, `cityname`, `type`) VALUES (2340, 283, '济阳县', 3);INSERT INTO `t_city`(`id`, `pid`, `cityname`, `type`) VALUES (2341, 283, '商河县', 3);INSERT INTO `t_city`(`id`, `pid`, `cityname`, `type`) VALUES (2342, 284, '市南区', 3);INSERT INTO `t_city`(`id`, `pid`, `cityname`, `type`) VALUES (2343, 284, '市北区', 3);INSERT INTO `t_city`(`id`, `pid`, `cityname`, `type`) VALUES (2344, 284, '城阳区', 3);INSERT INTO `t_city`(`id`, `pid`, `cityname`, `type`) VALUES (2345, 284, '四方区', 3);INSERT INTO `t_city`(`id`, `pid`, `cityname`, `type`) VALUES (2346, 284, '李沧区', 3);INSERT INTO `t_city`(`id`, `pid`, `cityname`, `type`) VALUES (2347, 284, '黄岛区', 3);INSERT INTO `t_city`(`id`, `pid`, `cityname`, `type`) VALUES (2348, 284, '崂山区', 3);INSERT INTO `t_city`(`id`, `pid`, `cityname`, `type`) VALUES (2349, 284, '胶州市', 3);INSERT INTO `t_city`(`id`, `pid`, `cityname`, `type`) VALUES (2350, 284, '即墨市', 3);INSERT INTO `t_city`(`id`, `pid`, `cityname`, `type`) VALUES (2351, 284, '平度市', 3);INSERT INTO `t_city`(`id`, `pid`, `cityname`, `type`) VALUES (2352, 284, '胶南市', 3);INSERT INTO `t_city`(`id`, `pid`, `cityname`, `type`) VALUES (2353, 284, '莱西市', 3);INSERT INTO `t_city`(`id`, `pid`, `cityname`, `type`) VALUES (2354, 285, '滨城区', 3);INSERT INTO `t_city`(`id`, `pid`, `cityname`, `type`) VALUES (2355, 285, '惠民县', 3);INSERT INTO `t_city`(`id`, `pid`, `cityname`, `type`) VALUES (2356, 285, '阳信县', 3);INSERT INTO `t_city`(`id`, `pid`, `cityname`, `type`) VALUES (2357, 285, '无棣县', 3);INSERT INTO `t_city`(`id`, `pid`, `cityname`, `type`) VALUES (2358, 285, '沾化县', 3);INSERT INTO `t_city`(`id`, `pid`, `cityname`, `type`) VALUES (2359, 285, '博兴县', 3);INSERT INTO `t_city`(`id`, `pid`, `cityname`, `type`) VALUES (2360, 285, '邹平县', 3);INSERT INTO `t_city`(`id`, `pid`, `cityname`, `type`) VALUES (2361, 286, '德城区', 3);INSERT INTO `t_city`(`id`, `pid`, `cityname`, `type`) VALUES (2362, 286, '陵县', 3);INSERT INTO `t_city`(`id`, `pid`, `cityname`, `type`) VALUES (2363, 286, '乐陵市', 3);INSERT INTO `t_city`(`id`, `pid`, `cityname`, `type`) VALUES (2364, 286, '禹城市', 3);INSERT INTO `t_city`(`id`, `pid`, `cityname`, `type`) VALUES (2365, 286, '宁津县', 3);INSERT INTO `t_city`(`id`, `pid`, `cityname`, `type`) VALUES (2366, 286, '庆云县', 3);INSERT INTO `t_city`(`id`, `pid`, `cityname`, `type`) VALUES (2367, 286, '临邑县', 3);INSERT INTO `t_city`(`id`, `pid`, `cityname`, `type`) VALUES (2368, 286, '齐河县', 3);INSERT INTO `t_city`(`id`, `pid`, `cityname`, `type`) VALUES (2369, 286, '平原县', 3);INSERT INTO `t_city`(`id`, `pid`, `cityname`, `type`) VALUES (2370, 286, '夏津县', 3);INSERT INTO `t_city`(`id`, `pid`, `cityname`, `type`) VALUES (2371, 286, '武城县', 3);INSERT INTO `t_city`(`id`, `pid`, `cityname`, `type`) VALUES (2372, 287, '东营区', 3);INSERT INTO `t_city`(`id`, `pid`, `cityname`, `type`) VALUES (2373, 287, '河口区', 3);INSERT INTO `t_city`(`id`, `pid`, `cityname`, `type`) VALUES (2374, 287, '垦利县', 3);INSERT INTO `t_city`(`id`, `pid`, `cityname`, `type`) VALUES (2375, 287, '利津县', 3);INSERT INTO `t_city`(`id`, `pid`, `cityname`, `type`) VALUES (2376, 287, '广饶县', 3);INSERT INTO `t_city`(`id`, `pid`, `cityname`, `type`) VALUES (2377, 288, '牡丹区', 3);INSERT INTO `t_city`(`id`, `pid`, `cityname`, `type`) VALUES (2378, 288, '曹县', 3);INSERT INTO `t_city`(`id`, `pid`, `cityname`, `type`) VALUES (2379, 288, '单县', 3);INSERT INTO `t_city`(`id`, `pid`, `cityname`, `type`) VALUES (2380, 288, '成武县', 3);INSERT INTO `t_city`(`id`, `pid`, `cityname`, `type`) VALUES (2381, 288, '巨野县', 3);INSERT INTO `t_city`(`id`, `pid`, `cityname`, `type`) VALUES (2382, 288, '郓城县', 3);INSERT INTO `t_city`(`id`, `pid`, `cityname`, `type`) VALUES (2383, 288, '鄄城县', 3);INSERT INTO `t_city`(`id`, `pid`, `cityname`, `type`) VALUES (2384, 288, '定陶县', 3);INSERT INTO `t_city`(`id`, `pid`, `cityname`, `type`) VALUES (2385, 288, '东明县', 3);INSERT INTO `t_city`(`id`, `pid`, `cityname`, `type`) VALUES (2386, 289, '市中区', 3);INSERT INTO `t_city`(`id`, `pid`, `cityname`, `type`) VALUES (2387, 289, '任城区', 3);INSERT INTO `t_city`(`id`, `pid`, `cityname`, `type`) VALUES (2388, 289, '曲阜市', 3);INSERT INTO `t_city`(`id`, `pid`, `cityname`, `type`) VALUES (2389, 289, '兖州市', 3);INSERT INTO `t_city`(`id`, `pid`, `cityname`, `type`) VALUES (2390, 289, '邹城市', 3);INSERT INTO `t_city`(`id`, `pid`, `cityname`, `type`) VALUES (2391, 289, '微山县', 3);INSERT INTO `t_city`(`id`, `pid`, `cityname`, `type`) VALUES (2392, 289, '鱼台县', 3);INSERT INTO `t_city`(`id`, `pid`, `cityname`, `type`) VALUES (2393, 289, '金乡县', 3);INSERT INTO `t_city`(`id`, `pid`, `cityname`, `type`) VALUES (2394, 289, '嘉祥县', 3);INSERT INTO `t_city`(`id`, `pid`, `cityname`, `type`) VALUES (2395, 289, '汶上县', 3);INSERT INTO `t_city`(`id`, `pid`, `cityname`, `type`) VALUES (2396, 289, '泗水县', 3);INSERT INTO `t_city`(`id`, `pid`, `cityname`, `type`) VALUES (2397, 289, '梁山县', 3);INSERT INTO `t_city`(`id`, `pid`, `cityname`, `type`) VALUES (2398, 290, '莱城区', 3);INSERT INTO `t_city`(`id`, `pid`, `cityname`, `type`) VALUES (2399, 290, '钢城区', 3);INSERT INTO `t_city`(`id`, `pid`, `cityname`, `type`) VALUES (2400, 291, '东昌府区', 3);INSERT INTO `t_city`(`id`, `pid`, `cityname`, `type`) VALUES (2401, 291, '临清市', 3);INSERT INTO `t_city`(`id`, `pid`, `cityname`, `type`) VALUES (2402, 291, '阳谷县', 3);INSERT INTO `t_city`(`id`, `pid`, `cityname`, `type`) VALUES (2403, 291, '莘县', 3);INSERT INTO `t_city`(`id`, `pid`, `cityname`, `type`) VALUES (2404, 291, '茌平县', 3);INSERT INTO `t_city`(`id`, `pid`, `cityname`, `type`) VALUES (2405, 291, '东阿县', 3);INSERT INTO `t_city`(`id`, `pid`, `cityname`, `type`) VALUES (2406, 291, '冠县', 3);INSERT INTO `t_city`(`id`, `pid`, `cityname`, `type`) VALUES (2407, 291, '高唐县', 3);INSERT INTO `t_city`(`id`, `pid`, `cityname`, `type`) VALUES (2408, 292, '兰山区', 3);INSERT INTO `t_city`(`id`, `pid`, `cityname`, `type`) VALUES (2409, 292, '罗庄区', 3);INSERT INTO `t_city`(`id`, `pid`, `cityname`, `type`) VALUES (2410, 292, '河东区', 3);INSERT INTO `t_city`(`id`, `pid`, `cityname`, `type`) VALUES (2411, 292, '沂南县', 3);INSERT INTO `t_city`(`id`, `pid`, `cityname`, `type`) VALUES (2412, 292, '郯城县', 3);INSERT INTO `t_city`(`id`, `pid`, `cityname`, `type`) VALUES (2413, 292, '沂水县', 3);INSERT INTO `t_city`(`id`, `pid`, `cityname`, `type`) VALUES (2414, 292, '苍山县', 3);INSERT INTO `t_city`(`id`, `pid`, `cityname`, `type`) VALUES (2415, 292, '费县', 3);INSERT INTO `t_city`(`id`, `pid`, `cityname`, `type`) VALUES (2416, 292, '平邑县', 3);INSERT INTO `t_city`(`id`, `pid`, `cityname`, `type`) VALUES (2417, 292, '莒南县', 3);INSERT INTO `t_city`(`id`, `pid`, `cityname`, `type`) VALUES (2418, 292, '蒙阴县', 3);INSERT INTO `t_city`(`id`, `pid`, `cityname`, `type`) VALUES (2419, 292, '临沭县', 3);INSERT INTO `t_city`(`id`, `pid`, `cityname`, `type`) VALUES (2420, 293, '东港区', 3);INSERT INTO `t_city`(`id`, `pid`, `cityname`, `type`) VALUES (2421, 293, '岚山区', 3);INSERT INTO `t_city`(`id`, `pid`, `cityname`, `type`) VALUES (2422, 293, '五莲县', 3);INSERT INTO `t_city`(`id`, `pid`, `cityname`, `type`) VALUES (2423, 293, '莒县', 3);INSERT INTO `t_city`(`id`, `pid`, `cityname`, `type`) VALUES (2424, 294, '泰山区', 3);INSERT INTO `t_city`(`id`, `pid`, `cityname`, `type`) VALUES (2425, 294, '岱岳区', 3);INSERT INTO `t_city`(`id`, `pid`, `cityname`, `type`) VALUES (2426, 294, '新泰市', 3);INSERT INTO `t_city`(`id`, `pid`, `cityname`, `type`) VALUES (2427, 294, '肥城市', 3);INSERT INTO `t_city`(`id`, `pid`, `cityname`, `type`) VALUES (2428, 294, '宁阳县', 3);INSERT INTO `t_city`(`id`, `pid`, `cityname`, `type`) VALUES (2429, 294, '东平县', 3);INSERT INTO `t_city`(`id`, `pid`, `cityname`, `type`) VALUES (2430, 295, '荣成市', 3);INSERT INTO `t_city`(`id`, `pid`, `cityname`, `type`) VALUES (2431, 295, '乳山市', 3);INSERT INTO `t_city`(`id`, `pid`, `cityname`, `type`) VALUES (2432, 295, '环翠区', 3);INSERT INTO `t_city`(`id`, `pid`, `cityname`, `type`) VALUES (2433, 295, '文登市', 3);INSERT INTO `t_city`(`id`, `pid`, `cityname`, `type`) VALUES (2434, 296, '潍城区', 3);INSERT INTO `t_city`(`id`, `pid`, `cityname`, `type`) VALUES (2435, 296, '寒亭区', 3);INSERT INTO `t_city`(`id`, `pid`, `cityname`, `type`) VALUES (2436, 296, '坊子区', 3);INSERT INTO `t_city`(`id`, `pid`, `cityname`, `type`) VALUES (2437, 296, '奎文区', 3);INSERT INTO `t_city`(`id`, `pid`, `cityname`, `type`) VALUES (2438, 296, '青州市', 3);INSERT INTO `t_city`(`id`, `pid`, `cityname`, `type`) VALUES (2439, 296, '诸城市', 3);INSERT INTO `t_city`(`id`, `pid`, `cityname`, `type`) VALUES (2440, 296, '寿光市', 3);INSERT INTO `t_city`(`id`, `pid`, `cityname`, `type`) VALUES (2441, 296, '安丘市', 3);INSERT INTO `t_city`(`id`, `pid`, `cityname`, `type`) VALUES (2442, 296, '高密市', 3);INSERT INTO `t_city`(`id`, `pid`, `cityname`, `type`) VALUES (2443, 296, '昌邑市', 3);INSERT INTO `t_city`(`id`, `pid`, `cityname`, `type`) VALUES (2444, 296, '临朐县', 3);INSERT INTO `t_city`(`id`, `pid`, `cityname`, `type`) VALUES (2445, 296, '昌乐县', 3);INSERT INTO `t_city`(`id`, `pid`, `cityname`, `type`) VALUES (2446, 297, '芝罘区', 3);INSERT INTO `t_city`(`id`, `pid`, `cityname`, `type`) VALUES (2447, 297, '福山区', 3);INSERT INTO `t_city`(`id`, `pid`, `cityname`, `type`) VALUES (2448, 297, '牟平区', 3);INSERT INTO `t_city`(`id`, `pid`, `cityname`, `type`) VALUES (2449, 297, '莱山区', 3);INSERT INTO `t_city`(`id`, `pid`, `cityname`, `type`) VALUES (2450, 297, '开发区', 3);INSERT INTO `t_city`(`id`, `pid`, `cityname`, `type`) VALUES (2451, 297, '龙口市', 3);INSERT INTO `t_city`(`id`, `pid`, `cityname`, `type`) VALUES (2452, 297, '莱阳市', 3);INSERT INTO `t_city`(`id`, `pid`, `cityname`, `type`) VALUES (2453, 297, '莱州市', 3);INSERT INTO `t_city`(`id`, `pid`, `cityname`, `type`) VALUES (2454, 297, '蓬莱市', 3);INSERT INTO `t_city`(`id`, `pid`, `cityname`, `type`) VALUES (2455, 297, '招远市', 3);INSERT INTO `t_city`(`id`, `pid`, `cityname`, `type`) VALUES (2456, 297, '栖霞市', 3);INSERT INTO `t_city`(`id`, `pid`, `cityname`, `type`) VALUES (2457, 297, '海阳市', 3);INSERT INTO `t_city`(`id`, `pid`, `cityname`, `type`) VALUES (2458, 297, '长岛县', 3);INSERT INTO `t_city`(`id`, `pid`, `cityname`, `type`) VALUES (2459, 298, '市中区', 3);INSERT INTO `t_city`(`id`, `pid`, `cityname`, `type`) VALUES (2460, 298, '山亭区', 3);INSERT INTO `t_city`(`id`, `pid`, `cityname`, `type`) VALUES (2461, 298, '峄城区', 3);INSERT INTO `t_city`(`id`, `pid`, `cityname`, `type`) VALUES (2462, 298, '台儿庄区', 3);INSERT INTO `t_city`(`id`, `pid`, `cityname`, `type`) VALUES (2463, 298, '薛城区', 3);INSERT INTO `t_city`(`id`, `pid`, `cityname`, `type`) VALUES (2464, 298, '滕州市', 3);INSERT INTO `t_city`(`id`, `pid`, `cityname`, `type`) VALUES (2465, 299, '张店区', 3);INSERT INTO `t_city`(`id`, `pid`, `cityname`, `type`) VALUES (2466, 299, '临淄区', 3);INSERT INTO `t_city`(`id`, `pid`, `cityname`, `type`) VALUES (2467, 299, '淄川区', 3);INSERT INTO `t_city`(`id`, `pid`, `cityname`, `type`) VALUES (2468, 299, '博山区', 3);INSERT INTO `t_city`(`id`, `pid`, `cityname`, `type`) VALUES (2469, 299, '周村区', 3);INSERT INTO `t_city`(`id`, `pid`, `cityname`, `type`) VALUES (2470, 299, '桓台县', 3);INSERT INTO `t_city`(`id`, `pid`, `cityname`, `type`) VALUES (2471, 299, '高青县', 3);INSERT INTO `t_city`(`id`, `pid`, `cityname`, `type`) VALUES (2472, 299, '沂源县', 3);

最后,这是测试结果:

标签: #枣庄java培训