這篇文章主要介紹ssm如何實現(xiàn)分頁查詢,文中介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們一定要看完!
創(chuàng)新互聯(lián)"三網(wǎng)合一"的企業(yè)建站思路。企業(yè)可建設(shè)擁有電腦版、微信版、手機版的企業(yè)網(wǎng)站。實現(xiàn)跨屏營銷,產(chǎn)品發(fā)布一步更新,電腦網(wǎng)絡(luò)+移動網(wǎng)絡(luò)一網(wǎng)打盡,滿足企業(yè)的營銷需求!創(chuàng)新互聯(lián)具備承接各種類型的網(wǎng)站制作、網(wǎng)站設(shè)計項目的能力。經(jīng)過十多年的努力的開拓,為不同行業(yè)的企事業(yè)單位提供了優(yōu)質(zhì)的服務(wù),并獲得了客戶的一致好評。
ssm整合實現(xiàn)分頁查詢
一、通過limit查詢語句實現(xiàn)分頁,并展示
1.mapper.xml配置
<select id="selectUsersByPage" parameterType="int" resultMap="UserMap"> SELECT * number from user limit #{page},10 </select>
查詢user表,從第page項開始,每次返回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 > <table> <tr > <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">首頁</div> <div class="page_cell" ip="up_page">上一頁</div> <div ><%=session.getAttribute("page")%>/${ulist[0].number}</div> <div class="page_cell" onclick="next_page(<%=session.getAttribute("page")%>)">下一頁</div> <div class="page_cell">末頁</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
/** * 下一頁 */ 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++){ //.....異步刷新頁面 } }, error:function(data){ alert("網(wǎng)絡(luò)連接錯誤"); } }); }
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"; } /** * 用戶信息分頁查詢 * @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); }
問題:在ajax傳遞json對象的時候,發(fā)生了415錯誤(未知媒體錯誤)
原因:
<mvc:annotation-driven />會自動注冊DefaultAnnotationHandlerMapping和AnnotationMethodHandlerAdapter兩個bean ,AnnotationMethodHandlerAdapter將會初始化7個轉(zhuǎn)換器,可以通過調(diào)用AnnotationMethodHandlerAdapter的getMessageConverts()方法來獲取轉(zhuǎn)換器的一個集合 List<HttpMessageConverter>
ByteArrayHttpMessageConverter StringHttpMessageConverter ResourceHttpMessageConverter SourceHttpMessageConverter XmlAwareFormHttpMessageConverter Jaxb2RootElementHttpMessageConverter MappingJacksonHttpMessageConverter
解決:對于json的解析就是通過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>
加了依賴包后問題就完美解決了,運行結(jié)果如下:
以上查詢的數(shù)據(jù)是通過存儲過程批量插入的:
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如何實現(xiàn)分頁查詢”這篇文章的所有內(nèi)容,感謝各位的閱讀!希望分享的內(nèi)容對大家有幫助,更多相關(guān)知識,歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道!
網(wǎng)站欄目:ssm如何實現(xiàn)分頁查詢
文章來源:http://jinyejixie.com/article18/ggidgp.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供企業(yè)網(wǎng)站制作、小程序開發(fā)、網(wǎng)站收錄、網(wǎng)站設(shè)計公司、、微信小程序
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)