mybatis使用PageInterceptor插件进行查询分页

时间:2020-06-18 11:46:23 类型:JAVA
字号:    

mybatis使用PageInterceptor插件进行查询分页方法:

   一. 下载pagehelper-5.1.11.jarJSQLParser.jar

        pagehelper jar依赖前面这个jar所以两个需要同时引入,才可使用pagehelper

  二.   mybatis-config.xml配置

<!-- 配置分页插件 -->
    <plugins>
        <plugin interceptor="com.github.pagehelper.PageInterceptor">
            <!-- 设置数据库类型 Oracle,Mysql,MariaDB,SQLite,Hsqldb,PostgreSQL六种数据库-->
            <property name="helperDialect" value="mysql"/>
        </plugin>
    </plugins>

  三. 调用

@RequestMapping(value = "/studentList",method = RequestMethod.GET)
public String studentList(Student student, Model model,
                          @RequestParam(defaultValue = "1") Integer page,
                            HttpServletRequest request){
    PageHelper.startPage(page,2);//开始分页
    List<Student> list = studentService.studentAll(student);
    PageInfo<Student> pageInfo = new PageInfo<Student>(list);//封装分页数据
    model.addAttribute("studentList",list);
    //model.addAttribute("pageInfo",pageInfo);
    int total = (int) pageInfo.getTotal(); //页码总数
    int size  = 2;
    int step  = 9;
    String pages = Pager.getPages(total, size, step, page, request, "page");
    model.addAttribute("pages",pages);
    return "admin/student/index";
}

四. 视图页面

<table class="listTable">
    <tr>
        <td>
            <form action="?">
                <input type="text" name="names">
                <select name="sex">
                    <option value="男">男</option>
                    <option value="女">女</option>
                </select>
                <input type="submit" value="查询">
            </form>
        </td>
    </tr>
</table>
<table class="listTable">
    <tr>
        <td>选择</td>
        <td>ID</td>
        <td>姓名</td>
        <td>性别</td>
        <td>爱好</td>
        <td>时间</td>
        <td>图片</td>
        <td>操作</td>
    </tr>
    <c:forEach items="${studentList }" var="list">
        <tr>
            <td>
                <input type="checkbox" name="ids" value="${list.id}">
            </td>
            <td>${list.id}</td>
            <td>
                    ${list.names}
            </td>
            <td>
                    ${list.sex}
            </td>
            <td>
                    ${list.hobby}
            </td>
            <td>
                <jsp:useBean id="dateValue" class="java.util.Date"/>
                <jsp:setProperty name="dateValue" property="time" value="${list.time}"/>
                <fmt:formatDate value="${dateValue}" pattern="yyyy-MM-dd HH:mm"/>

            </td>
            <td>
                <c:if test="${list.pic != ''}">
                    <img src="/up/${list.pic}" style="max-width: 120px;">
                </c:if>
            </td>
            <td>
                <a href="">删除</a>
                <a href="">修改</a>
             </td>
        </tr>
    </c:forEach>
    <tr>
        <td colspan="8">
            ${pages}
        </td>
    </tr>
</table>

效果如下:

1.jpg

Paper.getPasge见http://www.ncyteng.com/news/show/745.html

<