Mybatis级联查询

先写一下需求:
 				省  编号 001
 				              002
 				              003
 				              .....
 				市  编号00101
 				             00102
 				             00103
 				             00201
 				             ........
 				区   编号0010101
 				              0010102
 				              .............
级联查询省市区

/**
* 市
*/
@Data
public class BaseProvince {


    /**----------------------------------------------------------    -省-   --------------------------------------------------------------------*/
    /**
     * 省ID
     */
    private String provinceId;

    /**
     * 省VERSION
     */
    private Integer version;

    /**
     * 省名称
     */
    private String provinceName;

    /**
     * 序列号
     */
    private Integer sortNum;

    /**
     * 国标代码
     */
    private String  gbCode;

    /**
     * 数据过期时间0:永不过期
     */
    private BigInteger invalidTime;

    /**
     * 0否1是
     */
    private String delFlag;

    /**
     * 市
     */
    @Transient
    private List<BaseCity> cities;

}
/**
 * <Description>
 * 市
 * @createDate 2019/12/13 11:32
 */
@Data
public class BaseCity {

    /**--------------------------------------------------------------  市    --------------------------------------------------------------*/


    /**
     * 城市ID
     */
    private String  cityId;

    /**
     * 城市名称
     */
    private String cityName;

    /**
     * 市 序列号
     */
    private Integer sortNum;

    /**
     * 市地址经度
     */
    private String longitude;

    /**
     * 市地址纬度
     */
    private String latitude;

    /**
     * 市国标代码
     */
    private String gbCode;

    /**
     * 市 VERSION
     */
    private Integer version;

    /**
     * 市 数据过期时间0:永不过期
     */
    private BigInteger invalidTime;

    /**
     * 市 0否1是
     */
    private String delFlag;

    /**
     * 区
     */
    @Transient
    private List<BaseCounty> countys;

}
/**
 * <Description>
 * 区
 * @createDate 2019/12/13 11:34
 */
@Data
public class BaseCounty {

    /**--------------------------------------------------------- 区 -------------------------------------------------------------------------*/

    /**
     * 城镇ID
     */
    private String countyId;

    /**
     * 城镇名称
     */
    private String countyName;


    /**
     * 区序列号
     */
    private Integer sortNum;

    /**
     * 区 地址经度
     */
    private String longitude;

    /**
     * 区 地址纬度
     */
    private String latitude;

    /**
     * 区 国标代码
     */
    private String gbCode;

    /**
     * 区 VERSION
     */
    private Integer version;

    /**
     * 区 数据过期时间0:永不过期
     */
    private BigInteger invalidTime;

    /**
     * 区 0否1是
     */
    private String DelFlag;
}

Mapper

***
<mapper namespace="BaseProvinceMapper">

<!-- 可根据自己的需求,是否要使用 -->
<resultMap type="BaseProvince" id="baseProvinceMap">
    <result property="provinceId" column="province_id"/>
    <result property="version" column="version"/>
    <result property="provinceName" column="province_name"/>
    <result property="sortNum" column="sort_num"/>
    <result property="gbCode" column="gb_code"/>
    <result property="invalidTime" column="invalid_time"/>
    <result property="delFlag" column="del_flag"/>
    <collection property="cities" column="province_id"  select="BaseCityMapper.selectShi">
    </collection>
</resultMap>
        <select id="selectSheng" resultMap="baseProvinceMap">
         SELECT * FROM base_province
        </select>
</mapper>
<mapper namespace="BaseCityMapper">
    <resultMap type="BaseCity" id="baseCityMap">
        <result property="cityId" column="city_id"/>
        <result property="version" column="version"/>
        <result property="cityName" column="city_name"/>
        <result property="sortNum" column="sort_num"/>
        <result property="gbCode" column="gb_code"/>
        <result property="invalidTime" column="invalid_time"/>
        <result property="delFlag" column="del_flag"/>
        <result property="latitude" column="latitude"/>
        <result property="longitude" column="longitude"/>
        <collection property="countys" column="city_id" select="BaseCountyMapper.selectQu">
        </collection>
    </resultMap>
    <select id="selectShi" resultMap="baseCityMap">
               SELECT * FROM base_city WHERE city_id like concat(concat(#{cityId}),'%')
        </select>
</mapper>
<mapper namespace="BaseCountyMapper">
    <resultMap type="BaseCounty" id="baseCountyMap">
        <result property="countyId" column="county_id"/>
        <result property="version" column="version"/>
        <result property="countyName" column="county_name"/>
        <result property="sortNum" column="sort_num"/>
        <result property="gbCode" column="gb_code"/>
        <result property="invalidTime" column="invalid_time"/>
        <result property="delFlag" column="del_flag"/>
        <result property="latitude" column="latitude"/>
        <result property="longitude" column="longitude"/>
    </resultMap>
    <select id="selectQu" resultMap="baseCountyMap">
              SELECT * FROM base_county WHERE county_id like concat(concat(#{countyId}),'%')
        </select>
</mapper>

这里用到的级联查询中的一种 其中注意:collection中的 column="" 表示你要传入的参数要和 你传入的数据 的column保存一致

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值