1.pageHelper遇到resultMap的collection后,分页总数不对
<resultMap id="queryInvoicesResultMap" type="com.hwht.invoicecenter.entity.QueryInvoicesPO"> <id column="serviceInvoiceBillNo" property="serviceInvoiceBillNo" jdbcType="VARCHAR"/> <result column="invoiceMethod" property="invoiceMethod" jdbcType="VARCHAR"/> <result column="invoiceStatus" property="invoiceStatus" jdbcType="VARCHAR"/> <result column="invoiceTitle" property="invoiceTitle" jdbcType="VARCHAR"/> <result column="invoiceType" property="invoiceType" jdbcType="VARCHAR"/> <result column="templateNo" property="templateNo" jdbcType="VARCHAR"/> <result column="templateItemNo" property="templateItemNo" jdbcType="VARCHAR"/> <result column="buyerType" property="buyerType" jdbcType="VARCHAR"/> <result column="taxNo" property="taxNo" jdbcType="VARCHAR"/> <result column="customerAddress" property="customerAddress" jdbcType="VARCHAR"/> <result column="customerBankName" property="customerBankName" jdbcType="VARCHAR"/> <result column="customerTelephone" property="customerTelephone" jdbcType="VARCHAR"/> <result column="customerBankAccount" property="customerBankAccount" jdbcType="VARCHAR"/> <result column="invoiceContentType" property="invoiceContentType" jdbcType="VARCHAR"/> <result column="invoiceContent" property="invoiceContent" jdbcType="VARCHAR"/> <result column="invoiceRemark" property="invoiceRemark" jdbcType="VARCHAR"/> <result column="remark" property="remark" jdbcType="VARCHAR"/> <result column="totalPriceWithTax" property="totalPriceWithTax" jdbcType="DECIMAL"/> <result column="issueInvoiceType" property="issueInvoiceType" jdbcType="VARCHAR"/> <collection property="invoiceOrderList" ofType="com.hwht.invoicecenter.message.queryinvoiceinfo.InvoiceOrder" column="serviceInvoiceBillNo"> <id column="orderInfoID" property="orderInfoID" jdbcType="INTEGER"/> <result column="orderID" property="orderID" jdbcType="VARCHAR"/> <result column="serviceType" property="serviceType" jdbcType="VARCHAR"/> <result column="callbackPrefixURL" property="callbackPrefixURL" jdbcType="VARCHAR"/> <result column="priceWithTax" property="priceWithTax" jdbcType="DECIMAL"/> </collection> </resultMap> <select id="queryInvoices" resultMap="queryInvoicesResultMap" parameterType="com.hwht.invoicecenter.message.queryinvoices.QueryInvoicesParams"> select i.serviceInvoiceBillNo,i.invoiceMethod,i.invoiceStatus,i.invoiceTitle,i.invoiceType,i.templateNo, i.templateItemNo,i.buyerType,i.taxNo,i.customerAddress,i.customerBankName,i.customerTelephone, i.customerBankAccount,i.invoiceContentType,i.invoiceContent,i.invoiceRemark,i.remark,i.totalPriceWithTax, i.issueInvoiceType,o.orderInfoID,o.orderID,o.serviceType,o.callbackPrefixURL,o.priceWithTax from t_invoice i join t_invoice_order o on i.serviceInvoiceBillNo=o.serviceInvoiceBillNo <trim prefixOverrides="and" prefix="where"> <if test="orderID !=null"> and o.orderID=#{orderID,jdbcType=VARCHAR} </if> <if test="retryTimes !=null"> <![CDATA[ and i.retryTimes<=#{retryTimes,jdbcType=INTEGER}]]> </if> <if test="serviceInvoiceBillNo !=null"> and i.serviceInvoiceBillNo=#{serviceInvoiceBillNo,jdbcType=VARCHAR} </if> <if test="invoiceStatus !=null"> and i.invoiceStatus=#{invoiceStatus,jdbcType=VARCHAR} </if> <if test="isQueryAll=='0'"> <![CDATA[and i.expectInvoiceTime<=NOW() and i.expectInvoiceTime>date_sub(now(),interval 1 month)]]> </if> <if test="isReCreate=='1'"> and i.unusualType in ('0','2') </if> <if test="isCombine=='1'"> and i.invoiceStatus not in ("5","7") </if> </trim> </select>
(示例代码如上)
由于PageHelper分页总数是根据count(0)来计算出来的,而collection会把结果集合并(一对多的情况),计算总数在合并结果集之前,所以计算的总数不对
2.pageHelper遇到resultMap的collection后,分页结果集不对
同计数的原理一样,分页实际是在合并结果集之前进行的,这就导致了存在一对多情况时,实际返回的结果集个数与分页要求的每页结果集个数不一致,且如果最后一条记录也涉及一对多时,结果集内容也不一定准确
综上:需要分页时,resultMap不要嵌套collection;
另外,在PageHelper.startPage(request.getPageNum(), request.getPageSize());后面紧跟第一条SQL才会被分页(startPage与SQL中间不能有别的代码),且是线程安全的
分页修改后示例(把一对多的部分挪出去单独查询,不使用分页插件):
<resultMap id="InvoiceResultMap" type="com.hwht.invoicecenter.message.queryinvoicelist.Invoice"> <id column="serviceInvoiceBillNo" property="serviceInvoiceBillNo" jdbcType="VARCHAR"/> <result column="createTime" property="createTime" jdbcType="TIMESTAMP"/> <result column="customerCompany" property="customerCompany" jdbcType="VARCHAR"/> <result column="distributionStatus" property="distributionStatus" jdbcType="VARCHAR"/> <result column="distributionType" property="distributionType" jdbcType="VARCHAR"/> <result column="invoiceBillNo" property="invoiceBillNo" jdbcType="VARCHAR"/> <result column="invoiceMethod" property="invoiceMethod" jdbcType="VARCHAR"/> <result column="invoiceStatus" property="invoiceStatus" jdbcType="VARCHAR"/> <result column="invoiceTitle" property="invoiceTitle" jdbcType="VARCHAR"/> <result column="invoiceType" property="invoiceType" jdbcType="VARCHAR"/> <result column="recipient" property="recipient" jdbcType="VARCHAR"/> <result column="address" property="address" jdbcType="VARCHAR"/> <result column="issueInvoiceType" property="issueInvoiceType" jdbcType="VARCHAR"/> <result column="telephone" property="telephone" jdbcType="VARCHAR"/> <result column="totalPriceWithTax" property="totalPriceWithTax" jdbcType="VARCHAR"/> <result column="invoiceWeChatURL" property="invoiceWeChatURL" jdbcType="VARCHAR"/> </resultMap> <select id="queryInvoiceList" resultMap="InvoiceResultMap" parameterType="com.hwht.invoicecenter.message.queryinvoicelist.QueryInvoiceListRequest"> select d.address,i.createTime,i.customerCompany,d.distributionStatus,d.distributionType,i.invoiceBillNo, i.invoiceMethod,i.invoiceStatus,i.invoiceTitle,i.invoiceType,i.issueInvoiceType,d.recipient, i.serviceInvoiceBillNo,d.telephone,i.totalPriceWithTax,i.invoiceWeChatURL from t_invoice i join t_invoice_order o on i.serviceInvoiceBillNo=o.serviceInvoiceBillNo join t_invoice_distribution d on i.serviceInvoiceBillNo=d.serviceInvoiceBillNo <trim prefixOverrides="and" prefix="where"> <if test="orderID !=null"> and o.orderID=#{orderID,jdbcType=VARCHAR} </if> <if test="serviceInvoiceBillNo !=null"> and i.serviceInvoiceBillNo=#{serviceInvoiceBillNo,jdbcType=VARCHAR} </if> <if test="invoiceBillNo !=null"> and i.invoiceBillNo=#{invoiceBillNo,jdbcType=VARCHAR} </if> <if test="invoiceTitle !=null"> and i.invoiceTitle=#{invoiceTitle,jdbcType=VARCHAR} </if> <if test="recipient !=null"> and d.recipient=#{recipient,jdbcType=VARCHAR} </if> <if test="invoiceMethod !=null"> and i.invoiceMethod=#{invoiceMethod,jdbcType=VARCHAR} </if> <if test="invoiceType !=null"> and i.invoiceType=#{invoiceType,jdbcType=VARCHAR} </if> <if test="distributionType !=null"> and d.distributionType=#{distributionType,jdbcType=VARCHAR} </if> <if test="invoiceStatus !=null"> and i.invoiceStatus=#{invoiceStatus,jdbcType=VARCHAR} </if> <if test="address !=null"> <![CDATA[and d.address like concat('%',#{address,jdbcType=VARCHAR},'%')]]> </if> <if test="memberID !=null"> and i.memberID=#{memberID,jdbcType=VARCHAR} </if> <if test="enviromentFlag !=null"> and o.enviromentFlag=#{enviromentFlag,jdbcType=VARCHAR} </if> <if test="customerCompany !=null"> <![CDATA[and i.customerCompany like concat('%',#{customerCompany,jdbcType=VARCHAR},'%')]]> </if> </trim> order by i.createTime desc </select>
扫描二维码关注公众号,回复:
1518208 查看本文章
PageHelper.startPage(request.getPageNum(), request.getPageSize()); List<Invoice> invoiceList = tInvoiceMapper.queryInvoiceList(request); for (Invoice invoice : invoiceList) { invoice.setInvoiceOrderList(tInvoiceOrderMapper.queryByOrderIDAndServiceInvoiceBillNo(invoice.getServiceInvoiceBillNo(), request.getOrderID())); } if (!CollectionUtils.isEmpty(invoiceList)) { PageInfo<Invoice> pageInfo = new PageInfo<>(invoiceList); response.setInvoiceList(invoiceList); response.setPageNum(pageInfo.getPageNum()); response.setPageSize(pageInfo.getSize()); response.setTotalPage(pageInfo.getPages()); response.setTotalSize(pageInfo.getTotal()); }pageHelper配置:
pagehelper.row-bounds-with-count=true pagehelper.support-methods-arguments=true pagehelper.closeConn=false pagehelper.helperDialect=mysql