先主查询,再关联子查询,不影响分页效果,否则子查询也会参与分页。
<resultmap id="hdr" type="com.hdr"> <id column="crh_id" property="id" javatype="int"/> <collection property="dtllist" select="queryallrmddetail" column="crh_id" fetchtype="eager"> </collection> </resultmap>
<resultmap id="dtl" type="com.dtl"> </resultmap>
主查询:
<select id="querybycondition" parametertype="string" resultmap="hdr"> </select>
在主查询后,通过传入主键id进行关联子查询:
<select id="queryalldetail" parametertype="int" resultmap="dtl"> select * from biz_dtl where crh_id = #{id} </select>
主查询的结果是list,以及每一条记录的内涵list,性能是n+1次查询。
如果提高查询性能,可以使用别名的方式,在sql中把子查询进行重新命名。
不过如果主查询包括sum和group语句,这种方式就不可以。
只有在平铺所有主从表的时候可用。
<resultmap id="blogresult" type="blog"> <id property="id" column="blog_id" /> <result property="title" column="blog_title"/> <collection property="posts" oftype="post" resultmap="blogpostresult" columnprefix="post_"/> </resultmap> <resultmap id="blogpostresult" type="post"> <id property="id" column="id"/> <result property="subject" column="subject"/> <result property="body" column="body"/> </resultmap>
到此这篇关于mybatis分页查询主从表的实现示例的文章就介绍到这了,更多相关mybatis分页查询主从表内容请搜索代码网以前的文章或继续浏览下面的相关文章希望大家以后多多支持代码网!
发表评论