您好,登錄后才能下訂單哦!
這篇文章主要介紹ssm如何實現分頁查詢,文中介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們一定要看完!
ssm整合實現分頁查詢
一、通過limit查詢語句實現分頁,并展示
1.mapper.xml配置
<select id="selectUsersByPage" parameterType="int" resultMap="UserMap"> SELECT * number from user limit #{page},10 </select>
查詢user表,從第page項開始,每次返回10條數據
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("網絡連接錯誤"); } }); }
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對象的時候,發生了415錯誤(未知媒體錯誤)
原因:
<mvc:annotation-driven />會自動注冊DefaultAnnotationHandlerMapping和AnnotationMethodHandlerAdapter兩個bean ,AnnotationMethodHandlerAdapter將會初始化7個轉換器,可以通過調用AnnotationMethodHandlerAdapter的getMessageConverts()方法來獲取轉換器的一個集合 List<HttpMessageConverter>
ByteArrayHttpMessageConverter StringHttpMessageConverter ResourceHttpMessageConverter SourceHttpMessageConverter XmlAwareFormHttpMessageConverter Jaxb2RootElementHttpMessageConverter MappingJacksonHttpMessageConverter
解決:對于json的解析就是通過MappingJacksonHttpMessageConverter轉換器完成的。所以就需要加入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>
加了依賴包后問題就完美解決了,運行結果如下:
以上查詢的數據是通過存儲過程批量插入的:
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如何實現分頁查詢”這篇文章的所有內容,感謝各位的閱讀!希望分享的內容對大家有幫助,更多相關知識,歡迎關注億速云行業資訊頻道!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。