企業(yè)網(wǎng)站管理seo1視頻發(fā)布會
一,分頁的概念
分頁是一種將大量數(shù)據(jù)或內(nèi)容分割成多個頁面以便逐頁顯示的方式。在分頁中,數(shù)據(jù)被分割成一定數(shù)量的頁,每頁顯示一部分?jǐn)?shù)據(jù)或內(nèi)容,用戶可以通過翻頁或跳分頁是一種將大量數(shù)據(jù)或內(nèi)容分割成多個頁面以便逐頁顯示的方式。在分頁中,數(shù)據(jù)被分割成一定數(shù)量的頁,每頁顯示一部分?jǐn)?shù)據(jù)或內(nèi)容,用戶可以通過翻頁或跳轉(zhuǎn)頁面來瀏覽更多內(nèi)容。轉(zhuǎn)頁面來瀏覽更多內(nèi)容
1.1 分頁應(yīng)用場景
數(shù)據(jù)展示:
? ? ?當(dāng)需要展示大量數(shù)據(jù),并將其劃分為多個頁面進(jìn)行逐頁查看時,可以使用分頁應(yīng)用。例如,在電子商務(wù)網(wǎng)站上展示商品列表或搜索結(jié)果時,分頁應(yīng)用可以提供良好的用戶體驗
數(shù)據(jù)瀏覽:
? ? ?當(dāng)需要在一個長列表或長文檔中進(jìn)行瀏覽時,分頁應(yīng)用可以將內(nèi)容分割成多個頁面,方便用戶逐頁閱讀或瀏覽。例如,在新聞網(wǎng)站上瀏覽新聞文章或在書籍電子閱讀器中閱讀電子書時,分頁應(yīng)用可以讓用戶更加方便地跳轉(zhuǎn)頁面
搜索結(jié)果分頁:
? ? ?當(dāng)進(jìn)行搜索操作時,檢索出來的結(jié)果可能非常龐大,分頁應(yīng)用可以將搜索結(jié)果分頁展示,以便用戶逐頁查看結(jié)果。這在搜索引擎、論壇或社交媒體平臺等場景下非常常見
1.2 分頁的優(yōu)點
提高性能:
? ? 分頁應(yīng)用可以將大量數(shù)據(jù)分割成多個頁面,只加載當(dāng)前頁面所需的數(shù)據(jù),從而減輕數(shù)據(jù)庫和服務(wù)器的負(fù)載,提高查詢和渲染速度。
改善用戶體驗:
? ?通過分頁,用戶可以更方便地瀏覽和導(dǎo)航大量數(shù)據(jù),減少頁面滾動和加載時間,提高交互效率和用戶滿意度。
靈活導(dǎo)航:
? ? 分頁應(yīng)用可以為用戶提供靈活的導(dǎo)航選項,允許用戶跳轉(zhuǎn)到特定頁面,或者調(diào)整每頁顯示的數(shù)據(jù)量,以滿足個性化需求。
數(shù)據(jù)安全性:
? ? 通過限制每頁數(shù)據(jù)的數(shù)量,分頁應(yīng)用可以限制對數(shù)據(jù)的訪問,提高數(shù)據(jù)的安全性和隱私保護(hù)。
二,分頁功能的實現(xiàn)
本文章根據(jù)上篇文章內(nèi)容進(jìn)行擴(kuò)展實現(xiàn)分頁功能,上一篇連接地址:簡單的模糊查詢
為了讓分頁更加提高效率我們用的是一個PageHelper插件,讓插件來來幫助我們實現(xiàn)這個功能
pom.xml導(dǎo)入PageHelper插件
<!-- ********************** 分頁 Pom依賴 ********************** --><dependency><groupId>com.github.pagehelper</groupId><artifactId>pagehelper</artifactId><version>5.1.2</version></dependency>
mybatis.xml配置攔截器
注意:該配置受dtd約束影響,要注意配置的位置,不然根目錄會報錯
<plugins><!-- 配置分頁插件PageHelper, 4.0.0以后的版本支持自動識別使用的數(shù)據(jù)庫 --><plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin></plugins>
Mapper.xml的配置:
<select id="mhcx4" resultType="com.Bing.model.Book" parameterType="java.util.Map" >select<include refid="Base_Column_List" />from t_mvc_bookwhere bname like concat('%',#{bname},'%')</select>
在uils導(dǎo)入PageBean工具類
package com.Bing.util;import javax.servlet.http.HttpServletRequest; import java.io.Serializable; import java.util.Map;public class PageBean implements Serializable {private static final long serialVersionUID = 2422581023658455731L;//頁碼private int page=1;//每頁顯示記錄數(shù)private int rows=10;//總記錄數(shù)private int total=0;//是否分頁private boolean isPagination=true;//上一次的請求路徑private String url;//獲取所有的請求參數(shù)private Map<String,String[]> map;public PageBean() {super();}//設(shè)置請求參數(shù)public void setRequest(HttpServletRequest req) {String page=req.getParameter("page");String rows=req.getParameter("rows");String pagination=req.getParameter("pagination");this.setPage(page);this.setRows(rows);this.setPagination(pagination);this.url=req.getContextPath()+req.getServletPath();this.map=req.getParameterMap();}public String getUrl() {return url;}public void setUrl(String url) {this.url = url;}public Map<String, String[]> getMap() {return map;}public void setMap(Map<String, String[]> map) {this.map = map;}public int getPage() {return page;}public void setPage(int page) {this.page = page;}public void setPage(String page) {if(null!=page&&!"".equals(page.trim()))this.page = Integer.parseInt(page);}public int getRows() {return rows;}public void setRows(int rows) {this.rows = rows;}public void setRows(String rows) {if(null!=rows&&!"".equals(rows.trim()))this.rows = Integer.parseInt(rows);}public int getTotal() {return total;}public void setTotal(int total) {this.total = total;}public void setTotal(String total) {this.total = Integer.parseInt(total);}public boolean isPagination() {return isPagination;}public void setPagination(boolean isPagination) {this.isPagination = isPagination;}public void setPagination(String isPagination) {if(null!=isPagination&&!"".equals(isPagination.trim()))this.isPagination = Boolean.parseBoolean(isPagination);}/*** 獲取分頁起始標(biāo)記位置* @return*/public int getStartIndex() {//(當(dāng)前頁碼-1)*顯示記錄數(shù)return (this.getPage()-1)*this.rows;}/*** 末頁* @return*/public int getMaxPage() {int totalpage=this.total/this.rows;if(this.total%this.rows!=0)totalpage++;return totalpage;}/*** 下一頁* @return*/public int getNextPage() {int nextPage=this.page+1;if(this.page>=this.getMaxPage())nextPage=this.getMaxPage();return nextPage;}/*** 上一頁* @return*/public int getPreivousPage() {int previousPage=this.page-1;if(previousPage<1)previousPage=1;return previousPage;}@Overridepublic String toString() {return "PageBean [page=" + page + ", rows=" + rows + ", total=" + total + ", isPagination=" + isPagination+ "]";} }
Mapper類方法:
List<Book> mhcx4(@Param("bname") String bname);
在Mapper.xml配置的方法名進(jìn)行定義相對應(yīng)的接口和實現(xiàn)類
接口方法:
List<Book> mhcx4(String bname, PageBean pageBean);
實現(xiàn)類:
@Overridepublic List<Book> mhcx4(String bname, PageBean pageBean) {if(pageBean!=null && pageBean.isPagination()){PageHelper.startPage(pageBean.getPage(),pageBean.getRows());}List<Book> books=bookMapper.mhcx4("圣墟");if(pageBean!=null && pageBean.isPagination()){PageInfo<Book> info = new PageInfo<Book>(books);System.out.println("當(dāng)前頁:"+ info.getPageNum());System.out.println("展示記錄數(shù):"+info.getPageSize());System.out.println("符合查詢條件的總記錄:"+info.getTotal());pageBean.setTotal((int) info.getTotal());}return books;}
測試:
@Testpublic void mhcx4() {PageBean pageBean=new PageBean();//實例化PageBeanpageBean.setPage(2);//第幾頁pageBean.setRows(15);//顯示的條目bookBiz.mhcx4("圣墟",pageBean).forEach(System.out::println);//模糊查詢}
運行結(jié)果:
如果不想用分頁功能單純的想展示所有數(shù)據(jù)的話,在測試的時候調(diào)用下面方法即可
pageBean.setPagination(false);
三,Mybatis特殊字符
?MyBatis 中,有幾個特殊字符需要進(jìn)行轉(zhuǎn)義或處理,以避免 SQL 解析錯誤或相關(guān)問題。以下是一些常見的特殊字符及其處理方式
< 和?
>
:? ? ?這兩個字符在 XML 文件中有特殊含義,因此需要進(jìn)行轉(zhuǎn)義??梢?span style="color:#fe2c24;">使用?
<
?和?>
?分別表示?<
?和?>
。
&
:? ? ?也需要進(jìn)行轉(zhuǎn)義,可以使用?
&
?表示
'
:? ? 當(dāng)在 SQL 查詢語句中使用字符串參數(shù)時,需要注意單引號的處理??梢允褂秒p引號包裹字符串,或者使用兩個單引號來轉(zhuǎn)義一個單引號。例如:
SELECT * FROM user WHERE name = "John"
?或?SELECT * FROM user WHERE name = 'O''Connor'
。
%
?和?_
:? ? ?在使用 LIKE 語句進(jìn)行模糊查詢時,
%
?表示任意數(shù)量的字符,而?_
?表示一個字符。如果需要在字符串中使用字面量?%
?和?_
,則需要使用?\
?進(jìn)行轉(zhuǎn)義。例如:SELECT * FROM user WHERE name LIKE '%\%%' ESCAPE '\'
。
|
?和?&
:? ? 在使用動態(tài) SQL 標(biāo)簽(如?
<if>
、<choose>
、<foreach>
?等)時,|
?表示邏輯 OR 運算符,而?&
?表示邏輯 AND 運算符。如果要顯示字面量的?|
?和?&
?,則需要使用?\
?進(jìn)行轉(zhuǎn)義。例如:<![CDATA[${param1} \| ${param2}]]>
。