mybatis-pageHelper做分頁,mybatis-pagehelper

來源:互聯網
上載者:User

mybatis-pageHelper做分頁,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

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.