ssm實(shí)現(xiàn)分頁(yè)查詢的實(shí)例
ssm整合實(shí)現(xiàn)分頁(yè)查詢
一、通過(guò)limit查詢語(yǔ)句實(shí)現(xiàn)分頁(yè),并展示
1.mapper.xml配置
<select id="selectUsersByPage" parameterType="int" resultMap="UserMap">
SELECT * number from user limit #{page},10
</select>
查詢user表,從第page項(xiàng)開始,每次返回10條數(shù)據(jù)
2.index.jsp
<html>
<head>
<title>page</title>
<link rel="stylesheet" type="text/css" href="css/index.css" rel="external nofollow" >
</head>
<body>
<div style="width: 100%;margin-top:20px;">
<table>
<tr style="background-color: #F5F5F5;">
<th>username</th>
<th>password</th>
<th>sex</th>
<th>email</th>
<th>createTime</th>
<th>updateTime</th>
</tr>
<div id = "show_data">
<c:choose>
<c:when test="${ulist != null}">
<c:forEach items="${ulist}" var="u">
<tr>
<td>${u.username}</td>
<td>${u.password}</td>
<td>${u.sex}</td>
<td>${u.email}</td>
<td><fmt:formatDate value="${u.create_time}" type="date"/></td>
<td><fmt:formatDate value="${u.update_time}" type="date"/></td>
</tr>
</c:forEach>
</c:when>
<c:otherwise>
<tr>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
</tr>
</c:otherwise>
</c:choose>
</div>
</table>
<div class="page">
<div class="page_cell">首頁(yè)</div>
<div class="page_cell" ip="up_page">上一頁(yè)</div>
<div style="float: left;margin: 2px"><%=session.getAttribute("page")%>/${ulist[0].number}</div>
<div class="page_cell" onclick="next_page(<%=session.getAttribute("page")%>)">下一頁(yè)</div>
<div class="page_cell">末頁(yè)</div>
</div>
</div>
</body>
<script type="text/javascript" src="js/index.js"></script>
<script type="text/javascript" src="js/jquery.js"></script>
</html>
3.css
body{
width: 100%;
margin: 0;
}
table{
border:1px solid red;
text-align: center;
margin: auto;
border-collapse: collapse;
}
tr{
border: 1px solid #ddd
}
th{
width: 150px;
font-weight: 700;
height: 36px;
}
td{
height: 36px;
}
.page{
margin: auto;
width: 300px;
text-align: center;
margin-top: 10px;
}
.page_cell{
float: left;
width: 50px;
border:1px solid #F5F5F5;
margin:2px;
cursor: pointer;
}
.page_cell:hover{
-webkit-box-shadow: #777 0px 0px 1px;
}
4.js
/**
* 下一頁(yè)
*/
function next_page(page){
var data = {
"page":page
};
$.ajax({
type:"post",
url:"/RoleControl/next_page.do",
data:JSON.stringify(data),
dataType:"json",
contentType:"application/json",
success:function(data){
var show_data = document.getElementById("show_data")
show_data.innerHTML = " ";
for(i=0; i<data.length; i++){
//.....異步刷新頁(yè)面
}
},
error:function(data){
alert("網(wǎng)絡(luò)連接錯(cuò)誤");
}
});
}
5.控制器
@RequestMapping("/index.do")
public String index(ModelMap map, HttpSession session){
session.setAttribute("page",1);
List<User> ulist = userService.selectUsersByPage(0);
map.put("ulist",ulist);
return "index";
}
/**
* 用戶信息分頁(yè)查詢
* @param params
* @return
*/
@RequestMapping(value = "/next_page.do",method = RequestMethod.POST)
@ResponseBody
public String getUsersByPage(@RequestBody JSONObject params){
// Map<String,String> paramsMap = JSON.parseObject(params,new TypeReference<Map<String,String>>(){});
System.out.println(params.get("page").toString());
List<User> ulist = userService.selectUsersByPage(Integer.parseInt(params.get("page").toString())*10);
return JSON.toJSONString(ulist);
}
問(wèn)題:在ajax傳遞json對(duì)象的時(shí)候,發(fā)生了415錯(cuò)誤(未知媒體錯(cuò)誤)

原因:
<mvc:annotation-driven />會(huì)自動(dòng)注冊(cè)DefaultAnnotationHandlerMapping和AnnotationMethodHandlerAdapter兩個(gè)bean ,AnnotationMethodHandlerAdapter將會(huì)初始化7個(gè)轉(zhuǎn)換器,可以通過(guò)調(diào)用AnnotationMethodHandlerAdapter的getMessageConverts()方法來(lái)獲取轉(zhuǎn)換器的一個(gè)集合 List<HttpMessageConverter>
ByteArrayHttpMessageConverter StringHttpMessageConverter ResourceHttpMessageConverter SourceHttpMessageConverter XmlAwareFormHttpMessageConverter Jaxb2RootElementHttpMessageConverter MappingJacksonHttpMessageConverter
解決:對(duì)于json的解析就是通過(guò)MappingJacksonHttpMessageConverter轉(zhuǎn)換器完成的。所以就需要加入jackson依賴包:
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>2.5.2</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.5.2</version>
</dependency>
加了依賴包后問(wèn)題就完美解決了,運(yùn)行結(jié)果如下:

以上查詢的數(shù)據(jù)是通過(guò)存儲(chǔ)過(guò)程批量插入的:
begin
declare pid int;
set pid = 10000;
while pid>0 DO
insert into user values (pid,'pw','sex','email',now(),now());
set pid = pid-1;
end while;
end
這篇ssm實(shí)現(xiàn)分頁(yè)查詢的實(shí)例就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
mybatis3使用@Select等注解實(shí)現(xiàn)增刪改查操作
這篇文章主要介紹了mybatis3使用@Select等注解實(shí)現(xiàn)增刪改查操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-11-11
一篇文章帶你了解MySQL數(shù)據(jù)庫(kù)基礎(chǔ)
這篇文章主要介紹了MySql數(shù)據(jù)庫(kù)基礎(chǔ)知識(shí)點(diǎn),總結(jié)整理了mysql數(shù)據(jù)庫(kù)基本創(chuàng)建、查看、選擇、刪除以及數(shù)據(jù)類型相關(guān)操作技巧,需要的朋友可以參考下2021-08-08
Apache Commons Math3學(xué)習(xí)之?dāng)?shù)值積分實(shí)例代碼
這篇文章主要介紹了Apache Commons Math3學(xué)習(xí)之?dāng)?shù)值積分實(shí)例代碼,涉及使用辛普森積分的例子,這里分享給大家,供需要的朋友參考。2017-10-10
SpringBoot項(xiàng)目@Async方法問(wèn)題解決方案
這篇文章主要介紹了SpringBoot項(xiàng)目@Async方法問(wèn)題解決方案,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-04-04
MyBatis中RowBounds實(shí)現(xiàn)內(nèi)存分頁(yè)
RowBounds是MyBatis提供的一種內(nèi)存分頁(yè)方式,適用于小數(shù)據(jù)量的分頁(yè)場(chǎng)景,本文就來(lái)詳細(xì)的介紹一下,具有一定的參考價(jià)值,感興趣的可以了解一下2024-12-12
Java日志框架用法及常見(jiàn)問(wèn)題解決方案
這篇文章主要介紹了Java日志框架用法及常見(jiàn)問(wèn)題解決方案,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-10-10
Spring?Boot虛擬線程Webflux在JWT驗(yàn)證和MySQL查詢性能比較
RabbitMQ高級(jí)應(yīng)用之消費(fèi)端限流策略basicQos詳解
Struts2學(xué)習(xí)手冊(cè)之文件上傳基礎(chǔ)教程

