mybatis-地区三表生成地区树

package com.dhht.manager.vo.area;

import lombok.Data;

import java.io.Serializable;
import java.util.List;

/**
* @Author: sh
* @Description: ProvinceVO
* @Date: 19:33 2019/12/23
*/
@Data
public class ProvinceVO implements Serializable {
private static final long serialVersionUID = 8581900297888963919L;
private Long id;
private String name;
private String code;
private Long regionLevel;
private String regionName;
private Long parentId;
private List<CityVO> children;
}

package com.dhht.manager.vo.area;

import lombok.Data;

import java.io.Serializable;
import java.util.List;

/**
* @Author: sh
* @Description: CityVO
* @Date: 19:33 2019/12/23
*/
@Data
public class CityVO implements Serializable {
private static final long serialVersionUID = -4506315529106267971L;
private Long id;
private String name;
private String code;
private Long regionLevel;
private String regionName;
private Long parentId;
private List<DistrictVO> children;
}

package com.dhht.manager.vo.area;

import lombok.Data;

import java.io.Serializable;
import java.util.List;

/**
* @Author: sh
* @Description: DistrictVO
* @Date: 19:35 2019/12/23
*/
@Data
public class DistrictVO implements Serializable {
private static final long serialVersionUID = 6682075711606147829L;

private Long id;
private String name;
private String code;
private Long regionLevel;
private String regionName;
private Long parentId;
private List<Object> children;
}

package com.dhht.dao;

import com.dhht.manager.vo.area.DistrictVO;
import com.dhht.manager.vo.area.ProvinceVO;
import com.dhht.model.City;
import org.apache.ibatis.annotations.Param;

import java.util.List;

public interface CityMapper {

List<ProvinceVO> treeProvinceQuery(@Param(value = "code")String code);

List<City> treeCityQuery(@Param(value = "id")Long id);

List<DistrictVO> treeDistrictQuery(@Param(value = "id")Long id);
}


<?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">
<mapper namespace="com.dhht.dao.CityMapper">
<!--province-->
<resultMap id="provinceVoMap" type="com.dhht.manager.vo.area.ProvinceVO">
<id column="id" jdbcType="BIGINT" property="id" />
<result column="name" jdbcType="VARCHAR" property="name" />
<result column="code" jdbcType="VARCHAR" property="code" />
<result column="parentId" jdbcType="BIGINT" property="parentId" />
<collection property="children" column="id" select="com.dhht.dao.CityMapper.treeCityQuery"></collection>
</resultMap>
<!--end province-->

<!--city-->
<resultMap id="cityVoMap" type="com.dhht.manager.vo.area.CityVO">
<id column="id" jdbcType="BIGINT" property="id" />
<result column="name" jdbcType="VARCHAR" property="name" />
<result column="code" jdbcType="VARCHAR" property="code" />
<result column="parentId" jdbcType="BIGINT" property="parentId" />
<collection property="children" column="id" select="com.dhht.dao.CityMapper.treeDistrictQuery"></collection>
</resultMap>
<!--end city-->

<!--district-->
<resultMap id="districtVoMap" type="com.dhht.manager.vo.area.DistrictVO">
<id column="id" jdbcType="BIGINT" property="id" />
<result column="name" jdbcType="VARCHAR" property="name" />
<result column="code" jdbcType="VARCHAR" property="code" />
<result column="parentId" jdbcType="BIGINT" property="parentId" />
</resultMap>
<!--end district-->
<resultMap id="BaseResultMap" type="com.dhht.model.City">
<id column="id" jdbcType="BIGINT" property="id" />
<result column="name" jdbcType="VARCHAR" property="name" />
<result column="province_id" jdbcType="BIGINT" property="provinceId" />
<result column="code" jdbcType="VARCHAR" property="code" />
</resultMap>

<select id="treeProvinceQuery" resultMap="provinceVoMap">
select id,name,code,NULL AS parentId
from lv_province
WHERE CODE=#{code}
</select>

<select id="treeCityQuery" resultMap="cityVoMap">
select id,name,code,#{id} AS parentId
from lv_city
WHERE province_id=#{id}
</select>

<select id="treeDistrictQuery" resultMap="districtVoMap">
select id,name,code,#{id} AS parentId
from lv_district
WHERE city_id=#{id}
</select>
</mapper>
原文地址:https://www.cnblogs.com/sung1024/p/12128010.html