Data page (physical page and logical page)

# Data paging

## essential parameters

#### total number of data (count) Article

+ Source: obtained from a database query

How many pieces of data per #### impressions (pageSize)

+ Source: front-end acquisition

#### Page (currentPage) is currently located

+ Source: front-end acquisition

#### total number of pages (countPage)

+ Source: computed from

> Total = total number of pages of data / number of impressions per
>
> countPage = COUNT / the pageSize

#### data itself

+ Source: database query

## physical pages

What is the physical page ####

The so-called physical page is actually part of the data is returned directly to achieve through the database. A time to query data from a database. Correspondingly, there are logical page.

#### Mysql in the realization of the statement

```sql
SELECT * FROM `shop` LIMIT (currentPage-1)*pageSize, pageSize;
```

#### Java program

Java `` `
Private void doPhysicalPaganation (the HttpServletRequest REQ, the HttpServletResponse RESP) throws ServletException, IOException {

// page acquisition parameters passed over
String sCurrentPage = req.getParameter (" The currentPage ");
String sPageSize = req.getParameter (" the pageSize ") ;

// default display the first page of the current page
int. 1 = the currentPage;
the try {
// If the distal end is passed specific page, we use pass over the distal
the currentPage = the Integer.parseInt (sCurrentPage);
} the catch (a NumberFormatException E) {
}

int the pageSize = 10;
the try {
// If the distal end is passed specific page, we use pass over the front end of
the pageSize = the Integer.parseInt (sPageSize);
} the catch (a NumberFormatException E) {
}

Paganation <Student> = new new Paganation paganation <> ();
paganation.setCurrentPage(currentPage);
paganation.setPageSize(pageSize);

// 分页查询
studentDao.findByPageWithPhysical(paganation);

req.setAttribute("page", paganation);

req.getRequestDispatcher("physical.jsp").forward(req, resp);
}
```

Java `` `
public Paganation <Student> findByPageWithPhysical (Paganation <Student> pageParam) {

Paganation <Student> = Page pageParam;

Connection Conn = null;
the PreparedStatement pstmt = null;
the ResultSet RS = null;
the try {
// Get the connection
conn = JDBCTools .getConnection ();

// get the data of the current page to show the
// create pre-compiled object, pre-compiled SQL statement
pstmt = conn.prepareStatement ( "the SELECT * from Student limit,??");
// first keyword limit skip parameter it indicates how many data the result set
pstmt.setInt (. 1, (pageParam.getCurrentPage () -. 1) * pageParam.getPageSize ());
// second parameter limit keyword from the result set how many take data
pstmt.setInt (2, pageParam.getPageSize ());

// execute the statement, obtaining the result set
RS = pstmt.executeQuery ();

// set the result package
next ()) { total count = rs.getInt (1); }






















page.setTotalCount(totalCount);

} catch (SQLException e) {
e.printStackTrace();
}

return page;
}
```

 

## logical page

What is the logical page ####

In fact, the so-called logical page to page through the memory. Specifically, the first time the query data, remove all data into memory, display data from other pages to achieve the interception of data in memory, display.

#### Mysql in the realization of the statement

```sql
SELECT * FROM `shop` ;
```

#### Java program

Java `` `
Private void doLogicPaganation (the HttpServletRequest REQ, the HttpServletResponse RESP) throws ServletException, IOException {


// page acquisition parameters passed over
String sCurrentPage = req.getParameter (" The currentPage ");
String sPageSize = req.getParameter (" the pageSize ") ;

// default display the first page of the current page
int. 1 = the currentPage;
the try {
// If the distal end is passed specific page, we use pass over the distal
the currentPage = the Integer.parseInt (sCurrentPage);
} the catch (a NumberFormatException E) {
}

int the pageSize = 10;
the try {
// If the distal end is passed specific page, we use pass over the front end of
the pageSize = the Integer.parseInt (sPageSize);
} the catch (a NumberFormatException E) {
}

paganation <Student> = new new paganation paganation <> ();
paganation.setCurrentPage(currentPage);
paganation.setPageSize(pageSize);

// 分页查询
studentDao.findByPageWithLogic(paganation);

req.setAttribute("page", paganation);

req.getRequestDispatcher("logic.jsp").forward(req, resp);


}
```

`` `Java
List <Student> Cache = new new the ArrayList <> ();
public Paganation <Student> findByPageWithLogic (Paganation <Student> pageParam) {
Paganation <Student> Page = pageParam;

Connection Conn = null;
the PreparedStatement pstmt = null;
the ResultSet = null RS;
the try {
List <Student> DATAS = null;
// when the cache data, this page fetch data directly from the cache, without having to search the database
iF (cache.size ()> 0) {
DATAS getPageData = ( page);
} the else {

// If there is no data in the cache, the query from the database all the data into the cache

// get the connection
conn = JDBCTools.getConnection ();
// get the data in the current page to show the
// create pre compiled objects, pre-compiled SQL statement
pstmt = conn.prepareStatement ( "the SELECT * from Student");

// execute the statement, get the result set
rs = pstmt.executeQuery ();

// set the result package
cache.clear ();
the while (rs.next ()) {
Student Student Student new new = ();
student.setStuId (rs.getString ( "stu_id"));
student.setStuName (rs.getString ( "stu_name"));
student.setSex (rs.getString ( "Sex"));
student.setAge (rs.getInt ( "Age"));
student.setScience (rs.getString ( "OF S."));
Student. setTelephone (rs.getString ( "Telephone"));
Cache.Add (Student);
}

// the acquired data to show this page to be encapsulated into a page object
DATAS = getPageData (page);
}

page.setData (DATAS );
page.setTotalCount (cache.size ());

} the catch (SQLException E) {
e.printStackTrace ();
}

return Page;
}

/**
* 从缓存中获取当前页要展示的数据
* @param page
* @return
*/
private List<Student> getPageData(Paganation<Student> page) {
List<Student> datas = new ArrayList<>();
int startIdx = (page.getCurrentPage() - 1)*page.getPageSize();
int endIdx = page.getCurrentPage() * page.getPageSize();
if(endIdx > cache.size()) {
endIdx = cache.size();
}
datas = cache.subList(startIdx, endIdx);
return datas;
}
```

 

## two kinds of advantages and disadvantages of paging

1. paging logic high efficiency, take up memory space for data query, but less number of inquiries and more cases.
2. physical page inefficient, save memory space for the number of inquiries but few large data queries situation.

Guess you like

Origin www.cnblogs.com/tsymqwb/p/11773787.html