關於Spring+mybatis+PageHelper分頁插件PageHelper的使用策略

<div id="article_content" class="article_content csdn-tracking-statistics tracking-click" data-mod="popu_519" data-dsm="post" style="overflow: hidden;"> <div class="markdown_views"> <ol> <li>把插件jar包導入項目(具體上篇有介紹<a href="http://blog.csdn.net/qq_33624284/article/details/72821811" target="_blank">http://blog.csdn.net/qq_33624284/article/details/72821811</a>)</li> <li>spring-mybatis.xml文件中配置</li> </ol>css

<pre class="prettyprint"><code class="hljs xml has-numbering"><span class="hljs-comment">&lt;!-- spring和MyBatis完美整合,不須要mybatis的配置映射文件 --&gt;</span></br> <span class="hljs-tag">&lt;<span class="hljs-title">bean</span> <span class="hljs-attribute">id</span>=<span class="hljs-value">"sqlSessionFactory"</span> <span class="hljs-attribute">class</span>=<span class="hljs-value">"org.mybatis.spring.SqlSessionFactoryBean"</span>&gt;</span></br> <span class="hljs-tag">&lt;<span class="hljs-title">property</span> <span class="hljs-attribute">name</span>=<span class="hljs-value">"dataSource"</span> <span class="hljs-attribute">ref</span>=<span class="hljs-value">"dataSource"</span> /&gt;</span></br> <span class="hljs-comment">&lt;!-- 自動掃描mapping.xml文件 --&gt;</span></br> <span class="hljs-tag">&lt;<span class="hljs-title">property</span> <span class="hljs-attribute">name</span>=<span class="hljs-value">"mapperLocations"</span> <span class="hljs-attribute">value</span>=<span class="hljs-value">"classpath:com/wl/goldatg/mapping/*.xml"</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-title">property</span>&gt;</span></br> <span class="hljs-comment">&lt;!-- 配置分頁插件 --&gt;</span></br> <span class="hljs-tag">&lt;<span class="hljs-title">property</span> <span class="hljs-attribute">name</span>=<span class="hljs-value">"plugins"</span>&gt;</span></br> <span class="hljs-tag">&lt;<span class="hljs-title">array</span>&gt;</span></br> <span class="hljs-tag">&lt;<span class="hljs-title">bean</span> <span class="hljs-attribute">class</span>=<span class="hljs-value">"com.github.pagehelper.PageHelper"</span>&gt;</span></br> <span class="hljs-tag">&lt;<span class="hljs-title">property</span> <span class="hljs-attribute">name</span>=<span class="hljs-value">"properties"</span>&gt;</span></br> <span class="hljs-tag">&lt;<span class="hljs-title">value</span>&gt;</span></br> dialect=mysql</br> reasonable=true</br> <span class="hljs-tag">&lt;/<span class="hljs-title">value</span>&gt;</span></br> <span class="hljs-tag">&lt;/<span class="hljs-title">property</span>&gt;</span></br> <span class="hljs-tag">&lt;/<span class="hljs-title">bean</span>&gt;</span></br> <span class="hljs-tag">&lt;/<span class="hljs-title">array</span>&gt;</span></br> <span class="hljs-tag">&lt;/<span class="hljs-title">property</span>&gt;</span></br> <span class="hljs-tag">&lt;/<span class="hljs-title">bean</span>&gt;</span></code></pre>java

<p>## 下面咱們就能夠使用 ##</p>mysql

<ul> <li>新建sql語句,返回list結果集</li> </ul>git

<pre class="prettyprint"><code class="hljs cs has-numbering">&lt;<span class="hljs-keyword">select</span> id=<span class="hljs-string">"selectByList"</span> resultMap=<span class="hljs-string">"BaseResultMap"</span>&gt;</br> <span class="hljs-keyword">select</span> </br> &lt;include refid=<span class="hljs-string">"Base_Column_List"</span> /&gt;</br> <span class="hljs-keyword">from</span> user_t</br> &lt;/<span class="hljs-keyword">select</span>&gt;</code></pre>github

<ul> <li>service層</li> </ul>spring

<pre class="prettyprint"><code class="hljs cs has-numbering"><span class="hljs-keyword">public</span> List&lt;User&gt; <span class="hljs-title">selectByList</span>() {</br> <span class="hljs-keyword">return</span> <span class="hljs-keyword">this</span>.userDao.selectByList();</br> }</code></pre>sql

<ul> <li>Controller類</li> </ul>數據庫

<pre class="prettyprint"><code class="hljs java has-numbering"><span class="hljs-annotation">@RequestMapping</span>(<span class="hljs-string">"/userList"</span>) <span class="hljs-keyword">public</span> String <span class="hljs-title">userList</span>(@<span class="hljs-title">RequestParam</span>(required=<span class="hljs-keyword">true</span>,defaultValue=<span class="hljs-string">"1"</span>) Integer page,HttpServletRequest request,Model model){</br> <span class="hljs-comment">//PageHelper.startPage(page, pageSize);這段代碼表示,程序開始分頁了,page默認值是1,pageSize默認是10,意思是從第1頁開始,每頁顯示10條記錄。</span></br> PageHelper.startPage(page, <span class="hljs-number">3</span>);</br> List&lt;User&gt; userList = userService.selectByList();</br> PageInfo&lt;User&gt; p=<span class="hljs-keyword">new</span> PageInfo&lt;User&gt;(userList);</br> model.addAttribute(<span class="hljs-string">"page"</span>, p);</br> model.addAttribute(<span class="hljs-string">"userList"</span>,userList);</br> <span class="hljs-keyword">return</span> <span class="hljs-string">"showUser"</span>;</br> }</code></pre>markdown

<p>PageInfo這個類是插件裏的類,很是方便的調用,分頁再次提升性能:</p>mybatis

<pre class="prettyprint"><code class="hljs java has-numbering"> <span class="hljs-comment">//當前頁</span></br> <span class="hljs-keyword">private</span> <span class="hljs-keyword">int</span> pageNum;</br> <span class="hljs-comment">//每頁的數量</span></br> <span class="hljs-keyword">private</span> <span class="hljs-keyword">int</span> pageSize;</br> <span class="hljs-comment">//當前頁的數量</span></br> <span class="hljs-keyword">private</span> <span class="hljs-keyword">int</span> size;</br> <span class="hljs-comment">//排序</span></br> <span class="hljs-keyword">private</span> String orderBy;</br></br> <span class="hljs-comment">//因爲startRow和endRow不經常使用,這裏說個具體的用法</span></br> <span class="hljs-comment">//能夠在頁面中"顯示startRow到endRow 共size條數據"</span></br></br> <span class="hljs-comment">//當前頁面第一個元素在數據庫中的行號</span></br> <span class="hljs-keyword">private</span> <span class="hljs-keyword">int</span> startRow;</br> <span class="hljs-comment">//當前頁面最後一個元素在數據庫中的行號</span></br> <span class="hljs-keyword">private</span> <span class="hljs-keyword">int</span> endRow;</br> <span class="hljs-comment">//總記錄數</span></br> <span class="hljs-keyword">private</span> <span class="hljs-keyword">long</span> total;</br> <span class="hljs-comment">//總頁數</span></br> <span class="hljs-keyword">private</span> <span class="hljs-keyword">int</span> pages;</br> <span class="hljs-comment">//結果集</span></br> <span class="hljs-keyword">private</span> List&lt;T&gt; list;</br></br> <span class="hljs-comment">//第一頁</span></br> <span class="hljs-keyword">private</span> <span class="hljs-keyword">int</span> firstPage;</br> <span class="hljs-comment">//前一頁</span></br> <span class="hljs-keyword">private</span> <span class="hljs-keyword">int</span> prePage;</br> <span class="hljs-comment">//下一頁</span></br> <span class="hljs-keyword">private</span> <span class="hljs-keyword">int</span> nextPage;</br> <span class="hljs-comment">//最後一頁</span></br> <span class="hljs-keyword">private</span> <span class="hljs-keyword">int</span> lastPage;</br></br> <span class="hljs-comment">//是否爲第一頁</span></br> <span class="hljs-keyword">private</span> <span class="hljs-keyword">boolean</span> isFirstPage = <span class="hljs-keyword">false</span>;</br> <span class="hljs-comment">//是否爲最後一頁</span></br> <span class="hljs-keyword">private</span> <span class="hljs-keyword">boolean</span> isLastPage = <span class="hljs-keyword">false</span>;</br> <span class="hljs-comment">//是否有前一頁</span></br> <span class="hljs-keyword">private</span> <span class="hljs-keyword">boolean</span> hasPreviousPage = <span class="hljs-keyword">false</span>;</br> <span class="hljs-comment">//是否有下一頁</span></br> <span class="hljs-keyword">private</span> <span class="hljs-keyword">boolean</span> hasNextPage = <span class="hljs-keyword">false</span>;</br> <span class="hljs-comment">//導航頁碼數</span></br> <span class="hljs-keyword">private</span> <span class="hljs-keyword">int</span> navigatePages;</br> <span class="hljs-comment">//全部導航頁號</span></br> <span class="hljs-keyword">private</span> <span class="hljs-keyword">int</span>[] navigatepageNums;</code></pre>

<p>使用PageInfo這個類,你須要將查詢出來的list放進去:</p>

<pre class="prettyprint"><code class="hljs xml has-numbering">List<span class="hljs-tag">&lt;<span class="hljs-title">User</span>&gt;</span> userList = userService.selectByList();</br> PageInfo<span class="hljs-tag">&lt;<span class="hljs-title">User</span>&gt;</span> p=new PageInfo<span class="hljs-tag">&lt;<span class="hljs-title">User</span>&gt;</span>(userList);</code></pre>

<p>頁面使用很是方便,直接貼代碼:</p>

<ol> <li>控制層</li> </ol>

<pre class="prettyprint"><code class="hljs avrasm has-numbering">model<span class="hljs-preprocessor">.addAttribute</span>(<span class="hljs-string">"page"</span>, p)<span class="hljs-comment">;</span></code></pre>

<ol> <li>前臺頁面</li> </ol>

<pre class="prettyprint"><code class="hljs xml has-numbering"><span class="hljs-tag">&lt;<span class="hljs-title">body</span>&gt;</span></br> <span class="hljs-tag">&lt;<span class="hljs-title">center</span>&gt;</span></br> <span class="hljs-tag">&lt;<span class="hljs-title">table</span> <span class="hljs-attribute">width</span>=<span class="hljs-value">"200"</span> <span class="hljs-attribute">border</span>=<span class="hljs-value">"1"</span>&gt;</span></br> <span class="hljs-tag">&lt;<span class="hljs-title">tr</span>&gt;</span></br> <span class="hljs-tag">&lt;<span class="hljs-title">th</span> <span class="hljs-attribute">scope</span>=<span class="hljs-value">"col"</span>&gt;</span>序號<span class="hljs-tag">&lt;/<span class="hljs-title">th</span>&gt;</span></br> <span class="hljs-tag">&lt;<span class="hljs-title">th</span> <span class="hljs-attribute">scope</span>=<span class="hljs-value">"col"</span>&gt;</span>ID<span class="hljs-tag">&lt;/<span class="hljs-title">th</span>&gt;</span></br> <span class="hljs-tag">&lt;<span class="hljs-title">th</span> <span class="hljs-attribute">scope</span>=<span class="hljs-value">"col"</span>&gt;</span>姓名<span class="hljs-tag">&lt;/<span class="hljs-title">th</span>&gt;</span></br> <span class="hljs-tag">&lt;<span class="hljs-title">th</span> <span class="hljs-attribute">scope</span>=<span class="hljs-value">"col"</span>&gt;</span>密碼<span class="hljs-tag">&lt;/<span class="hljs-title">th</span>&gt;</span></br> <span class="hljs-tag">&lt;<span class="hljs-title">th</span> <span class="hljs-attribute">scope</span>=<span class="hljs-value">"col"</span>&gt;</span>年齡<span class="hljs-tag">&lt;/<span class="hljs-title">th</span>&gt;</span></br> <span class="hljs-tag">&lt;/<span class="hljs-title">tr</span>&gt;</span></br> <span class="hljs-tag">&lt;<span class="hljs-title">c:forEach</span> <span class="hljs-attribute">begin</span>=<span class="hljs-value">"0"</span> <span class="hljs-attribute">step</span>=<span class="hljs-value">"1"</span> <span class="hljs-attribute">items</span>=<span class="hljs-value">"${userList}"</span> <span class="hljs-attribute">var</span>=<span class="hljs-value">"list"</span> <span class="hljs-attribute">varStatus</span>=<span class="hljs-value">"userlist"</span>&gt;</span></br> <span class="hljs-tag">&lt;<span class="hljs-title">tr</span>&gt;</span></br> <span class="hljs-tag">&lt;<span class="hljs-title">td</span>&gt;</span>${userlist.count}<span class="hljs-tag">&lt;/<span class="hljs-title">td</span>&gt;</span></br> <span class="hljs-tag">&lt;<span class="hljs-title">td</span>&gt;</span>${list.id}<span class="hljs-tag">&lt;/<span class="hljs-title">td</span>&gt;</span></br> <span class="hljs-tag">&lt;<span class="hljs-title">td</span>&gt;</span>${list.userName}<span class="hljs-tag">&lt;/<span class="hljs-title">td</span>&gt;</span></br> <span class="hljs-tag">&lt;<span class="hljs-title">td</span>&gt;</span>${list.password}<span class="hljs-tag">&lt;/<span class="hljs-title">td</span>&gt;</span></br> <span class="hljs-tag">&lt;<span class="hljs-title">td</span>&gt;</span>${list.age}<span class="hljs-tag">&lt;/<span class="hljs-title">td</span>&gt;</span></br> <span class="hljs-tag">&lt;/<span class="hljs-title">tr</span>&gt;</span></br> <span class="hljs-tag">&lt;/<span class="hljs-title">c:forEach</span>&gt;</span></br> <span class="hljs-tag">&lt;/<span class="hljs-title">table</span>&gt;</span></br> <span class="hljs-tag">&lt;<span class="hljs-title">p</span>&gt;</span>一共${page.pages}頁<span class="hljs-tag">&lt;/<span class="hljs-title">p</span>&gt;</span></br> <span class="hljs-tag">&lt;<span class="hljs-title">a</span> <span class="hljs-attribute">href</span>=<span class="hljs-value">"userList?page=${page.firstPage}"</span>&gt;</span>第一頁<span class="hljs-tag">&lt;/<span class="hljs-title">a</span>&gt;</span></br> <span class="hljs-tag">&lt;<span class="hljs-title">a</span> <span class="hljs-attribute">href</span>=<span class="hljs-value">"userList?page=${page.nextPage}"</span>&gt;</span>下一頁<span class="hljs-tag">&lt;/<span class="hljs-title">a</span>&gt;</span></br> <span class="hljs-tag">&lt;<span class="hljs-title">a</span> <span class="hljs-attribute">href</span>=<span class="hljs-value">"userList?page=${page.prePage}"</span>&gt;</span>上一頁<span class="hljs-tag">&lt;/<span class="hljs-title">a</span>&gt;</span></br> <span class="hljs-tag">&lt;<span class="hljs-title">a</span> <span class="hljs-attribute">href</span>=<span class="hljs-value">"userList?page=${page.lastPage}"</span>&gt;</span>最後頁<span class="hljs-tag">&lt;/<span class="hljs-title">a</span>&gt;</span></br> <span class="hljs-tag">&lt;/<span class="hljs-title">center</span>&gt;</span></br> <span class="hljs-tag">&lt;/<span class="hljs-title">body</span>&gt;</span></code></pre>

<p>最後結果顯示:(關鍵點${page.prePage}簡單又快捷) <br> <img src="https://img-blog.csdn.net/20170601103012274?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvcXFfMzM2MjQyODQ=/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast" alt="這裏寫圖片描述" title=""></p> </div> <link rel="stylesheet" href="https://csdnimg.cn/release/phoenix/production/markdown_views-68a8aad09e.css"> </div> 前臺須要傳入的參數是當前頁和頁面顯示數目,固然頁面顯示數目也能夠後臺規定,通常在接收參數時最好加上默認配置以下: ```java @RequestParam(defaultValue="1",value="pageNum")String pageNum, @RequestParam(defaultValue="10",value="pageSize")String pageSize ```

相關文章
相關標籤/搜索