1.介绍
PageHelper 是一款非常流行的 MyBatis 分页插件,可以帮助开发者在进行数据库查询时实现分页功能,提升系统性能和用户体验。
2.安装
首先,需要在项目的 Maven 配置文件(pom.xml)中添加 PageHelper 的依赖:
<!--pagehelper分页插件-->
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper</artifactId>
<version>{version}</version>
</dependency>
3.配置
在 MyBatis 的配置文件中添加 PageHelper 插件的配置:在 Spring Boot 项目中,你可以通过 Java 配置来添加分页插件:
@Configuration
@MapperScan("scan.your.mapper.package")
public class MybatisPlusConfig {
/**
* 添加分页插件
*/
@Bean
public MybatisPlusInterceptor mybatisPlusInterceptor() {
MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL)); // 如果配置多个插件, 切记分页最后添加
// 如果有多数据源可以不配具体类型, 否则都建议配上具体的 DbType
return interceptor;
}
}
4.具体实现
编写controller层代码,
@RestController
public class EmpController {
// 注释说明:EmpService的实例用于与员工数据交互
private final EmpService empService;
// 通过构造方法注入EmpService,以便控制器可以调用服务层的方法
@Autowired
public EmpController(EmpService empService) {
this.empService = empService;
}
/**
* 根据指定的参数分页查询员工信息。
*
* @param currentPage 当前页码,默认为1。
* @param pageSize 每页显示的记录数,默认为10。
* @param firstName 姓氏,可选参数。
* @param departmentId 部门ID,可选参数。
* @param hireDate 招聘日期,可选参数,格式为"yyyy-MM-dd"。
* @return 返回分页查询结果,包含员工信息。
*/
@GetMapping("/emp")
public Result<Serializable> search(
@RequestParam(value = "currentPage", defaultValue = "1") int currentPage,
@RequestParam(value = "pageSize", defaultValue = "10") int pageSize,
@RequestParam(value = "firstName", required = false) String firstName,
@RequestParam(value = "departmentId", required = false) String departmentId,
@RequestParam(value = "hireDate", required = false)@DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate hireDate
) {
// 调用服务层方法,根据参数进行员工信息的分页查询
IPage<Emp> empIPage = empService.getEmpByPage(currentPage, pageSize, firstName, departmentId, hireDate);
// 将查询结果封装成成功响应并返回
return Result.success(empIPage);
}
}
在service层的接口创建search接口
/**
* 员工服务接口,继承自IService<Emp>,定义了针对员工实体的业务操作。
* 该接口主要用于员工信息的分页查询,支持根据员工名字、部门ID和雇佣日期进行过滤。
*/
public interface EmpService extends IService<Emp> {
IPage<Emp> getEmpByPage(int currentPage, int pageSize, String firstName, String departmentId, LocalDate hireDate);
}
在service层的实现类里实现search方法
/**
* 员工服务实现类,提供关于员工的业务操作实现。
*/
@Service
public class EmpServiceImpl extends ServiceImpl<EmpMapper, Emp>
implements EmpService{
/**
* 自动注入员工Mapper,用于执行数据库操作。
*/
@Autowired
private EmpMapper empMapper;
/**
* 根据条件分页查询员工信息。
*
* @param currentPage 当前页码
* @param pageSize 每页显示数量
* @param firstName 姓氏
* @param departmentId 部门ID
* @param hireDate 入职日期
* @return 分页查询结果
*/
@Override
public IPage<Emp> getEmpByPage(int currentPage, int pageSize, String firstName, String departmentId, LocalDate hireDate) {
// 创建分页对象
Page<Emp> page = new Page<>(currentPage, pageSize);
// 构建查询条件
QueryWrapper<Emp> queryWrapper = new QueryWrapper<>();
if (firstName != null && !firstName.equals("")) {
queryWrapper.like("firstName", firstName);
}
if (departmentId != null && !departmentId.equals("")) {
queryWrapper.eq("departmentId", departmentId);
}
if (hireDate != null) {
queryWrapper.ge("hireDate", hireDate);
}
// 执行分页查询
return empMapper.selectPage(page, queryWrapper);
}
}
在mapper层定义selectPageVo 执行分页查询的接口
/**
* 员工映射接口,继承自BaseMapper,用于对员工实体进行数据库操作。
* @Mapper 注解告知MyBatis Plus将此接口注册为一个映射器,用于SQL的自动映射。
*/
@Mapper
public interface EmpMapper extends BaseMapper<Emp> {
Object selectPageVo(Page<Emp> page, QueryWrapper<Emp> queryWrapper, String firstName, String departmentId, LocalDate hireDate);
}
在EmpMapper.xml文件中实现electPageVo接口,编写实际的SQL语句
<select id="selectPageVo" parameterType="com.example.yourpackage.EmpQueryCriteria" resultMap="BaseResultMap">
SELECT *
FROM emp
<where>
<!-- firstName的模糊查询 -->
<if test="firstName != null and firstName != ''">
AND firstName LIKE concat('%', #{firstName}, '%')
</if>
<!-- departmentId的确切匹配查询 -->
<if test="departmentId != null">
AND departmentId = #{departmentId}
</if>
<!-- hireDate的范围查询 -->
<if test="startDate != null and endDate != null">
AND hireDate BETWEEN #{startDate} AND #{endDate}
</if>
<!-- 只有startDate -->
<if test="startDate != null and endDate == null">
AND hireDate >= #{startDate}
</if>
<!-- 只有endDate -->
<if test="startDate == null and endDate != null">
AND hireDate <= #{endDate}
</if>
</where>
</select>