EP框架之非入侵分页查询

  |   0 评论   |   0 浏览

  分页查询是日常开发中用的最平凡的点了吧,几乎数据量稍微大一点的表,都会用到分页查询,EP框架的分页查询,是基于pagehelper实现的非入侵式分页查询,那么什么叫非入侵式分页查询呢?待会代码样例中解释。EP框架中已经引入了pagehelper的包,这里项目中不用再重复引入。下面就以Demo样例中的学生列表查询代码为例,讲解分页查询:
  Controller中的方法:

/**
 * 查询学生列表
 *
 * @param condition the condition
 * @return the result
 */
@GetMapping("/students")
public Result queryStudentList(StudentConditionDto condition) {
	PageUtils.setPage(condition);//建议在service外层设置分页参数,这样查询列表的方法可以重用,例如在excel导出时候不需要分页
	return Result.ok(new PageInfo<>(tableDemoService.queryStudentList(condition)));
}

  StudentConditionDto对象:

import io.github.junxworks.ep.core.Pageable;

public class StudentConditionDto extends Pageable {
	private String studentName;
	private String studentNo;
	private Byte status;
	public String getStudentName() {
		return studentName;
	}
	public void setStudentName(String studentName) {
		this.studentName = studentName;
	}
	public String getStudentNo() {
		return studentNo;
	}
	public void setStudentNo(String studentNo) {
		this.studentNo = studentNo;
	}
	public Byte getStatus() {
		return status;
	}
	public void setStatus(Byte status) {
		this.status = status;
	}
}

  Service方法:

	@Override
	public List<StudentVo> queryStudentList(StudentConditionDto condition) {
		return tableDemoMapper.queryStudentListByCondition(condition);
	}

  StudentConditionDto需要继承Pageable对象,pageable中就两个属性,一个是页数,一个是分页大小。Controller中的方法其实很简单,接收StudentConditionDto入参,使用PageUtils.setPage(condition),初始化pageHelper的分页参数,通过PageInfo对象来接收返回的列表即可,service的查询里面,不需要体现分页查询的任何逻辑,这里就当成普通查询查就好了,到这里分页查询就算完成了。为什么叫非入侵的分页查询呢?因为对分页的控制,始终是在controller层,service和mapper其实对外面分没分页不敏感,这样有一个好处,service同样的方法,可能有些地方需要分页,例如前端查询页面,有些功能又不需要分页,例如Excel服务器端导出,这样service的通用性会更强一点。


标题:EP框架之非入侵分页查询
作者:michael
地址:https://blog.junxworks.cn/articles/2021/04/04/1617523583571.html