Java Mybatis行边界,集合在resultMap内未返回预期结果

Java Mybatis行边界,集合在resultMap内未返回预期结果,java,pagination,mybatis,Java,Pagination,Mybatis,Mapper.xml 这样,对于包含3条记录的第1页,将使用RowBounds0,3调用它。对于第2页,它用RowBounds3,3调用,对于第3页,用RowBounds6,3调用 mybatis日志中上述函数的SQL查询返回以下结果 id code description orderId ------------------------------------------- 1 101 Samolina 200 1

Mapper.xml

这样,对于包含3条记录的第1页,将使用RowBounds0,3调用它。对于第2页,它用RowBounds3,3调用,对于第3页,用RowBounds6,3调用

mybatis日志中上述函数的SQL查询返回以下结果

id      code      description      orderId
-------------------------------------------
1       101       Samolina         200
1       101       Samolina         201
2       102       Trampoline       300
2       102       Trampoline       301
2       102       Trampoline       302
2       102       Trampoline       303
3       103       Pajar            401
3       103       Pajar            402
4       104       Tramtor          500
4       104       Tramtor          501
4       104       Tramtor          502
5       105       Wadnor           600
5       105       Wadnor           601
5       105       Wadnor           602
5       105       Wadnor           603
6       106       Bramget          701
6       106       Bramget          702
但是,当mybatis应用分页和收集时,在第一页上可以看到以下记录,这很好

id      code      description      orderIds
-------------------------------------------
1       101       Samolina         (200,201)
2       102       Trampoline       (300,301,302,303)
3       103       Pajar            (401,402)
因此,对于第二页,记录应该从id=4开始。然而,这是mybatis为第二页返回的内容

id      code      description      orderIds
-------------------------------------------
2       102       Trampoline       (301,302,303)
3       103       Pajar            (401,402)
4       104       Tramtor          (500,501,502)
现在,如果您查看实际查询返回的数据库,下面是第4条记录。这就是第二页真正开始的地方。注意第2页的第一条记录中缺少orderId 300。这是因为orderId=300是实际查询中的第三条记录,因此不包括它

id      code      description      orderId
-------------------------------------------
2       102       Trampoline       301

关于mybatis分页或集合,我有什么遗漏吗?

我在您的代码中看到了一些问题:

如果使用分页,则需要将ORDER BY添加到SQL中。当不存在时,SQL以任何顺序返回行,并且顺序可能/将随时间而改变。我认为这是您代码中的一个严重错误。 另外,最好在封闭实体的某些属性上指定标记。否则它会变得非常慢。 另外,使用子句代替where 1=1。MyBatis将删除第一个,并在需要时在运行时自动删除。 以下是地图绘制程序的修改版本:

<resultMap id="resultMap" type="dBWrapper">
    <id property="id" column="id" /> <!-- I used ID here -->
    <result property="code" column="code" />
    <result property="description" column="description" />
    <collection property="orderIds" ofType="java.lang.Long" >
        <result column="orderId" />
    </collection>
</resultMap>

<select id="selectAllProducts" resultMap="resultMap" parameterType="dBWrapper">
    SELECT product.id, product.code, product.description, 
      provider.providerName, order.orderId
    FROM Product product
    LEFT JOIN Order order on product.id = order.productId
    <where>
      <if test="orderSearchId != null"> 
        and order.orderId = #{orderSearchId}
      </if>
    </where>
    ORDER BY product.id, order.id
</select>

穿刺者有有效的分数。但它似乎无法解决这个问题

这个问题可以通过重写sql来解决。 基本上,您必须限制一些最终将表示从MyBatis mapper返回的实际实体的内部/中间表

或者,如果数据库中没有很多条目,则可以在检索所有条目时在代码中实现分页!项目

public List<Item> getItems(Paging paging) {
    List<Item> items = itemMapper.selectItems();
    if (paging != null) {
        items = items.stream()
                .skip((long) paging.pageIndex * paging.pageSize)
                .limit(paging.pageSize)
                .collect(Collectors.toList());
    }
    return items;
}

你解决了这个问题吗?我也有同样的问题。穿刺者的回答有一些正确的观点,但不能解决问题。在我看来,mybatis没有简单的方法知道如何设置查询限制,因为与结果中单个项对应的行数是可变的。
<resultMap id="resultMap" type="dBWrapper">
    <id property="id" column="id" /> <!-- I used ID here -->
    <result property="code" column="code" />
    <result property="description" column="description" />
    <collection property="orderIds" ofType="java.lang.Long" >
        <result column="orderId" />
    </collection>
</resultMap>

<select id="selectAllProducts" resultMap="resultMap" parameterType="dBWrapper">
    SELECT product.id, product.code, product.description, 
      provider.providerName, order.orderId
    FROM Product product
    LEFT JOIN Order order on product.id = order.productId
    <where>
      <if test="orderSearchId != null"> 
        and order.orderId = #{orderSearchId}
      </if>
    </where>
    ORDER BY product.id, order.id
</select>
public List<Item> getItems(Paging paging) {
    List<Item> items = itemMapper.selectItems();
    if (paging != null) {
        items = items.stream()
                .skip((long) paging.pageIndex * paging.pageSize)
                .limit(paging.pageSize)
                .collect(Collectors.toList());
    }
    return items;
}