springMvc+Mybatis下使用pagehelper 插件

废话不多说,直接说流程,我的项目是Maven项目。先进行配置
1、pom.xml配置,添加jar

 <dependency>
        <groupId>com.github.pagehelper</groupId>
        <artifactId>pagehelper</artifactId>
        <version>3.7.5</version>
    </dependency>

2、applicationContext.xml和mybatis-config.xml的配置

applicationContext.xml配置:

<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="dataSource" />
        <!-- 自动扫描mappers.xml文件 -->
        <property name="mapperLocations" value="classpath:com/web/mapper/*.xml"></property>
        <!-- mybatis配置文件 -->
        <property name="configLocation" value="classpath:mybatis-config.xml"></property>

    </bean>

mybatis-config.xml配置:

<!-- Mybatis分页配置 -->
    <plugins>
    <!-- com.github.pagehelper为PageHelper类所在包名 -->
    <plugin interceptor="com.github.pagehelper.PageHelper">
         <!-- 设置数据库类型 Oracle,Mysql,MariaDB,SQLite,Hsqldb,PostgreSQL六种数据库--> 
        <property name="dialect" value="mysql"/>
        <!-- 该参数默认为false -->
        <!-- 设置为true时,会将RowBounds第一个参数offset当成pageNum页码使用 -->
        <!-- 和startPage中的pageNum效果一样-->
        <property name="offsetAsPageNum" value="true"/>
        <!-- 该参数默认为false -->
        <!-- 设置为true时,使用RowBounds分页会进行count查询 -->
        <property name="rowBoundsWithCount" value="true"/>
        <!-- 设置为true时,如果pageSize=0或者RowBounds.limit = 0就会查询出全部的结果 -->
        <!-- (相当于没有执行分页查询,但是返回结果仍然是Page类型)-->
        <property name="pageSizeZero" value="true"/>
        <!-- 3.3.0版本可用 - 分页参数合理化,默认false禁用 -->
        <!-- 启用合理化时,如果pageNum<1会查询第一页,如果pageNum>pages会查询最后一页 -->
        <!-- 禁用合理化时,如果pageNum<1或pageNum>pages会返回空数据 -->
        <property name="reasonable" value="true"/>
        <!-- 3.5.0版本可用 - 为了支持startPage(Object params)方法 -->
        <!-- 增加了一个`params`参数来配置参数映射,用于从Map或ServletRequest中取值 -->
        <!-- 可以配置pageNum,pageSize,count,pageSizeZero,reasonable,不配置映射的用默认值 -->
        <!-- 不理解该含义的前提下,不要随便复制该配置 -->
        <property name="params" value="pageNum=start;pageSize=limit;"/>
    </plugin>
</plugins>

3.UserController的写法:

    /**
     * 分页查询方法
     * 
     * @author wangguan,
     * @date 2017年11月3日 下午2:55:02,
     * @version argType
     */
    @RequestMapping("/findByPage")
    public String findByPage(@RequestParam(required=true,defaultValue="1")Integer page,User user,HttpServletRequest request,Model model,Map<String,Object> map){
          PageHelper.startPage(page, 3);
          List<User> userList = userService.findByPage("");
          PageInfo<User> p=new PageInfo<User>(userList);
          model.addAttribute("page", p);
          model.addAttribute("userList",userList);
          return "showUser";

    }

4、showUser.jsp的写法:

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
  <%@ include file="/common/jsp/taglibs.jsp"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<script type="text/javascript" src="common/js/jquery.min.js"></script>
<title>Insert title here</title>
</head>
<body>
  <center>
        <table width="200" border="1">
          <tr>
            <th scope="col">ID</th>
             <th scope="col">姓名</th>
            <th scope="col">密码</th> 
          </tr>
           <c:forEach  items="${userList}" var="list" >
          <tr>
            <td>${list.id}</td>
            <td>${list.userName}</td>
            <td>${list.password}</td>
          </tr>
          </c:forEach>
        </table>
        <p>一共${page.pages}页</p>
        <a href="findByPage?page=${page.firstPage}">第一页</a>
        <a href="findByPage?page=${page.nextPage}">下一页</a>
        <a href="findByPage?page=${page.prePage}">上一页</a>
        <a href="findByPage?page=${page.lastPage}">最后页</a>
    </center>
  </body>
  </html>

好了,service,serviceImpl,和mapper的都跟一般的Mybatis写法都一样的。

效果如下:
这里写图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值