我是靠谱客的博主 欣慰蛋挞,这篇文章主要介绍Mybatis使用PageHelper1.limit分页,现在分享给大家,希望可以做个参考。

1.limit分页

select 字段名 from 表名 limit startIndex , pageSize

  • 第一个数字: startIndex (起始下标。表示从数据库表中的第几条数据开始,数据库表中的第一条数据是从0开始)

  • 第二个数字:pageSize (每页显示的记录条数,就是每页显示多少条记录)

  • 页码 pageNum 是前端页面中的第几页

    • 假设已知页码 pageNum , 还有每页显示的记录条数 pageSize , 那么 startIndex 这个数字可以是自动获取的吗

    • startIndex = (pageNum - 1) * pageSize

1.1 mybatis不使用PageHelper做分页的查询

  • 数据库表如下

  • 数据库表对应的java类pojo

复制代码
1
2
3
4
5
6
7
public class Car implements Serializable{    private Long id;    private String carNum;    private String brand;    private Double guidePrice;    private String produceTime;    private String carType;
  • Mysql标准通用的分页SQL语句

    复制代码
    1
    2
    3
    4
    5
    6
    SELECT 字段名 FROM 表名 ...... LIMIT (pageNum - 1)*pageSize ,pageSize
  • 首先我在mybatis核心配置文件配置了

    复制代码
    1
    2
    3
    4
    <settings>     <!--开启驼峰命名自动映射,即从经典数据库列名 A_COLUMN 映射到经典 Java 属性名 aColumn。-->     <setting name="mapUnderscoreToCamelCase" value="true"/> </settings>
  • CarMapper接口

    复制代码
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    public interface CarMapper {    /**     * 通过分页查询的方式,获取Car列表     * @param startIndex 从数据库表中的第几条数据开始 ,计算公式 : (pageNum - 1) * pageSize     * @param pageSize 每页显示多少条数据     * @return     */    List<Car> selectAllByPage(            @Param("startIndex") Integer startIndex,            @Param("pageSize") Integer pageSize   ); }
  • CarMapper.xml

    复制代码
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    <mapper namespace="com.dongsheng.mybatis.mapper.CarMapper">    <!--        List<Car> selectAllByPage(            @Param("startIndex") Integer startIndex,            @Param("pageSize") Integer pageSize);    -->    <!-- 这里我不配置 resultMap ,因为我在mybatis核心配置文件中已经配置了开启驼峰命名映射 -->    <select id="selectAllByPage" resultType="com.dongsheng.mybatis.pojo.Car">       SELECT           id,car_num,brand,guide_price,produce_time,car_type       FROM           t_car       LIMIT           #{startIndex},#{pageSize}    </select> </mapper>
  • test测试类

    复制代码
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    @Test public void testSelectAllByPage(){    // 假设前端传过来的数据 pageNum=3 ,pageSize=3    Integer pageNum = 3;   //⻚码    Integer pageSize = 3;  // 每⻚显示记录条数    Integer startIndex = (pageNum-1)*pageSize;    SqlSession sqlSession = OpenSqlSessionUtil.openSession();    CarMapper mapper = sqlSession.getMapper(CarMapper.class);    List<Car> cars = mapper.selectAllByPage(startIndex, pageSize);    cars.forEach(car -> {        System.out.println(car);   });    sqlSession.close(); }

获取数据不难,难的是获取分⻚相关的数据⽐较难。可以借助mybatis的PageHelper插件。

1.2 mybatis使用PageHelper

  • 第⼀步:引⼊依赖

    复制代码
    1
    2
    3
    4
    5
    6
    <!-- pom.xml --> <dependency> <groupId>com.github.pagehelper</groupId> <artifactId>pagehelper</artifactId> <version>5.3.1</version> </dependency>
  • 第⼆步:在mybatis-config.xml⽂件中配置插件

    复制代码
    1
    2
    3
    <plugins> <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin> </plugins>
  • 第三步:编写Java代码

    CarMapper接口

    复制代码
    1
    2
    3
    public interface CarMapper {    List<Car> selectAll(); }

    CarMapper.xml

    复制代码
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    <mapper namespace="com.dongsheng.mybatis.mapper.CarMapper">    <!--        List<Car> selectAll()    -->    <select id="selectAll" resultType="com.dongsheng.mybatis.pojo.Car">       SELECT           id,car_num,brand,guide_price,produce_time,car_type       FROM           t_car    </select> </mapper>

    test测试类

    复制代码
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    @Test    public void testSelectAllByPage(){        SqlSession sqlSession = OpenSqlSessionUtil.openSession();        CarMapper mapper = sqlSession.getMapper(CarMapper.class); ​        // 关键点 :在查询语句之前开启分⻚功能。        PageHelper.startPage(3,3);        // 执行查询语句        List<Car> cars = mapper.selectAll();        // 获取分页的信息对象        PageInfo<Car> pageInfo = new PageInfo<>(cars, 3); ​        System.out.println(pageInfo);   }

    关键点:

    • 在查询语句之前开启分⻚功能。

    • 在查询语句之后封装PageInfo对象。(PageInfo对象将来会存储到request域当中。在⻚⾯上展 示。)

  • 执⾏结果:

    复制代码
    1
    2
    3
    4
    5
    6
    7
    8
    9
    PageInfo{ pageNum=3, pageSize=3, size=3, startRow=7, endRow=9, total=12, pages=4,       list=Page{count=true, pageNum=3, pageSize=3, startRow=6, endRow=9, total=12, pages=4, reasonable=false, pageSizeZero=false} [Car{id=172, carNum='6666', brand='丰田霸道', guidePrice=32.0, produceTime='2020-10-11', carType='燃油车'}, Car{id=173, carNum='1008', brand='本田超雄', guidePrice=30.0, produceTime='2000-10-19', carType='燃油车'}, Car{id=174, carNum='1008', brand='比亚迪燃烧', guidePrice=30.0, produceTime='2000-10-11', carType='电池车'}], prePage=2, nextPage=4, isFirstPage=false, isLastPage=false, hasPreviousPage=true, hasNextPage=true, navigatePages=3, navigateFirstPage=2, navigateLastPage=4,navigatepageNums=[2, 3, 4]}

最后

以上就是欣慰蛋挞最近收集整理的关于Mybatis使用PageHelper1.limit分页的全部内容,更多相关Mybatis使用PageHelper1内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(45)

评论列表共有 0 条评论

立即
投稿
返回
顶部