分页

分页:前端分页、后端分页
前端分页:一次请求数据表中的所有数据,然后在前端缓存并计算count和分页逻辑,适合小规模平台,当数据量大的时候会产生性能问题
后端分页:在ajax请求中指定页码和每页的大小,复杂一些,性能瓶颈在MySQL的查询,这个瓶颈可以通过调优解决,web开发使用的是这种方式

MySQL分页的支持只要通过limit关键字
limit只有一个参数的情况下

select * from 表 limit 5;			就是返回5条数据

limit有2个参数的时候

select * from 表 limit 0,10;			从第一位开始返回10条数据,下标从0开始计算    

继续从第11位开始展示

select * from 表 limit 10,10;			从下标10开始,返回10条数据,就是从第11条开始,返回10条数据

springboot中使用PageHelper分页插件,会有一个线程安全的问题
第一步:添加包,添加成功之后一定要刷新maven工程

    <dependency>
        <groupId>com.github.pagehelper</groupId>
        <artifactId>pagehelper-spring-boot-starter</artifactId>
        <version>1.2.2</version>
    </dependency>

该方法有3个参数
pageNumber 第一页就写1,第二页就写2
pageSize limit中的第二个参数
True 是否返回总行数

controller中

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping(value = "/MyBatisController")
public class MyBatisController {
@Autowired
MyInterfaceTestMapper myInterfaceTestMapper;
@RequestMapping("/selectForPage")
public @ResponseBody Object selectForPage(Integer pageNumber,Integer pageSize){
    //设置起始行数,每页几行,返回总行数
    Page page=PageHelper.startPage(pageNumber,pageSize,true);
    /**
     *方法括号里的是限定条件,null是没有条件,全表扫描
     * 调用接口中的查询方法,接口中的方法返回什么类型,这里就用什么类型接收
     * 接口方法的返回类型也是List<TestCase>
     */
    List<TestCase> interfaceTestList= myInterfaceTestMapper.getTestCase(null);
    //获取返回总数
    long total=page.getTotal();
    Map map= new HashMap();
    map.put("total", total);
    map.put("rows",interfaceTestList );
    return map;
}
}

.xml文件中

//resultMap
    <resultMap id="testCase" type="com.longteng.lesson2.my.mybatis.TestCase">
            <id column="test_case_id" property="testCaseId"></id>
            <result column="test_case_name" property="testCaseName"></result>
            <result column="interface_id" property="interfaceId"></result>
    </resultMap>

//查询语句
    <select id="getTestCase" resultMap="testCase">
            SELECT * FROM test_case
            <where> 1=1
                    <if test="null!=testCaseId">
                            AND test_case_id=#{testCaseId}
                    </if>
                    <if test="null!=testCaseName">
                            AND test_case_name=#{testCaseName}
                    </if>
                    <if test="null!=interface_id">
                            AND interface_id=#{interfaceId}
                    </if>
            </where>
    </select>

接口方法

    List<TestCase> getTestCase(TestCase testCase);

请求地址:返回第一页,返回10条数据,也就是10调数据是一页,第11条不展示
第11条可以用第二页请求,pageNumber=2就好

http://127.0.0.1:8080/MyBatisController/selectForPage?pageNumber=1&pageSize=10

在这里插入图片描述
数据库中有11条数据
在这里插入图片描述

因为线程安全的问题
切记:用PageHelper要注意
第二句查询方法一定要紧跟着PageHelper.startPage分页方法,不然会出问题,出现莫名其妙的分页

    Page page=PageHelper.startPage(pageNumber,pageSize,true);
    List<TestCase> interfaceTestList= myInterfaceTestMapper.getTestCase(null);
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值