此仅为个人笔记,若有不周之处,万望指正,不胜感激。
多表单独查询与多表连接查询的实现只需将映射文件稍作修改即可。关于单表连接查询的相关配置可查看另一篇博客:点击打开链接。映射文件代码如下:
<mapper namespace="com.bjpowernode.dao.ICountryDao">
<select id="selectMinisterByCountry" resultType="com.bjpowernode.beans.Minister">
select mid,mname from minister where countryId=#{cid}
</select>
<resultMap type="com.bjpowernode.beans.Country" id="countryMapper">
<id column="cid" property="cid"/>
<result column="cname" property="cname"/>
<collection property="ministers"
ofType="com.bjpowernode.beans.Minister"
select="selectMinisterByCountry"
column="cid"/>
</resultMap>
<select id="selectCountryById" resultMap="countryMapper">
select cid,cname
from country
where cid=#{cid}
</select>
</mapper>
多表连接查询在映射文件的含义是执行一条sql即可;多表单独查询是将一层一层查询,最后总结数据,需多条sql。此样例便是如此,先执行selectCountry操作,若在表Country的列cid中找到与#{cid}中cid值相同的行,则将找到的值返回到countryMapper中column="cid"的cid,而后执行selectMinisterByCountry,此操作中的#{cid}是取的刚才查询到的column="cid"的cid。切勿弄混!