Sample Code for implementing the paging function in the java ssm framework (oracle) and ssm sample code
The pagination feature of java web is described as follows:
Framework: ssm
Database: oracle
In other words, oracle's paging query is much more complicated than mysql. Here is a brief introduction:
Query the top 10 data records:
SELECT * FROM( SELECT ROWNUM WN,RN.* FROM ( SELECT id, title, create_time as createTime, musictor, musictitle FROM krry_blog ORDER BY create_time desc )RN )WNWHERE WN <= 10 AND WN > 0
Complex syntax,
In the same result, the mysql syntax is: Use a LIMIT to solve the problem.
SELECT id, title, create_time as createTime, musictor, musictitleFROM krry_blog ORDER BY create_time descLIMIT 0,5
The establishment of the SSM framework is not much to mention. The previous blog has a detailed introduction. Here we will talk about implementing the java web paging function.
Js plug-ins: krry_page.js and jQuery
Er persistent layer:
BlogMapper. java
Package com. krry. mapper; import java. util. hashMap; import java. util. list; import com. krry. entity. params;/***** Mapper: database Operation * @ author krry * @ version 1.0.0 **/public interface BlogMapper {/*** query all blogs * @ param params * @ return */public List <HashMap <String, object> findBlogs (Params params);/***** calculate the number of blogs * com. krry. dao. admin * method name: countBlogs * @ author krry * @ param params * @ return int * @ exception * @ since 1.0.0 */public long countBlogs ();}
BlogMapper. xml
<? Xml version = "1.0" encoding = "UTF-8"?> <! DOCTYPE mapper PUBLIC "-// mybatis.org//DTD Mapper 3.0 //" http://mybatis.org/dtd/mybatis-3-mapper.dtd "> <mapper namespace =" com. krry. mapper. BlogMapper "> <! -- Query all blog information by page --> <select id = "findBlogs" resultType = "java. util. hashMap "parameterType =" Params "> SELECT * FROM (select rownum wn, RN. * FROM (SELECT id, title, create_time as createTime, musictor, musictitle FROM krry_blog order by create_time desc) RN) wn where wn <=#{ pageSize} and wn >#{ pageNo} </select> <! -- Query the number of blogs --> <select id = "countBlogs" resultType = "long"> SELECT count (*) FROM krry_blog </select> </mapper>
Service layer:
Interface Class: IBlogService. java
Package com. krry. service; import java. util. hashMap; import java. util. list; import javax. servlet. http. httpServletRequest; import org. apache. ibatis. annotations. param; import com. krry. entity. blog; import com. krry. entity. params;/*** service layer: Process business logic (implemented in impl) * @ author asusaad **/public interface IBlogService {/*** query all blogs by page * @ param params * @ return */public List <HashMap <String, object> findBlogs (Params params);/*** calculate the number of blogs * @ param params * @ return */public long countBlogs ();}
Impl implementation class: BlogService. java
Package com. krry. service. impl; import java. util. hashMap; import java. util. list; import org. springframework. beans. factory. annotation. autowired; import org. springframework. stereotype. service; import org. springframework. web. servlet. modelAndView; import com. krry. entity. params; import com. krry. mapper. blogMapper; import com. krry. service. IBlogService;/*** implement the service layer interface * @ author asusaad **/@ Servicepublic class BlogService implements IBlogService {@ Autowired private BlogMapper blogMapper; /*** query blog */public List <HashMap <String, Object> findBlogs (Params params) {// query blog information List <HashMap <String, object> blog = blogMapper. findBlogs (params); return blog;}/*** calculate the number of blogs * @ param params * @ return */public long countBlogs () {long coutBlogs = blogMapper. countBlogs (); return coutBlogs ;}}
Controller control layer:
KrryController. java
Package com. krry. controller; import java. util. hashMap; import java. util. list; import org. springframework. beans. factory. annotation. autowired; import org. springframework. stereotype. controller; import org. springframework. web. bind. annotation. requestMapping; import org. springframework. web. bind. annotation. responseBody; import org. springframework. web. servlet. modelAndView; import com. krry. entity. params; import com. krry. service. IBlogService;/*** KrryController * controller layer, which is used as request forwarding * @ author asusaad **/@ Controller // indicates multi-sample mode, the web layer returned by each user is a different public class KrryController {@ Autowired private IBlogService blogService;/*** homepage, and queries the * @ return */@ RequestMapping ("/index") page by page ") public ModelAndView index (Params params) {params. setPageNo (0); params. setPageSize (10); // query only 10 items at the beginning // call the business layer List <HashMap <String, Object> blogs = blogService. findBlogs (params); // query the number of blogs long coutBlogs = blogService. countBlogs (); ModelAndView modelAndView = new ModelAndView (); modelAndView. addObject ("blogs", blogs); modelAndView. addObject ("coutBlogs", coutBlogs); modelAndView. setViewName ("index"); return modelAndView;}/*** paging query of ajax requests * @ param params * @ return */@ ResponseBody @ RequestMapping ("/loadData ") public HashMap <String, Object> loadData (Params params) {HashMap <String, Object> map = new HashMap <String, Object> (); List <HashMap <String, object> blogs = blogService. findBlogs (params); map. put ("blogs", blogs); return map ;}}
Here there are two entity classes, which serve as the injection Blog for database query and two parameters for paging query Params:
Set the data parameter: pageNo (next page): the current page number * Number of pages to be displayed on the next page
PageSize (next page): the number of queries (pageNo) + the number to be displayed per page
In the database, it is WN <= pageSize and WN> pageNo to query paging data.
Blog. java
Package com. krry. entity;/***** User * @ author krry * @ version 1.0.0 **/public class Blog {// primary key private String id; // Blog title private String title; // music author private String musictor; // music title private String musictitle; // creation time private String createTime; public Blog (String id, String title, String musictor, String musictitle, string createTime) {this. id = id; this. title = title; this. musictor = musictor; this. musictitle = musictitle; this. createTime = createTime;} public String getId () {return id;} public void setId (String id) {this. id = id;} public String getTitle () {return title;} public void setTitle (String title) {this. title = title;} public String getMusictor () {return musictor;} public void setMusictor (String musictor) {this. musictor = musictor;} public String getMusictitle () {return musictitle;} public void setMusictitle (String musictitle) {this. musictitle = musictitle;} public String getCreateTime () {return createTime;} public void setCreateTime (String createTime) {this. createTime = createTime ;}}
Params. java
package com.krry.entity;/** * * Params * @author krry * @version 1.0.0 * */public class Params { private Integer pageSize = 0; private Integer pageNo = 0; public Integer getPageNo() { return pageNo; } public void setPageNo(Integer pageNo) { this.pageNo = pageNo; } public Integer getPageSize() { return pageSize; } public void setPageSize(Integer pageSize) { this.pageSize = pageSize; }}
Web page index. jsp
<% @ Page language = "java" import = "java. util. * "pageEncoding =" UTF-8 "%> <% @ taglib uri =" http://java.sun.com/jsp/jstl/core "prefix =" c "%> <% String path = request. getContextPath (); String basePath = request. getScheme () + ": //" + request. getServerName () + ":" + request. getServerPort () + path + "/"; pageContext. setAttribute ("basePath", basePath); %> <! Doctype html>
Paging:
The above is all the content of this article. I hope it will be helpful for your learning and support for helping customers.