分页功能详解
接口请求:
获取分页数据所需函数:
startPage
selectApCommentsList
controller代码:
@RestController
@RequestMapping("/system/comments")
public class ApCommentsController extends BaseController
{
@Autowired
private IApCommentsService apCommentsService;
/**
* 查询评论列表
*/
@PreAuthorize("@ss.hasPermi('system:comments:list')")
@GetMapping("/list")
public TableDataInfo list(ApComments apComments)
{
startPage(); //
List<ApComments> list = apCommentsService.selectApCommentsList(apComments); //
return getDataTable(list);
}
/**
* 查询评论列表及发布此的用户信息
*/
ApCommentsMapper.xml
selectApCommentsList对应xml,没有使用pageNum 和pageSize 删选数据,那么它返回的应该是所有数据
<select id="selectApCommentsList" parameterType="ApComments" resultMap="ApCommentsResult">
<include refid="selectApCommentsVo"/>
<where>
<if test="videoId != null "> and video_id = #{videoId}</if>
<if test="content != null and content != ''"> and content = #{content}</if>
<if test="peopleId != null "> and people_id = #{peopleId}</if>
<if test="comTime != null "> and com_time = #{comTime}</if>
<if test="isEnabled != null "> and is_enabled = #{isEnabled}</if>
</where>
</select>
<select id="selectApCommentsListMsg" parameterType="ApComments" resultMap="ApCommentsMsgResult">
select
a.Id,
a.video_id,
a.content,
a.people_id,
a.com_time,
a.is_enabled,
a.remark,
b.name as people,
b.picture as people_head
from
ap_comments a
left join ap_people b on a.people_id = b.Id
<where>
<if test="videoId != null "> and video_id = #{videoId}</if>
</where>
order by a.Id desc
</select>
startPage()函数:
MyBatis 通过startPage 中的函数PageHelper.startPage做到了对SQL语句的修改:
protected void startPage()
{
PageDomain pageDomain = TableSupport.buildPageRequest();
Integer pageNum = pageDomain.getPageNum();
Integer pageSize = pageDomain.getPageSize();
if (StringUtils.isNotNull(pageNum) && StringUtils.isNotNull(pageSize))
{
String orderBy = SqlUtil.escapeOrderBySql(pageDomain.getOrderBy());
Boolean reasonable = pageDomain.getReasonable();
PageHelper.startPage(pageNum, pageSize, orderBy).setReasonable(reasonable);
}
}
getDataTable()函数:
new PageInfo(list).get Total();的total 到底是多少呢?怎么得到的呢?
total 的值是表格所有数据的总行数
MyBatis 通过修改SQL 语句,多请求了一句SQL 语句:SELECT count(0) FROM sys_oper_log 返回了总行数给到Page 对象的total 成员。然后通过子类Page 强转父类List 对象,访问其成员total 值,赋值给PageInfo 父类成员total,即this.total = ((Page)list).getTotal();
protected TableDataInfo getDataTable(List<?> list)
{
TableDataInfo rspData = new TableDataInfo();
rspData.setCode(HttpStatus.SUCCESS);
rspData.setMsg("查询成功");
rspData.setRows(list);
rspData.setTotal(new PageInfo(list).getTotal());
return rspData;
}
public PageSerializable(List<T> list) {
this.list = list;
if (list instanceof Page) {
this.total = ((Page)list).getTotal();
} else {
this.total = (long)list.size();
}
}
获取分页数据所需SQL代码解析
LIMIT
请求后端的Log
可以看到limit后面有有两个参数,分别是两个10
LIMIT的两种常用用法:
不指定初始位置
LIMIT 关键字不指定初始位置时,记录默认从第一条记录开始显示。显示记录的条数由 LIMIT 关键字指定。
LIMIT 不指定初始位置的基本语法格式如下:
LIMIT 记录数
其中,“记录数”表示显示记录的条数。如果“记录数”的值小于查询结果的总数,则会从第一条记录开始,显示指定条数的记录。如果“记录数”的值大于查询结果的总数,则会直接显示查询出来的所有记录。
指定初始位置
LIMIT 关键字可以指定查询结果从哪条记录开始显示,显示多少条记录。
LIMIT 指定初始位置的基本语法格式如下:
LIMIT 初始位置, 记录数
其中,“初始位置”表示从哪条记录开始显示;“记录数”表示显示记录的条数。第一条记录的位置是 0,第二条记录的位置是 1。后面的记录依次类推。
注意:LIMIT 后的两个参数必须都是正整数。
LIMIT的两个参数代表的含义
因此可以得出两个参数代表的含义,第一个10代表从第11个记录开始,第二个10代表查询出之后的10个记录,最后一行的total返回值,代表了在该limit分页下,查询返回了1条数据(因为第二页只有一条数据)