栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

springboot集合mybatis使用pagehelper,三步学会使用pagehelper

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

springboot集合mybatis使用pagehelper,三步学会使用pagehelper

第一步:导入分页插件的依赖

 
            com.github.pagehelper
            pagehelper
            5.1.10
  

第二步:注册sqlsessionfactory添加分页插件

package com.example.config;

import com.github.pagehelper.PageInterceptor;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.sql.DataSource;

@Configuration
public class MybatisConfig {
    
    @Bean
    public SqlSessionFactory getSqlSessionFactory(DataSource ds) throws Exception {
        SqlSessionFactoryBean factory = new SqlSessionFactoryBean();
        //设置数据源
        factory.setDataSource(ds);
        //把分页插件设置到SqlSessionFactory插件库
        factory.setPlugins(new PageInterceptor());
        //返回具体实例对象SqlSessionFactory
        // SqlSessionFactoryBean,Bean在后的特殊Bean需要调用getObject()返回Bean
        SqlSessionFactory object = factory.getObject();
        return object;

    }
}

第三步:使用分页

 //分页使用
    @GetMapping("/test1")
    public Map test1() {
        //启用分页查询,查询第一页每页5条
        PageHelper.startPage(1, 5);
        List orderMasters = orderMaterMapper.queryOrderMaster();
        Page page = (Page) orderMasters;
        int pages = page.getPages();//总页数
        long total = page.getTotal();//总的条数
        int pageSize = page.getPageSize();//每页条数
        int pageNum = page.getPageNum();//当前页码
        //封装成map为了以后向前端传参
        Map map = new HashMap<>();
        map.put("pages", pages);
        map.put("total", total);
        map.put("total", orderMasters);
        return map;
    }
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/992185.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号