Foreword:
This method essentially splits the one-to-many related query into subqueries. Because Pagehelper only paging the first SQL, two queries will solve this problem.
But I write related queries just to reduce the number of queries. This method can also be used to write two selectLists and use code to process the data instead. Maybe it means writing less code and making the business look better. .
In my impression, when using paging before, there was no problem of total errors caused by related queries. I don’t know if I used other paging plug-ins, manual paging, or Pagehelper, but a certain version has solved this problem. If anyone knows, please comment. Please give me some advice, thank you!
Related query
<resultMap id="pageListResult" type="com.investment.entity.vo.DirectFundFoundContributorPageVo">
<id property="id" column="id"/>
<result property="fundId" column="fundId"/>
<result property="foundId" column="foundId" />
......
<collection property="files" ofType="com.investment.entity.system.SysFile">
<result property="realName" column="realName" />
<result property="accessName" column="accessName" />
<result property="convertName" column="convertName" />
<result property="suffix" column="suffix" />
<result property="url" column="url" />
<result property="pdfUrl" column="pdfUrl" />
</collection>
</resultMap>
<select id="selectPageList" resultMap="pageListResult" resultType="com.investment.entity.vo.DirectFundFoundContributorPageVo">
select dffc.xxx, sf.xxx
from direct_fund_found_contributor dffc
left join direct_fund df on dffc.fundId = df.id
left join direct_fund_found dff on dffc.foundId = dff.id
left join (select * from sys_file where headType = 3 and delFlag = 1 ) sf on sf.tableId = dffc.id
<where>
<if test="fundId != null">
and dffc.fundId = #{fundId}
</if>
and dff.delFlag = 1
and dffc.delFlag = 1
</where>
order by dffc.id desc
</select>
Use the query tool to query the results as shown in the figure, so Pagehelper also recognizes total=3
But the effect I want is: there are two Lists, and the internal file list with id=1 is two pieces of data.
subquery
In fact, it is queried twice
The transformation is also very simple, just split the SQL and fine-tune the resultMap
- Add select="selectFiles" to the collection to represent the sql of the subquery;
- column="id"represents the associated field.
<resultMap id="pageListResult" type="com.investment.entity.vo.DirectFundFoundContributorPageVo">
<id property="id" column="id"/>
<result property="fundId" column="fundId"/>
<result property="foundId" column="foundId" />
......
<collection property="files" ofType="com.investment.entity.system.SysFile" select="selectFiles" column="id" >
<result property="realName" column="realName" />
<result property="accessName" column="accessName" />
<result property="convertName" column="convertName" />
<result property="suffix" column="suffix" />
<result property="url" column="url" />
<result property="pdfUrl" column="pdfUrl" />
</collection>
</resultMap>
Add a new query statement, the id is equal to the selectFiles defined above, and the column="id" field used for the associated field
<select id="selectFiles" resultType="com.zkjg.investment.entity.system.SysFile">
select xxx
from sys_file
where headType = 3 and delFlag = 1 and tableId = #{id}
</select>
<select id="selectPageList" resultMap="pageListResult" resultType="com.investment.entity.vo.DirectFundFoundContributorPageVo">
select dffc.xxx
from direct_fund_found_contributor dffc
left join direct_fund df on dffc.fundId = df.id
left join direct_fund_found dff on dffc.foundId = dff.id
<where>
<if test="fundId != null">
and dffc.fundId = #{fundId}
</if>
and dff.delFlag = 1
and dffc.delFlag = 1
</where>
order by dffc.id desc
</select>