在使用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"/>