zoukankan      html  css  js  c++  java
  • mybatis-pageHelper做分页

      Mybatis-PageHelpera是一个很好的第三方分页插件,支持很多数据库,几乎主流的数据库都支持

      github地址:https://github.com/pagehelper/Mybatis-PageHelper

      oschina地址:https://gitee.com/free/Mybatis_PageHelper

      下面我们几尝试的使用一下这个分页插件吧

    1. 引入第三方jar

    <dependency>
      <groupId>com.github.pagehelper</groupId>
      <artifactId>pagehelper</artifactId>
      <version>5.0.0</version>
    </dependency>
    1. 在application.xml 写拦截配置

    <bean id="demoSqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <!--dataSource属性指定要用到的连接池-->
        <property name="dataSource" ref="dataSource"/>
        <!--configLocation属性指定mybatis的核心配置文件-->
        <property name="configLocation" value="classpath:configuration.xml"/>
        <property name="mapperLocations" value="classpath:com/yihaomen/mybatis/model/*.xml"/>
        <property name="typeAliasesPackage" value="com.yihaomen.mybatis.model" />
        <property name="plugins">
            <array>
                <bean class="com.github.pagehelper.PageInterceptor">
                    <property name="properties">
                        <value>
                            helperDialect=mysql
                            reasonable=true
                            supportMethodsArguments=true
                            params=count=countSql
                            autoRuntimeDialect=true
                        </value>
                    </property>
                </bean>
            </array>
        </property>
    </bean>

      3.写controller层

    @RequestMapping("/pageList")
        public ModelAndView pageUserList(ModelAndView mv,
                 @RequestParam(required = true, defaultValue = "1") Integer pageNum,
                 @RequestParam(required = true, defaultValue = "3") Integer pageSize) {
    PageHelper.startPage(pageNum, pageSize);
    List<Article> list = articleService.getAllArticles();
    PageInfo<Article> pageInfo  = new PageInfo<Article>(list);
    mv.addObject("list", list);
    mv.addObject("page", pageInfo);
    mv.setViewName("list2");
    return mv;
    }

       4. 写页面list2.jsp

    <%@ page language="java" contentType="text/html; charset=UTF-8"
             pageEncoding="UTF-8"%>
    <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
    <html>
    <head>
      <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
      <title>article list</title>
    </head>
    <body>
      <c:forEach items="${list}" var="item">
        ${item.id }--${item.title }--${item.content }<br />
      </c:forEach>
      <br/>
      <a href="/user/pageList?pageNum=${page.prePage}">上一页</a>
     <a href="/user/pageList?pageNum=${page.nextPage}">下一页</a>
    </body>
    </html>

     https://gitee.com/huayicompany/springmvc-mybatis

  • 相关阅读:
    net core3.1 + electron 9.31.2 项目初始化
    DEV Express控件VScorllBar控件使用
    背包系统学习笔(tu)记(cao)
    C#应该掌握的一些东西
    服务器之间数据库拷贝
    photoshop AdobeCS6分享
    Random的实现原理
    C#利用System.Net发送邮件(带 抄送、密送、附件、html格式的邮件)
    jQuery validate验证控件remote缓存bug
    litrpa1.4版本正式发布了
  • 原文地址:https://www.cnblogs.com/happyflyingpig/p/7818497.html
Copyright © 2011-2022 走看看