mybatis分页的插件PageHelper

使用pageHelper进行分页

*maven项目在pom.xml中导入maven依赖

 <dependency>
    <groupId>com.github.pagehelper</groupId>
    <artifactId>pagehelper</artifactId>
    <version>最新版本</version>
</dependency>
  • 在 MyBatis 配置 xml 中配置拦截器插件
<plugins>
    <!-- com.github.pagehelper为PageHelper类所在包名 -->
    <plugin interceptor="com.github.pagehelper.PageInterceptor">
        <!-- 使用下面的方式配置参数,后面会有所有的参数介绍 -->
        <property name="param1" value="value1"/>
	</plugin>
</plugins>
  • 在spring中配置拦截器的插件
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
  <!-- 注意其他配置 -->
  <property name="plugins">
    <array>
      <bean class="com.github.pagehelper.PageInterceptor">
        <property name="properties">
          <!--使用下面的方式配置参数,一行配置一个 -->
          <value>
            params=value1
          </value>
        </property>
      </bean>
    </array>
  </property>
</bean>
  • Controller层的代码
 //获取数据库中的所有数据
    @RequestMapping(value = "/getList", method = RequestMethod.GET)
    public String getStudentList(ModelMap map, @RequestParam(defaultValue = "1", required = true, value = "pageNo") Integer pageNo) throws Exception {
        //设置每页的记录数
        Integer pageSize = 2;
        //分页查询
        PageHelper.startPage(pageNo, pageSize);
        //查询所有的学生信息
        List<Student> students = studentService.getList();
        PageInfo<Student> pageInfo = new PageInfo<Student>(students);
        map.addAttribute("pageInfo", pageInfo);
        return "/student/studentGetList";
    }
  • 对应的jsp页面
<center>
        <p>当前${pageInfo.pageNum}页,总${pageInfo.pages}页,总${pageInfo.total}条记录</p>
        <a href="${pageContext.request.contextPath}/getList?pageNo=${pageInfo.firstPage}">第一页</a>

        <c:if test="${pageInfo.hasPreviousPage}">
            <a href="${pageContext.request.contextPath}/getList?pageNo=${pageInfo.pageNum-1}">上一页</a>
        </c:if>

        <c:if test="${pageInfo.hasNextPage}">
            <a href="${pageContext.request.contextPath}/getList?pageNo=${pageInfo.pageNum+1}">下一页</a>
        </c:if>
        <a href="${pageContext.request.contextPath}/getList?pageNo=${pageInfo.lastPage}">最后页</a>
    </center>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值