Mybatis implements the simple implementation of physical paging and mybatis paging.

Source: Internet
Author: User

Mybatis implements the simple implementation of physical paging and mybatis paging.

Paging query:

First, we need to clarify what is paging? Why paging?

Pagination is to display data in multiple pages. Using pagination can improve customers' feelings.

 

Paging classification:

1. Physical paging: only query data on the current page from the database

Advantage: not occupying a lot of memory disadvantages: low cost efficiency (compared to logical paging)

2. Logical paging: Query all records from the database, store them to the memory, display the current page, and then obtain the data directly from the memory.

Advantage: High Efficiency disadvantage: High memory usage

 

In most cases, we use physical paging.

Physical paging:

1. Use jdbc directly: Use a rolling result set

Advantage: Cross-database disadvantage: low performance

2. Use the paging operation provided by the database itself: Use the specific paging function of each database

Advantage: High Performance disadvantage: not cross-Database

 

Mysql: limit

SQL service: top

Oracle: rownum

 

I use mysql, so let's talk about limit.

Select * from Table limit m, n;

M: indicates the number of entries starting from 0)

N: queries several records.

If we want to display three records, we need to query the data on the 2nd page.

Select * from Table limit (page-1) * number of entries per page;

 

Next, we begin to write entity classes.

The get \ set method is omitted.

Terms encapsulate what you want to provide to the front-end

 

Then write in the corresponding mapper:

Note:

1. The two parameters must be 0, 1, and cannot be named. An error is returned.

2. SQL cannot be used for computation. You need to computation the formula before replacing it. Otherwise, an error will be reported.

 

Write service layer

 

PageNum is the number of entries to be displayed on each page.

CurrentPage is the page from the front-end

In the control layer simulation, the control layer is omitted.

 

The structure of the final returned page is the result of paging.

 

PageNum corresponds to the current page 0

The number of entries displayed on each page is 2.

If the other information is omitted, enter it.

By analyzing the advantages and disadvantages of mybatis plug-in Paging

In the end, manual paging is a summary of today's work.

 

Continue with the new paging knowledge later, including the use of the mybatis paging plug-in. I hope all the great gods can leave comments to supplement the comments and make some mistakes. Thank you!

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.