若本身 a表又15条数据,left join 之后,会查出来20条数据,那么你上面得数据,返回得是多少条呢,

在你所描述的场景中,如果 a 表有 15 条数据,经过 LEFT JOIN 之后查询结果是 20 条数据,这说明 LEFT JOIN 产生了重复记录。如果你希望分页是基于 a 表的记录数而不是 LEFT JOIN 后的总记录数,那么需要确保每一页的数据量是基于 a 表的记录数,而不是 LEFT JOIN 后的总记录数。

为了实现这一点,可以通过以下步骤:

  1. 获取 a 表的分页记录:首先进行 a 表的分页查询,以确定分页逻辑。
  2. 基于分页后的 a 表记录进行 LEFT JOIN 查询:使用获取到的 a 表分页记录的 ID 列表进行 LEFT JOIN 查询,从而避免多余的记录。

实现步骤

1. 在 Mapper 中编写 SQL 查询

你需要两个查询:一个是仅查询 a 表的分页数据,另一个是基于 a 表 ID 列表的 LEFT JOIN 查询。

仅查询 a 表的分页数据
<select id="selectAPaged" resultType="YourAType">
    SELECT a.*
    FROM tableA a
    <!-- 其他条件 -->
</select>
基于 a 表 ID 列表的 LEFT JOIN 查询
<select id="selectWithLeftJoinByIds" resultType="YourResultType">
    SELECT a.*, b.*
    FROM tableA a
    LEFT JOIN tableB b ON a.id = b.a_id
    WHERE a.id IN
    <foreach item="item" index="index" collection="list" open="(" separator="," close=")">
        #{item}
    </foreach>
</select>
2. 在 Service 层实现分页查询逻辑

在 Service 层中,首先获取 a 表的分页数据,然后基于这些数据获取详细信息。

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class YourService {

    @Autowired
    private YourMapper yourMapper;

    public PageInfo<YourResultType> getPagedResults(int pageNum, int pageSize) {
        // 设置分页参数
        PageHelper.startPage(pageNum, pageSize);
        // 查询表 a 的分页数据
        List<YourAType> aPagedResults = yourMapper.selectAPaged();
        PageInfo<YourAType> pageInfoA = new PageInfo<>(aPagedResults);

        // 获取表 a 的 ID 列表
        List<Integer> aIds = aPagedResults.stream().map(YourAType::getId).collect(Collectors.toList());

        // 基于表 a 的 ID 列表查询包含 LEFT JOIN 的详细数据
        List<YourResultType> detailedResults = yourMapper.selectWithLeftJoinByIds(aIds);

        // 创建 PageInfo 对象来返回分页信息和详细结果
        PageInfo<YourResultType> pageInfo = new PageInfo<>(detailedResults);
        pageInfo.setTotal(pageInfoA.getTotal());
        pageInfo.setPageNum(pageInfoA.getPageNum());
        pageInfo.setPageSize(pageInfoA.getPageSize());
        pageInfo.setPages(pageInfoA.getPages());

        return pageInfo;
    }
}
3. 在 Controller 层调用 Service 方法

在 Controller 层中调用 Service 方法并返回结果:

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import com.github.pagehelper.PageInfo;

@RestController
@RequestMapping("/yourApi")
public class YourController {

    @Autowired
    private YourService yourService;

    @GetMapping("/getPagedResults")
    public PageInfo<YourResultType> getPagedResults(@RequestParam int pageNum, @RequestParam int pageSize) {
        return yourService.getPagedResults(pageNum, pageSize);
    }
}

总结

通过这种方法,你可以确保分页逻辑是基于 a 表的记录数,而 LEFT JOIN 后的结果仅基于当前页的 a 表记录。这可以避免因为 LEFT JOIN 产生的重复记录而导致的分页不准确问题。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值