springMvc+Mybatis下使用pagehelper 插件

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

 <dependency>
        <groupId>com.github.pagehelpergroupId>
        <artifactId>pagehelperartifactId>
        <version>3.7.5version>
    dependency>

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

applicationContext.xml配置:

<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="dataSource" />
        
        <property name="mapperLocations" value="classpath:com/web/mapper/*.xml">property>
        
        <property name="configLocation" value="classpath:mybatis-config.xml">property>

    bean>

mybatis-config.xml配置:


    <plugins>
    
    <plugin interceptor="com.github.pagehelper.PageHelper">
          
        <property name="dialect" value="mysql"/>
        
        
        
        <property name="offsetAsPageNum" value="true"/>
        
        
        <property name="rowBoundsWithCount" value="true"/>
        
        
        <property name="pageSizeZero" value="true"/>
        
        
        
        <property name="reasonable" value="true"/>
        
        
        
        
        <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 map){
          PageHelper.startPage(page, 3);
          List userList = userService.findByPage("");
          PageInfo p=new PageInfo(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"%>

<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 heretitle>
head>
<body>
  <center>
        <table width="200" border="1">
          <tr>
            <th scope="col">IDth>
             <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写法都一样的。

效果如下:
springMvc+Mybatis下使用pagehelper 插件_第1张图片

你可能感兴趣的:(pagehelper)