AI智能
改变未来

mybaits-plus使用分页插件关联查询时,返回结果数量不对扽问题


分页插件返回数量不对的问题

  1. 如果是对单表的查询,那么使用分页插件是没有问题的。

  2. 如果是有表的关联查询,那么分页插件实际上是对中间表作的一个分页,那么就会造成分页数量不对的问题。

<resultMap id=\"BaseResultMap\" type=\"com.zzhua.vadmin.entity.Order\"><result property=\"userId\" column=\"user_id\"></result><result property=\"createdTime\" column=\"created_time\"></result><result property=\"orderId\" column=\"order_id\"></result><result property=\"orderAddr\" column=\"order_addr\"></result><result property=\"orderFlag\" column=\"order_flag\"></result><result property=\"orderPrice\" column=\"orderPrice\"></result><result property=\"orderTel\" column=\"order_tel\"></result><result property=\"orderPrice\" column=\"order_price\"></result><result property=\"updatededTime\" column=\"updated_time\"></result></resultMap>

错误示例:

<resultMap id=\"OrderVoMap\" type=\"com.zzhua.vadmin.vo.OrderVo\" extends=\"BaseResultMap\"><collection property=\"orderinfoList\" ofType=\"com.zzhua.vadmin.entity.Orderinfo\"><result property=\"orderId\" column=\"order_id\"></result><result property=\"proId\" column=\"pro_id\"></result><result property=\"pcount\" column=\"pcount\"></result></collection></resultMap>List<OrderVo> queryOrder(@Param(\"order\") Order order, Page page);// 关联查询,得到中间表。一个订单会包含多个订单详情,那么就会造成中间表,同一个订单算成了2条记录,这样分页就出错了<select id=\"queryOrder\" resultMap=\"OrderVoMap\">select o.*,oi.* from t_order o left join t_orderinfo oi on o.order_id = oi.order_id<where><if test=\"order!=null and order.userId!=null \">and o.user_id =#{order.userId,jdbcType=BIGINT}</if><if test=\"order!=null and order.orderId!=null \">and o.order_id = #{order.orderId,jdbcType=BIGINT}</if><if test=\"order!=null and order.orderFlag!=null\">and o.order_flag = #{order.orderFlag,jdbcType=INTEGER}</if><if test=\"order!=null and order.orderTel!=null and order.orderTel!=\'\'\">and o.order_tel like  CONCAT(\'%\',o.order_tel,\'%\')</if></where>order by created_time desc</select>

正确示例:

<resultMap id=\"OrderVoMap2\" type=\"com.zzhua.vadmin.vo.OrderVo\" extends=\"BaseResultMap\"><collection property=\"orderinfoList\"ofType=\"com.zzhua.vadmin.entity.Orderinfo\"column=\"order_id\"select=\"com.zzhua.vadmin.mapper.OrderinfoMapper.queryOrderinfosByOrderId\"></collection></resultMap>List<OrderVo> queryOrder2(@Param(\"order\") Order order, Page page);<select id=\"queryOrder2\" resultMap=\"OrderVoMap2\">select * from t_order o<where><if test=\"order!=null and order.userId!=null \">and o.user_id =#{order.userId,jdbcType=BIGINT}</if><if test=\"order!=null and order.orderId!=null \">and o.order_id = #{order.orderId,jdbcType=BIGINT}</if><if test=\"order!=null and order.orderFlag!=null\">and o.order_flag = #{order.orderFlag,jdbcType=INTEGER}</if><if test=\"order!=null and order.orderTel!=null and order.orderTel!=\'\'\">and o.order_tel like  CONCAT(\'%\',o.order_tel,\'%\')</if></where>order by created_time desc</select>-----------------------------------------------------------------------------------------------------public interface OrderinfoMapper extends BaseMapper<Orderinfo> {List<Orderinfo> queryOrderinfosByOrderId(@Param(\"orderId\")Long orderId);}<select id=\"queryOrderinfosByOrderId\" resultType=\"com.zzhua.vadmin.entity.Orderinfo\">select * from t_orderinfo where order_id = #{orderId}</select>-----------------------------------------------------------------------------------------------------

赞(0) 打赏
未经允许不得转载:爱站程序员基地 » mybaits-plus使用分页插件关联查询时,返回结果数量不对扽问题