在 MyBatis 中使用 PageHelper
进行分页并同时使用 LEFT JOIN
操作,可以通过以下步骤确保获取到精准的总条数:
-
配置
PageHelper
插件:确保你的 MyBatis 配置文件中已经配置了PageHelper
插件。 -
设置分页参数:在你的查询方法中,使用
PageHelper
的静态方法startPage
来设置分页参数。 -
执行查询:编写包含
LEFT JOIN
的 SQL 查询语句,并在对应的 Mapper 中调用该查询方法。 -
获取分页信息:通过
PageInfo
对象来获取分页信息,包括总条数。
具体示例如下:
1. 配置 PageHelper
插件
在 MyBatis 配置文件中添加 PageHelper
插件配置:
<plugins>
<plugin interceptor="com.github.pagehelper.PageInterceptor">
<property name="helperDialect" value="mysql"/>
</plugin>
</plugins>
2. 编写包含 LEFT JOIN
的 SQL 查询
在 Mapper XML 文件中编写 SQL 查询语句,例如:
<select id="selectWithLeftJoin" resultType="YourResultType">
SELECT a.*, b.*
FROM tableA a
LEFT JOIN tableB b ON a.id = b.a_id
<!-- 其他条件 -->
</select>
3. 在 Service 层使用 PageHelper
进行分页查询
在你的 Service 层中使用 PageHelper
进行分页查询,并获取分页信息:
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import java.util.List;
@Service
public class YourService {
@Autowired
private YourMapper yourMapper;
public PageInfo<YourResultType> getPagedResults(int pageNum, int pageSize) {
// 设置分页参数
PageHelper.startPage(pageNum, pageSize);
// 执行查询
List<YourResultType> results = yourMapper.selectWithLeftJoin();
// 获取分页信息
PageInfo<YourResultType> pageInfo = new PageInfo<>(results);
return pageInfo;
}
}
4. 在 Controller 层调用 Service 方法
在你的 Controller 层中调用 Service 方法并返回结果:
@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);
}
}
总结
通过以上步骤,你可以使用 PageHelper
进行分页,并结合 LEFT JOIN
查询来获取精准的总条数信息。关键点在于通过 PageInfo
对象来获取分页信息,这样不仅能够得到查询结果,还能获得分页的详细信息(包括总条数)。
但如果我a表对应得b表数据是 一对多得关系,,分页是按照A表又多少条数据,就是多少条得,又办法来处理么
如果表 a
和表 b
是一对多的关系,并且你希望分页是按照表 a
的记录数进行,而不是表 b
的总记录数,那么可以通过以下方法来处理:
- 首先查询表
a
的记录数:这一步用于确定表a
的总记录数,并设置分页参数。 - 执行包含
LEFT JOIN
的分页查询:在这一步,分页逻辑仅针对表a
的记录数进行,而查询结果包括LEFT JOIN
的表b
的数据。
实现步骤
1. 在 Mapper 中编写 SQL 查询
你需要两个查询,一个是仅查询表 a
的记录数和记录的分页数据,另一个是包含 LEFT JOIN
的详细查询。
仅查询表 a
的分页数据
<select id="selectAPaged" resultType="YourAType">
SELECT a.*
FROM tableA a
<!-- 其他条件 -->
</select>
包含 LEFT JOIN
的详细查询
<select id="selectWithLeftJoin" 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;
@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.selectWithLeftJoin(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
的记录,这确保了分页逻辑是基于表 a
的记录数。然后,你使用这些记录的 ID 列表去查询包含 LEFT JOIN
的详细数据。最终,你可以通过 PageInfo
对象将分页信息和详细结果返回给前端。这样,你就实现了分页是按照表 a
的记录数进行的,而查询结果仍然包含 LEFT JOIN
的数据。
是否可以直接用一个SQL 就处理完毕呢,
可以直接使用一个 SQL 查询处理分页和 LEFT JOIN
操作,通过 PageHelper
插件来确保分页逻辑基于表 a
的记录数。这种方式在性能上可能更高效,且简化了代码逻辑。以下是实现步骤:
1. 配置 PageHelper
插件
确保在 MyBatis 配置文件中已经配置了 PageHelper
插件:
<plugins>
<plugin interceptor="com.github.pagehelper.PageInterceptor">
<property name="helperDialect" value="mysql"/>
</plugin>
</plugins>
2. 编写包含 LEFT JOIN
的分页查询
在 Mapper XML 文件中编写包含 LEFT JOIN
的分页查询语句。例如:
<select id="selectWithLeftJoinPaged" resultType="YourResultType">
SELECT a.*, b.*
FROM tableA a
LEFT JOIN tableB b ON a.id = b.a_id
<!-- 其他条件 -->
</select>
3. 在 Service 层使用 PageHelper
进行分页查询
在你的 Service 层中使用 PageHelper
进行分页查询,并获取分页信息:
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;
@Service
public class YourService {
@Autowired
private YourMapper yourMapper;
public PageInfo<YourResultType> getPagedResults(int pageNum, int pageSize) {
// 设置分页参数
PageHelper.startPage(pageNum, pageSize);
// 执行包含 LEFT JOIN 的分页查询
List<YourResultType> results = yourMapper.selectWithLeftJoinPaged();
// 获取分页信息
PageInfo<YourResultType> pageInfo = new PageInfo<>(results);
return pageInfo;
}
}
4. 在 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);
}
}
总结
通过上述步骤,你可以使用 PageHelper
插件和一个 SQL 查询实现分页和 LEFT JOIN
操作。这样,分页逻辑是基于表 a
的记录数,而查询结果包含了 LEFT JOIN
的数据。这种方式简化了代码逻辑,并提高了性能。