使用Mybatis實現(xiàn)分頁效果示例
一、創(chuàng)建分頁的工具類PageUti,存儲分頁所需要的五個數(shù)據(jù):pageIndex(當前頁碼),pageSize(每頁記錄條數(shù)),count(總記錄數(shù)),pageNum(總頁數(shù)),list(每一頁的內(nèi)容)。
/**
* @param <T> 泛型類
*/
public class PageUtil<T> {
//當前頁碼
private int pageIndex;
//每頁記錄數(shù)
private int pageSize;
//總記錄數(shù)
private int count;
//總頁數(shù)
private int pageNum;
//每一頁的內(nèi)容
private List<T> list;
}
二、xml文件:MySQL對分頁的支持是通過limit子句。
如:
# 每頁10條記錄,取第一頁,返回的是前10條記錄?
select * from?books limit (1-1)*10,10;
# 每頁10條記錄,取第二頁,返回的是第11條記錄,到第20條記錄,?
select * from?books limit (2-1)*10,10;?
所以:limit的第一個參數(shù)=(當前頁碼-1)*每頁記錄數(shù)
<!--分頁,查詢當前頁的內(nèi)容-->
<select id="getBooksPage" resultType="list" parameterType="map">
select * from books limit #{pageIndex},#{pageSize}
</select>
<!--查詢總記錄數(shù)-->
<select id="getCount" resultType="int" parameterType="map">
select count(*) from books
</select>
三、BooksServiceImpl.java:將limit需要的兩個參數(shù)存入map集合中,再將分頁所需要的五個數(shù)據(jù)封裝。
public PageUtil<Books> getBooksPage(int pageIndex, int pageSize) {
PageUtil<Books> pageUtil=new PageUtil<>();
SqlSession sqlSession= MybatisUtil.getSqlSession();
try {
//booksMapper是使用動態(tài)代理模式,運行反射機制,創(chuàng)建的一個代理對象
BooksMapper booksMapper=sqlSession.getMapper(BooksMapper.class);
Map map=new HashMap();
int row=booksMapper.getCount(map);
//計算總頁數(shù)
int pageCount= (int) Math.ceil((double) row / pageSize);
//limit的第一個參數(shù)=(當前頁碼-1)*每頁記錄數(shù)
map.put("pageIndex",(pageIndex - 1) * pageSize);
map.put("pageSize", pageSize);
List<Books> list=booksMapper.getBooksPage(map);
//將數(shù)據(jù)封裝
pageUtil.setPageIndex(pageIndex);
pageUtil.setPageSize(pageSize);
pageUtil.setCount(row);
pageUtil.setPageNum(pageCount);
pageUtil.setList(list);
}catch (Exception e){
e.printStackTrace();
}finally {
sqlSession.close();
}
return pageUtil;
}
四、servlet:
protected void getBooks(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
if(request.getParameter("pageIndex")!=null) {
int pageIndex=Integer.parseInt(request.getParameter("pageIndex"));
show(response,pageIndex,search);
}else {
show(response);
}
}
private void show(HttpServletResponse response,Object ...ages) throws IOException {
int pageIndex = 1;//默認第一頁
if(ages.length>0){
pageIndex=(int)ages[0];
}
PageUtil pageUtil=new PageUtil();
//每頁10條數(shù)據(jù)
pageUtil=new BooksServiceImpl().getBooksPage(pageIndex,10);
String js= JSON.toJSONString(pageUtil);
PrintWriter pw=response.getWriter();
pw.write(js);
}
五、前段頁面
顯示分頁:
var page = "<li>共" + data.count + "條,共" + data.pageNum + "頁,當前第" + data.pageIndex + "頁</li>";
page += "<li><a href='#' onclick='myPage(1)'>首頁</a></li>";
if (data.pageIndex == 1) {
page += "<li><a href='#' onclick='myPage(1)'>上一頁</a></li>";
} else {
page += "<li><a href='#' onclick='myPage(" + (data.pageIndex - 1) + ")'>上一頁</a></li>";
}
// for (var i = 1; i <= data.pageNum; i++) {
// page += "<a href='#' onclick='myPage(" + i + ")'>" + i + "</a>"
// }
if (data.pageIndex < data.pageNum) {
page += "<li><a href='#' onclick='myPage(" + (data.pageIndex + 1) + ")'>下一頁</a></li>";
} else {
page += "<li><a href='#' onclick='myPage(" + data.pageNum + ")'>下一頁</a></li>";
}
page += "<li><a href='#' onclick='myPage(" + data.pageNum + ")'>末頁</a></li>";
點擊分頁(myPage):通過ajax請求將頁碼傳到servlet
function myPage(index) {
console.log(index)
$.getJSON("books/getBooks.do", {"pageIndex": index}, function (data) {
showBooks(data)
});
}
六、MybatisUtil.java工具類:向外提供一個方法,可以獲取SqlSession對象
public class MybatisUtil {
static SqlSessionFactory sqlSessionFactory;
// 加載主配置文件,并創(chuàng)建 sqlSessionFactory
static {
String resource = "com/xmx/config/mybatis-config.xml";
InputStream inputStream = null;
try {
inputStream = Resources.getResourceAsStream(resource);
} catch (IOException e) {
e.printStackTrace();
}
sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
}
/**
* @Description: 向外提供一個方法,可以獲取SqlSession對象
* @Return: org.apache.ibatis.session.SqlSession
*/
public static SqlSession getSqlSession() {
// return sqlSessionFactory.openSession(true);
return sqlSessionFactory.openSession();
}
}
到此這篇關(guān)于使用Mybatis實現(xiàn)分頁效果示例的文章就介紹到這了,更多相關(guān)Mybites實現(xiàn)分頁內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Jenkins Host key verification failed問題解決
這篇文章主要介紹了Jenkins Host key verification failed問題解決,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下2019-11-11
Java中HTTP GET方法調(diào)用帶有body的問題解決
這篇文章主要為大家詳細介紹了Java如何解決HTTP GET方法調(diào)用帶有body的問題,文中的示例代碼講解詳細,感興趣的小伙伴可以參考一下2024-02-02
java中synchronized關(guān)鍵字的3種寫法實例
synchronized是Java中的關(guān)鍵字,是一種同步鎖,下面這篇文章主要給大家介紹了關(guān)于java中synchronized關(guān)鍵字的3種寫法,文中通過示例代碼介紹的非常詳細,需要的朋友可以參考下2021-11-11
Java中使用LocalDate根據(jù)日期來計算年齡的實現(xiàn)方法
這篇文章主要介紹了Java中使用LocalDate根據(jù)日期來計算年齡的實現(xiàn)方法,需要的朋友可以參考下2018-01-01
vue+springboot項目上傳部署tomcat的方法實現(xiàn)
本文主要介紹了vue+springboot項目上傳部署tomcat的方法實現(xiàn),包括環(huán)境準備、配置調(diào)整以及部署步驟,文中通過圖文及示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2024-01-01
詳解Java并發(fā)編程之內(nèi)置鎖(synchronized)
這篇文章主要介紹了Java并發(fā)編程之內(nèi)置鎖(synchronized)的相關(guān)知識,本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2021-03-03

