Student类中有引用Address 在查询学生的信息时,把address的信息也查询
1.级联查询(少用,重复利用效率低)
在mapper配置如下:
<resultMap type="Student" id="StudentResult">
<id property="id" column="id"/>
<result property="name" column="name"/>
<result property="age" column="age"/>
<result property="address.id" column="addressId"/>
<result property="address.province" column="province"/>
<result property="address.city" column="city"/>
<result property="address.area" column="area"/>
</resultMap>
2.重新定义Address类型,在Student中引入Address
(少用,原因同一)
<resultMap type="Address" id="AddressResult">
<result property="id" column="id"/>
<result property="province" column="province"/>
<result property="city" column="city"/>
<result property="area" column="area"/>
</resultMap>
<resultMap type="Student" id="StudentResult">
<id property="id" column="id"/>
<result property="name" column="name"/>
<result property="age" column="age"/>
<association property="address" resultMap="AddressResult">
</association>
</resultMap>
3.另外配置AddressMapper在查询Student时,把对应的外键Id,addressId的作为参数传递到AddressMapper的方法中,并调用此方法查询Address信息
(开发常用,重复利用效率高)
<resultMap type="Student" id="StudentResult">
<id property="id" column="id"/>
<result property="name" column="name"/>
<result property="age" column="age"/>
<association property="address" column="addressId" select="com.czp.mappers.AddressMapper.findById">
</association>
</resultMap>