作者:fyupeng
技术专栏:☞ https://github.com/fyupeng
项目地址:☞ https://github.com/fyupeng/distributed-blog-system-api
留给读者
一、介绍
比如你需要在Company
对象中,嵌套列表SonCompanys
和ContactInfos
,而且还是直接通过数据库查询出来,不想自己遍历去构造这种对象,那么你可以使用MyBatis
的collection
关键字。
二、代码
<select id="queryCompanyDataByKey" resultMap="CompanyItemMap">
select * from (
select a.id, a.area, a.uniscid, a.updatetime as updateTime, b.area as sonArea, b.uniscid as sonUniscid, c.name as contactName
from company a inner join son_comopany b on a.sonId = b.id
inner join contact_info c on a.contact_id = c.id
)
</select>
<resultMap id="CompanyItemMap" type="com.fyupeng.company">
<id property="id" column="id" />
<result property="area" column="area" />
<result property="uniscid" column="uniscid" />
// 子公司
<result property="updateTime" column="updateTime" />
<collection property="sonCompanys" ofType="com.fyupeng.SonCompany">
<result property="sonArea" column="sonArea" />
<result property="sonUniscid" column="sonUniscid" />
</collection>
// 联系人信息
<collection property="contactInfos" ofType="com.fyupeng.ContactInfo">
<result property="contactName" column="contactName" />
</collection>
</resultMap>
三、总结
简洁、高效、实用!