【SSM专栏二】PageHelper的使用

本文详细介绍如何在MyBatis项目中使用PageHelper分页插件,包括pom依赖导入、PageHelper拦截器配置、SqlSessionFactoryBean属性注入、controller与service层分页实现,以及JSP页面展示分页数据。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

目录

一、导入pom

二、配置PageHelper拦截器

三、在SqlSessionFactoryBean中注入属性

四、controller层:

五、service层:

 

一、导入pom

<!-- mybatis分页插件依赖 -->
    <dependency>
      <groupId>com.github.pagehelper</groupId>
      <artifactId>pagehelper</artifactId>
      <version>4.0.0</version>
    </dependency>

二、配置PageHelper拦截器

<!-- 配置pageHelp的拦截器 -->
    <bean id="plugin" class="com.github.pagehelper.PageHelper">
        <property name="properties">
            <value>helperDialect=mysql</value>
        </property>
    </bean>

三、在SqlSessionFactoryBean中注入属性

    <!-- spring和MyBatis完美整合,不需要mybatis的配置映射文件 -->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="dataSource" />
        <!-- 自动扫描mapping.xml文件 -->
        <property name="mapperLocations" value="classpath:mapper/*.xml"></property>
        <!-- 配置分页插件 -->
        <property name="plugins">
            <list>
                <ref bean="plugin"></ref>
            </list>
        </property>
    </bean>

四、controller层:

@RestController
@RequestMapping("student")
public class StudentController {

    @Autowired
    StudentService services;

    @RequestMapping("queryByPage")
    public ModelAndView queryByPage1(@RequestParam(defaultValue = "1") Integer page , @RequestParam(defaultValue="2") Integer pageSize){
        PageHelper.startPage(page,pageSize);//开始分页
        List<Student> list = services.getStudentAll();
        PageInfo<Student> pageInfo = new PageInfo<Student>(list);//封装分页数据

        ModelAndView mav = new ModelAndView();
        mav.setViewName("pages/students");
        mav.addObject("pageInfo", pageInfo);

        return mav;
    }

    @RequestMapping("queryByPage2")
    public PageInfo<Student> queryByPage2(@RequestParam(defaultValue = "1") Integer page , @RequestParam(defaultValue="10") Integer pageSize){
        PageHelper.startPage(page,pageSize);//开始分页
        List<Student> list = services.getStudentAll();
        PageInfo<Student> pageInfo = new PageInfo<Student>(list);//封装分页数据
        return pageInfo;
    }
}

五、service层:

@Service
public class StudentServiceImpl implements StudentService {

    @Autowired
    private StudentMapper studentMapper;

    @Override
    public List<Student> getStudentAll() {
        return studentMapper.selectByExample(new StudentExample());
    }

    /**
     * 静态方法startPage
     */
    @Override
    public List<Student> getStudentListByPage(StudentExample StudentExample, Integer pageNum, Integer pageSize) {
        //  在你需要进行分页的 MyBatis 查询方法前调用 PageHelper.startPage 静态方法即可,紧跟在这个方法后的第一个MyBatis 查询方法会被进行分页。
        //  只要你可以保证在 PageHelper 方法调用后紧跟 MyBatis 查询方法,这就是安全的
        PageHelper.startPage(pageNum, pageSize);
        return studentMapper.selectByExample(StudentExample);
    }

    /**
     * 分页时,实际返回的结果list类型是Page<E>,如果想取出分页信息,需要强制转换为Page<E>
     * 因为  public class Page<E> extends ArrayList<E> implements Closeable
     */
    @Override
    public Page<Student> getStudentListByPage1(StudentExample StudentExample, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<Student> list = studentMapper.selectByExample(StudentExample);
        if (null != list) {
            Page<Student> page = (Page<Student>) list;
            System.out.println(page);
            return page;
        }
        return null;
    }
}

六、jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
<p>当前${pageInfo.pageNum}页,共${pageInfo.pages}页,总共${pageInfo.total}条记录</p>
<a href="queryByPage?page=${pageInfo.firstPage}">第一页</a>
<c:if test="${pageInfo.hasPreviousPage}">
    <a href="queryByPage?page=${pageInfo.pageNum-1}">上一页</a>
</c:if>
<c:if test="${pageInfo.hasNextPage}">
    <a href="queryByPage?page=${pageInfo.pageNum+1}">下一页</a>
</c:if>
<a href="queryByPage?page=${pageInfo.lastPage}">最后一页</a>

</body>
</html>

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值