public class MyPageHelper { // 调用pageInfo插件内的方法 开启分页 需要传入一个起始页的值和每页显示的条数 public static void startPage(PageRequest pageRequest) { PageHelper.startPage(pageRequest.getPageNum(),pageRequest.getPageSize()); } // 封装结果集 public static PageResult getPageResult(List content) { // 创建分页对象 将结果传入对象中 pageInfo对象中生成信息 PageInfo pageInfo = new PageInfo<>(content); // 创建返回的对象 PageResult pageResult = new PageResult(); // 获取总页数 pageResult.setTotal(pageInfo.getTotal()); // 获取结果 pageResult.setContent(pageInfo.getList()); // 获取总页数 pageResult.setPages(pageInfo.getPages()); // 获取起始页数 pageResult.setPageNum(pageInfo.getPageNum()); // 获取每页显示的条数 pageResult.setPageSize(pageInfo.getPageSize()); // 返回结果 return pageResult; } }
public class PageRequest { /** *页数 */ private int pageNum; /** * 每页的信息的个数 */ private int pageSize; public PageRequest() { } public PageRequest(int pageNum, int pageSize) { this.pageNum = pageNum; this.pageSize = pageSize; } /** * 获取 * @return pageNum */ public int getPageNum() { return pageNum; } /** * 设置 * @param pageNum */ public void setPageNum(int pageNum) { this.pageNum = pageNum; } /** * 获取 * @return pageSize */ public int getPageSize() { return pageSize; } /** * 设置 * @param pageSize */ public void setPageSize(int pageSize) { this.pageSize = pageSize; } public String toString() { return "PageRequest{pageNum = " + pageNum + ", pageSize = " + pageSize + "}"; } }
public class PageResult { /** *页数 */ private int pageNum; /** * 每页的信息的个数 */ private int pageSize; /** * 总条数 */ private Long total; /** * 总页数 */ private int pages; /** * 结果集 */ private List<?> content; public PageResult() { } public PageResult(int pageNum, int pageSize, Long total, int pages, List<?> content) { this.pageNum = pageNum; this.pageSize = pageSize; this.total = total; this.pages = pages; this.content = content; } /** * 返回结果调用的方法 */ public static PageResult getPageResult(List content,PageRequest pageRequest,Long total) { // 创建返回的对象 PageResult pageResult = new PageResult(); // 将分页后查询到的结果放入对象中 pageResult.setContent(content); // 将起始页放入对象中 pageResult.setPageNum(pageRequest.getPageNum()); // 将每页显示的条数放入对象中 pageResult.setPageSize(pageRequest.getPageSize()); // 将总条数放入对象中 pageResult.setTotal(total); // 返回结果 return pageResult; } /** * 获取 * @return pageNum */ public int getPageNum() { return pageNum; } /** * 设置 * @param pageNum */ public void setPageNum(int pageNum) { this.pageNum = pageNum; } /** * 获取 * @return pageSize */ public int getPageSize() { return pageSize; } /** * 设置 * @param pageSize */ public void setPageSize(int pageSize) { this.pageSize = pageSize; } /** * 获取 * @return total */ public Long getTotal() { return total; } /** * 设置 * @param total */ public void setTotal(Long total) { this.total = total; } /** * 获取 * @return pages */ public int getPages() { return pages; } /** * 设置 * @param pages */ public void setPages(int pages) { this.pages = pages; } /** * 获取 * @return content */ public List<?> getContent() { return content; } /** * 设置 * @param content */ public void setContent(List<?> content) { this.content = content; } public String toString() { return "PageResult{pageNum = " + pageNum + ", pageSize = " + pageSize + ", total = " + total + ", pages = " + pages + ", content = " + content + "}"; } }
三个实体类写完了后,接下来就是mapper层的编写
@Mapper public interface StudentDao extends BaseMapper<Student> { List<Student> selectAllChd(PageRequest pageRequest); }
service的编写与实现类的编写
public interface StudentService extends IService<Student> { List<Student> selectAllChd(PageRequest pageRequest); }
@Service public class StudentServiceImpl extends ServiceImpl<StudentDao, Student> implements StudentService { @Resource StudentDao studentDao; @Override public List<Student> selectAllChd(PageRequest pageRequest) { return studentDao.selectAllChd(pageRequest); } }
再接着是controller的编写
@GetMapping("/query") @ResponseBody public PageResult queryList() {//分页写出来了 System.out.println("进来了query"); PageRequest pageRequest=new PageRequest(1,3); int pageNum = pageRequest.getPageNum(); pageRequest.setPageNum((pageRequest.getPageSize()*(pageRequest.getPageNum()-1))); List<Student> carManagerEntityList =studentService.selectAllChd(pageRequest); pageRequest.setPageNum(pageNum); Long total = studentService.count(); return PageResult.getPageResult(carManagerEntityList,pageRequest,total); }
mybatis-plus虽然使用起来方便,但是俩表映射就不能使用自带的分页了,所以需要小伙伴自己手动写,下面是映射语句
<resultMap type="com.cskt.entity.Student" id="StudentMap"> <result property="sId" column="s_id" jdbcType="INTEGER"/> <result property="sName" column="s_name" jdbcType="VARCHAR"/> <result property="sSex" column="s_sex" jdbcType="VARCHAR"/> <association property="sClass" resultMap="ClassMap"></association> </resultMap> <resultMap id="ClassMap" type="com.cskt.entity.StuClass"> <id property="cId" column="c_id"></id> <result property="cName" column="c_name"></result> </resultMap> <select id="selectAllChd" resultMap="StudentMap"> SELECT * FROM `student` as stu join stu_class as sts on stu.s_class=sts.c_id limit #{pageNum} ,#{pageSize} </select>
项目结构如下
查询效果如下,分页成功,并且俩表映射成功,希望可以帮助到各位小伙伴