mybatis中association、collection和discriminator的使用

在使用Mybatis中定义resultMap的时候,经常会遇到这三个比较常用的标签association、collection和discriminator,本篇博客就在上两遍博客的基础上,讲解如何使用这个三个标签。

下面依次进行介绍,首先看一下association:
先看一下两个不同的resultMap定义:

<resultMap id="city" type="com.entities.City">
        <id column="city_id" jdbcType="INTEGER" property="id"/>
        <result column="city_name" jdbcType="CHAR" property="name"/>
        <result column="city_countryCode" jdbcType="CHAR" property="countrycode"/>
        <result column="city_district" jdbcType="CHAR" property="district"/>
        <result column="city_population" jdbcType="INTEGER" property="population"/>
    </resultMap>
    <resultMap id="countryAndCity" type="com.entities.AssociationBean">
        <id column="country_code" jdbcType="CHAR" property="code" />
        <result column="country_name" jdbcType="CHAR" property="name" />
        <result column="country_continent" jdbcType="CHAR" property="continent" />
        <result column="country_region" jdbcType="CHAR" property="region" />
        <result column="country_surfaceArea" jdbcType="REAL" property="surfacearea" />
        <result column="country_indepYear" jdbcType="SMALLINT" property="indepyear" />
        <result column="country_population" jdbcType="INTEGER" property="population" />
        <result column="country_lifeExpectancy" jdbcType="REAL" property="lifeexpectancy" />
        <result column="country_gnp" jdbcType="REAL" property="gnp" />
        <result column="country_gnpOld" jdbcType="REAL" property="gnpold" />
        <result column="country_localName" jdbcType="CHAR" property="localname" />
        <result column="country_governmentForm" jdbcType="CHAR" property="governmentform" />
        <result column="country_headOfState" jdbcType="CHAR" property="headofstate" />
        <result column="country_capital" jdbcType="INTEGER" property="capital" />
        <result column="country_code2" jdbcType="CHAR" property="code2" />
        <result column="city_id" jdbcType="INTEGER" property="city_id"/>
        <result column="city_name" jdbcType="CHAR" property="city_name"/>
        <result column="city_countryCode" jdbcType="CHAR" property="city_countryCode"/>
        <result column="city_district" jdbcType="CHAR" property="city_district"/>
        <result column="city_population" jdbcType="INTEGER" property="city_population"/>
    </resultMap>
    <resultMap id="countryAndCity1" type="com.entities.AssociationBean1">
        <id column="country_code" jdbcType="CHAR" property="code" />
        <result column="country_name" jdbcType="CHAR" property="name" />
        <result column="country_continent" jdbcType="CHAR" property="continent" />
        <result column="country_region" jdbcType="CHAR" property="region" />
        <result column="country_surfaceArea" jdbcType="REAL" property="surfacearea" />
        <result column="country_indepYear" jdbcType="SMALLINT" property="indepyear" />
        <result column="country_population" jdbcType="INTEGER" property="population" />
        <result column="country_lifeExpectancy" jdbcType="REAL" property="lifeexpectancy" />
        <result column="country_gnp" jdbcType="REAL" property="gnp" />
        <result column="country_gnpOld" jdbcType="REAL" property="gnpold" />
        <result column="country_localName" jdbcType="CHAR" property="localname" />
        <result column="country_governmentForm" jdbcType="CHAR" property="governmentform" />
        <result column="country_headOfState" jdbcType="CHAR" property="headofstate" />
        <result column="country_capital" jdbcType="INTEGER" property="capital" />
        <result column="country_code2" jdbcType="CHAR" property="code2" />
        <association property="city" resultMap="city"/>
    </resultMap>

其实countryAndCity和countryAndCity1在实际对返回结果集的表示上是一致,都是对结果的封装,只是countryAndCity1将City表中的字段抽取出来进行封装成city对象,并将其作为一个属性存放在AssociationBean1对象中。
而countryAndCity只是将查询语句返回的所有字段映射为一个对象的内部属性,并没有对部分属性进行二次封装。类似的这种封装在联表查询中经常会遇到这种情况,下载看一下代码,以及完成的mapper语句

    <resultMap id="city" type="com.entities.City">
        <id column="city_id" jdbcType="INTEGER" property="id"/>
  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值