导入Pagehelper的依赖
<!--分页插件-->
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper-spring-boot-starter</artifactId>
<version>1.3.0</version>
</dependency>
配置yml依赖
# pagehelper
pagehelper:
#设置方言,此处指定 MySQL 数据库
helper-dialect: mysql
# 是否启动合理化,默认是 false。
# 启用合理化时,如果pageNum<1会查询第一页,如果pageNum>pages(最大页数)会查询最后一页。
# 禁用合理化时,如果pageNum<1或pageNum>pages会返回空数据
reasonable: true
# 是否支持接口参数来传递分页参数,默认false
support-methods-arguments: true
#为了支持startPage(Object params)方法,增加了该参数来配置参数映射,用于从对象中根据属性名取值
params: count=countSql
# 默认值为 false,当该参数设置为 true 时,如果 pageSize=0 或者 RowBounds.limit = 0 就会查询出全部的结果(相当于没有执行分页查询,但是返回结果仍然是 Page 类型)
page-size-zero: true
PageInfo
PageInfo
是一个用于封装分页相关信息的对象,它通常用在需要分页显示数据的场景中。PageInfo
对象允许前端应用更容易地获取和展示分页数据,同时也让后端的分页逻辑更加清晰。在Java开发中,尤其是使用MyBatis分页插件PageHelper时,PageInfo
这个类会被频繁使用。
一个典型的PageInfo
对象可能会包含以下属性(不局限于此,具体属性可能根据实现有所不同):
pageNum
: 当前页码,表示用户正在查看的页数。pageSize
: 每页显示记录数,表示每页要显示的数据条数。size
: 当前页的记录数,可能会小于pageSize
,尤其是在最后一页的情形下。startRow
和endRow
: 分别表示当前页的开始和结束行的行号(在全部数据集中的位置)。total
: 数据总记录数,表示总共有多少条数据。pages
: 总页数,根据total
和pageSize
计算得到。prePage
和nextPage
: 分别表示上一页和下一页的页码。isFirstPage
和isLastPage
: 指示当前页是否为第一页或最后一页的布尔值。hasPreviousPage
和hasNextPage
: 表示是否存在上一页或下一页的布尔值。list
: 当前页中包含的数据列表,保存了当前页面要展示的具体数据。
PageInfo
类通过封装这些属性,提供了一种便捷的方式来处理和显示分页数据。开发者可以通过获取PageInf。
对象的这些属性值,方便地实现分页功能,并在前端展示分页组件,如页码条、上一页下一页按钮等。
Controller层
package com.mbc.controller;
@RestController
@RequestMapping("/publishAnimal")
@Api(tags = "用户发布流浪动物接口")
@CrossOrigin
public class UserPublishedAnimalController {
@Autowired
private UserPublishedAnimalService userPublishedAnimalService;
@GetMapping("/RoughAnimalInfo")
@ApiOperation("分类查询流浪动物")
public Result<PageInfo<RoughAnimalInfoVO>> getByRoughAnimalInfo(RoughAnimalInfoDTO roughAnimalInfoDTO, @RequestParam(defaultValue = "1") Integer page , @RequestParam(defaultValue = "10") Integer size){
PageInfo<RoughAnimalInfoVO> list = userPublishedAnimalService.getByRoughAnimalInfo(roughAnimalInfoDTO,page,size);
return Result.success(list);
}
}
service层
package com.mbc.service;
public interface UserPublishedAnimalService {
PageInfo<RoughAnimalInfoVO> getByRoughAnimalInfo(RoughAnimalInfoDTO roughAnimalInfoDTO, Integer page, Integer size);
}
serviceimpl层
package com.mbc.service.impl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.mbc.dto.RoughAnimalInfoDTO;
import com.mbc.mapper.UserPublishedAnimalMapper;
import com.mbc.service.UserPublishedAnimalService;
import com.mbc.vo.DetailsAnimalInfoVO;
import com.mbc.vo.RoughAnimalInfoVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.time.LocalDateTime;
import java.util.ArrayList;
/**
* <p>
* 用户发布流浪动物表 服务实现类
* </p>
*
* @author suimeng
* @since 2024-04-15
*/
@Service
public class UserPublishedAnimalServiceImpl implements UserPublishedAnimalService {
@Autowired
private UserPublishedAnimalMapper userPublishedAnimalMapper;
@Override
public PageInfo<RoughAnimalInfoVO> getByRoughAnimalInfo(RoughAnimalInfoDTO roughAnimalInfoDTO, Integer page,
Integer size) {
// 设置分页参数
PageHelper.startPage(page, size);
// 进行查询
ArrayList<RoughAnimalInfoVO> list = userPublishedAnimalMapper.getByRoughAnimalInfo(roughAnimalInfoDTO, LocalDateTime.now());
return new PageInfo<>(list);
}
}
}
Mapper层
package com.mbc.mapper;
@Mapper
public interface UserPublishedAnimalMapper {
ArrayList<RoughAnimalInfoVO> getByRoughAnimalInfo(RoughAnimalInfoDTO roughAnimalInfoDTO, LocalDateTime now);
}
xml层
<select id="getByRoughAnimalInfo" resultType="com.mbc.vo.RoughAnimalInfoVO">
select a.description,a.location,a.status,a.last_modified_at,b.animal_name,b.sex,
<!-- ST_Distance_Sphere表示一个经纬度函数,并给他们起一个别名-->
ST_Distance_Sphere(point(#{roughAnimalInfoDTO.longitude}, #{roughAnimalInfoDTO.latitude}), point(c.longitude,
c.latitude)) as distance
from mbc_user_published_animal a
inner join mbc_animal_info b on a.animal_id = b.animal_id
inner join mbc_sign c on a.sign_id = c.sign_id
<where>
<if test="roughAnimalInfoDTO.kind == 0 or roughAnimalInfoDTO.kind == 1 or roughAnimalInfoDTO.kind == 2 or roughAnimalInfoDTO.kind == 3">
and b.kind = #{roughAnimalInfoDTO.kind}
</if>
<if test="roughAnimalInfoDTO.status == 1 or roughAnimalInfoDTO.status == 2 or roughAnimalInfoDTO.status == 3">
and a.status = #{roughAnimalInfoDTO.status}
</if>
<if test="roughAnimalInfoDTO.dynamicUpdate == 0">
and a.last_modified_at >= DATE_SUB(#{now},interval 24 Hour)
</if>
<if test="roughAnimalInfoDTO.dynamicUpdate == 1">
and a.last_modified_at >= DATE_SUB(#{now},interval 7 DAY)
</if>
<if test="roughAnimalInfoDTO.dynamicUpdate == 2">
and a.last_modified_at >= DATE_SUB(#{now},interval 1 MONTH)
</if>
</where>
<if test="roughAnimalInfoDTO.minDistance != null and roughAnimalInfoDTO.maxDistance != null">
having distance between #{roughAnimalInfoDTO.minDistance} * 1000 and #{roughAnimalInfoDTO.maxDistance} *
1000
</if>
order by distance ASC