PageHelper一对多collection分页查询,总条数不正确问题

最近在项目中使用PageHelper进行一对多关联查询的时候,出现查询总条目数和实际的不对应

例:A实例对应6个B实例(如下代码),应该查询出来只有一条数据,但PageHelper显示6条数据

public class A{
    private List<B> appletList;
}

查询SQL编写如下:

实例A对应request表,实例B对应record表

<resultMap autoMapping="true" type="com.admin.pojos.question.A" id="queryRequestListByUids">
    <result column="id" property="id"/>
    <collection autoMapping="true" ofType="com.admin.entity.B" property="appletList">
        <result column="arid" property="id"/>
        <result column="appletId" property="appletId"/>
    </collection>
</resultMap>
<select id="selectRequestByUids" resultMap="queryRequestListByUids">
    select r.*,toar.applet_type appletType,toar.create_dt toarCreateDt
    from request r
    left join record toar on r._uid = toar.request_uid
    where r.is_del = 0 and r._uid in
</select>

查询结果:

返回数据能够正常将一对多的多放到List<B>里面,也只有一条数据,但是在使用page.getTotal()获取总条目的时候,得到的值,不是1,而是更多

解决方案(两种):

第一种:修改PageHelper工具

第二种(我使用的):使用子查询进行关联一对多,具体如下:

<resultMap autoMapping="true" type="com.admin.pojos.question.A" id="queryRequestListByUids">
    <result column="id" property="id"/>
   <!--子查询需传递多个参数,在column字段追加即可(column="{id=id,name=name})-->
    <collection property="appletList" ofType="com.admin.entity.B"
                select="selectAppletRecord" column="{requestUid=_uid}"/>
</resultMap>
<select id="selectRequestByUids" resultMap="queryRequestListByUids">
    select r.*
    from request r
</select>


<resultMap id="BatchListResultMap" type="com.admin.entity.B">
    <result column="arid" property="id"/>
    <result column="appletId" property="appletId"/>
</resultMap>
<select id="selectAppletRecord" resultMap="BatchListResultMap">
    select
        toar.applet_type appletType,toar.create_dt toarCreateDt
    from record toar
    where toar.request_uid = #{requestUid}
</select>

通过上述操作就可以成功实现一对多的映射查询,page.getTotal()获取总条目也是正确的。

但我在实际过程中,在实现上述操作之后,返回数据的时候,报了如下错误:

org.springframework.http.converter.HttpMessageConversionException: Type definition error: [simple type, class org.apache.ibatis.executor.loader.javassist.JavassistProxyFactory$EnhancedResultObjectProxyImpl]; nested exception is com.fasterxml.jackson.databind.exc.InvalidDefinitionException: No serializer found for class org.apache.ibatis.executor.loader.javassist.JavassistProxyFactory$EnhancedResultObjectProxyImpl and no properties discovered to create BeanSerializer (to avoid exception, disable SerializationFeature.FAIL_ON_EMPTY_BEANS) (through reference chain: com.core.common.RestResult["result"]->com.core.common.OutputListResult["data"]->com.github.PageHelperr.Page[0]->com.admin.pojos.question.OutputRequestInfo_$$_jvste21_0["handler"])
    at org.springframework.http.converter.json.AbstractJackson2HttpMessageConverter.writeInternal(AbstractJackson2HttpMessageConverter.java:293)
    at org.springframework.http.converter.AbstractGenericHttpMessageConverter.write(AbstractGenericHttpMessageConverter.java:103)
    at org.springframework.web.servlet.mvc.method.annotation.AbstractMessageConverterMethodProcessor.writeWithMessageConverters(AbstractMessageConverterMethodProcessor.java:289)
    at org.springframework.web.servlet.mvc.method.annotation.RequestResponseBodyMethodProcessor.handleReturnValue(RequestResponseBodyMethodProcessor.java:180)
    at org.springframework.web.method.support.HandlerMethodReturnValueHandlerComposite.handleReturnValue(HandlerMethodReturnValueHandlerComposite.java:82)
    at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:119)
    at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:895)
    at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:800)
    at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:87)
    at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:1038)
    at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:942)
    at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:998)
    at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:890)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
    at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
    at java.lang.Thread.run(Thread.java:748)
Caused by: com.fasterxml.jackson.databind.exc.InvalidDefinitionException: No serializer found for class org.apache.ibatis.executor.loader.javassist.JavassistProxyFactory$EnhancedResultObjectProxyImpl and no properties discovered to create BeanSerializer (to avoid exception, disable SerializationFeature.FAIL_ON_EMPTY_BEANS) (through reference chain: com.core.common.RestResult["result"]->com.core.common.OutputListResult["data"]->com.github.PageHelperr.Page[0]->com.admin.pojos.question.OutputRequestInfo_$$_jvste21_0["handler"])
    at com.fasterxml.jackson.databind.exc.InvalidDefinitionException.from(InvalidDefinitionException.java:77)
    at com.fasterxml.jackson.databind.SerializerProvider.reportBadDefinition(SerializerProvider.java:1191)
    at com.fasterxml.jackson.databind.DatabindContext.reportBadDefinition(DatabindContext.java:313)
    at org.springframework.http.converter.json.AbstractJackson2HttpMessageConverter.writeInternal(AbstractJackson2HttpMessageConverter.java:287)
    ... 95 more

从报错可以看出OutputRequestInfo中没有对应的handler字段(没有找到get/set方法),使用上述操作,会多了handler字段,我们需要忽略这个字段,只需在实例A上加上一个注解即可:

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;

@JsonIgnoreProperties(value = "handler")
public class A{
    private List<B> appletList;
}

  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
一对多查询的分页可以使用PageHelper插件来实现,具体步骤如下: 1.在查询方法中使用PageHelper.startPage(pageNum, pageSize)来启动分页pageNum是页码,pageSize是每页显示的记录数。 2.在查询语句中使用LEFT JOIN关联两个表,例如: ``` SELECT a.*, b.* FROM table_a a LEFT JOIN table_b b ON a.id = b.a_id WHERE ... ``` 3.将查询结果组装成你需要的数据结构,例如: ``` public class A { private Long id; private String name; private List<B> bList; // getter, setter } public class B { private Long id; private String name; private Long aId; // getter, setter } ``` 4.在MyBatis的配置文件中配置resultMap,例如: ``` <resultMap id="aResultMap" type="A"> <id property="id" column="a_id"/> <result property="name" column="a_name"/> <collection property="bList" ofType="B"> <id property="id" column="b_id"/> <result property="name" column="b_name"/> <result property="aId" column="b_a_id"/> </collection> </resultMap> ``` 5.在查询方法中使用PageInfo来对查询结果进行分页,例如: ``` PageHelper.startPage(pageNum, pageSize); List<A> aList = aMapper.selectList(); PageInfo<A> aPageInfo = new PageInfo<>(aList); ``` 6.最后将分页结果返回给前端即可,例如: ``` Map<String, Object> result = new HashMap<>(); result.put("list", aPageInfo.getList()); result.put("total", aPageInfo.getTotal()); result.put("pageNum", aPageInfo.getPageNum()); result.put("pageSize", aPageInfo.getPageSize()); ``` 希望这些步骤能够帮助您实现一对多查询的分页

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值