Mysql分页_MySQL
分页其实很简单,无非就是根据sql语句,加上限制条件,显示从第几条数据,到第几条数据而已。
切入正题,先看一下下面的例子。
我有一张表sjdr_product_detail
首先查询全部:
<code class="hljs cs">select * from sjdr_product_detail; //一共17条数据</code>
<img src="/static/imghw/default1.png" data-src="http://img.bitscn.com/upimg/allimg/c150817/143aO91N5940-102J7.jpg" class="lazy" alt="这里写图片描述" title="\" />
分页,就是通过sql语句的limit关键字来限制条件。
<code class="hljs cs"><code class="hljs haml">select * from sjdr_product_detail limit #{start},#{pageSize}; </code></code>
<code class="hljs haml">start 和 pageSize 都用 #{ } 标注起来,是想让大家知道,这两个值是变量,即需要限制的条件。其实pageSize应该是可以固定的,比如每页显示3条,我们固定死。所以,下面主要的就是来得到这个start的值,那么到底这个值怎么得到呢?请看下面
<code class="hljs haml"><b>实验,找出规律</b>
<code class="hljs haml"><b>首先,pageSize = 3 ,即每页显示条数为3条</b>
<code class="hljs haml">查询第1页
<code class="hljs cs"><code class="hljs haml"><code class="hljs cs">#第1页 select * from sjdr_product_detail limit 0,3; #第2页 select * from sjdr_product_detail limit 3,3; #第3页 select * from sjdr_product_detail limit 6,3; #第4页 select * from sjdr_product_detail limit 9,3; #第5页 select * from sjdr_product_detail limit 12,3; #第6页 select * from sjdr_product_detail limit 15,3;</code></code></code>
<code class="hljs haml"><code class="hljs cs"><img src="/static/imghw/default1.png" data-src="http://img.bitscn.com/upimg/allimg/c150817/143aO91R1X0-119328.jpg" class="lazy" alt="这里写图片描述" title="\" />
<code class="hljs haml"><code class="hljs cs">数据一目了然,那么我们来研究一下,页数、start、pageSize 之间的关系
<code class="hljs haml"><code class="hljs cs">当前页 |
<code class="hljs haml"><code class="hljs cs">start |
<code class="hljs haml"><code class="hljs cs">pageSize |
<code class="hljs haml"><code class="hljs cs">1 |
<code class="hljs haml"><code class="hljs cs">0 |
<code class="hljs haml"><code class="hljs cs">3 |
<code class="hljs haml"><code class="hljs cs">2 |
<code class="hljs haml"><code class="hljs cs">3 |
<code class="hljs haml"><code class="hljs cs">3 |
<code class="hljs haml"><code class="hljs cs">3 |
<code class="hljs haml"><code class="hljs cs">6 |
<code class="hljs haml"><code class="hljs cs">3 |
<code class="hljs haml"><code class="hljs cs">4 |
<code class="hljs haml"><code class="hljs cs">9 |
<code class="hljs haml"><code class="hljs cs">3 |
<code class="hljs haml"><code class="hljs cs">5 |
<code class="hljs haml"><code class="hljs cs">12 |
<code class="hljs haml"><code class="hljs cs">3 |
<code class="hljs haml"><code class="hljs cs">6 |
<code class="hljs haml"><code class="hljs cs">15 |
<code class="hljs haml"><code class="hljs cs">3 |
<code class="hljs haml"><code class="hljs cs"><b>规律:从以上不难看出: start = (当前页-1)* pageSize</b>
<code class="hljs cs"><code class="hljs haml"><code class="hljs cs">0 = (1-1) * 3 3 = (2-1) * 3 6 = (3-1) * 3 9 = (4-1) * 3 12= (5-1) * 3 15= (6-1) * 3 </code></code></code>
<code class="hljs haml"><code class="hljs cs">既然我们得到了这个规律,那么下面就容易多了。怎么把它整合到我们的项目当中去?
<code class="hljs haml"><code class="hljs cs">首先,我们见一个分页工具类PageUtil.class
<code class="hljs cs"><code class="hljs haml"><code class="hljs cs"><code class="hljs cs">package cn.sg.util; /** * @author ZSL */ import java.util.ArrayList; import java.util.List; public class PageUtil { public PageUtil(int curr,int size,int total){ this.pageSize = size; this.totalPage = total % size == 0 ? total/size : (total/size) + 1; this.currPage = curr < 1 ? 1 : curr; this.currPage = curr > this.totalPage ? this.totalPage : this.currPage; skips = (this.currPage - 1) * this.pageSize; this.totalCount = total; } private int currPage = 1; //当前页 private int pageSize = 10; //每页显示条数 private int totalPage = 0; //总页数 private int totalCount = 0; //总条数 private int skips = 0; //跳过的值,即sql中的start值 private List rows = new ArrayList(); //用来存放查询到的数据集合 ////////////////////////////////////////////////////// //getter、setter 方法 public int getCurrPage() { return currPage; } public void setCurrPage(int currPage) { this.currPage = currPage; } public int getPageSize() { return pageSize; } public void setPageSize(int pageSize) { this.pageSize = pageSize; } public int getTotalPage() { return totalPage; } public void setTotalPage(int totalPage) { this.totalPage = totalPage; } public int getTotalCount() { return totalCount; } public void setTotalCount(int totalCount) { this.totalCount = totalCount; } public List getRows() { return rows; } public void setRows(List rows) { this.rows = rows; } public int getSkips() { return skips; } public void setSkips(int skips) { this.skips = skips; } } </code></code></code></code>
<code class="hljs haml"><code class="hljs cs"><code class="hljs cs">2 我这里用的是mybatis
<code class="hljs cs"><code class="hljs haml"><code class="hljs cs"><code class="hljs cs"><code class="hljs xml"><!-- 查询数量 --> <select id="getCount" resulttype="java.lang.Integer"> select count(1) from sjdr_product_detail; </select> <!-- 分页查询 --> <select id="queryByPage" resultmap="detail_resultMap" resulttype="Detail"> select * from sjdr_product_detail order by P_ID DESC limit #{skips},#{pageSize}; </select></code></code></code></code></code>
<code class="hljs haml"><code class="hljs cs"><code class="hljs cs"><code class="hljs xml">3 Controller
<code class="hljs cs"><code class="hljs haml"><code class="hljs cs"><code class="hljs cs"><code class="hljs xml"><code class="hljs java">@RequestMapping("detail_page.htm") public void queryByPage(HttpServletRequest req, Model model){ //当前页 int curr = Integer.valueOf(req.getParameter("curr")); //每页显示条数 int size = Integer.parseInt(req.getParameter("size")); //查询数量 int total = this.detailService.getCount(); PageUtil page = new PageUtil(curr, size, total); List<detail> detailList = this.detailService.queryByPage(page); page.setRows(detailList); //转成json格式 WriteResponseUtil util = new WriteResponseUtil(); util.writeResponse(JsonUtil.toJSONString(page), res); } </detail></code></code></code></code></code></code>
<code class="hljs haml"><code class="hljs cs"><code class="hljs cs"><code class="hljs xml"><code class="hljs java">所以分页查询的时候,只要传递页数和每页显示条数即可。我这边是倒叙查询<br />
查询结果:
<code class="hljs haml"><code class="hljs cs"><code class="hljs cs"><code class="hljs xml"><code class="hljs java">detail_page.htm?curr=1&size=3
<code class="hljs cs"><code class="hljs haml"><code class="hljs cs"><code class="hljs cs"><code class="hljs xml"><code class="hljs java"><code class="hljs cs">{ "currPage": 1, //第一页 "pageSize": 3, //每页显示条数 "rows": [ //查询数据集合 { "p_ID": "17", "p_PIC": "17777" }, { "p_ID": "16", "p_PIC": "16666" }, { "p_ID": "15", "p_PIC": "15555" } ], "skips": 0, //相当于sql中的start "totalCount": 17, //总条数 "totalPage": 6 //总页数 }</code></code></code></code></code></code></code>
<code class="hljs haml"><code class="hljs cs"><code class="hljs cs"><code class="hljs xml"><code class="hljs java"><code class="hljs cs">所以分页查询还是很简单的。<br>
希望给大家带来一点帮助。

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics











The main role of MySQL in web applications is to store and manage data. 1.MySQL efficiently processes user information, product catalogs, transaction records and other data. 2. Through SQL query, developers can extract information from the database to generate dynamic content. 3.MySQL works based on the client-server model to ensure acceptable query speed.

InnoDB uses redologs and undologs to ensure data consistency and reliability. 1.redologs record data page modification to ensure crash recovery and transaction persistence. 2.undologs records the original data value and supports transaction rollback and MVCC.

MySQL is an open source relational database management system, mainly used to store and retrieve data quickly and reliably. Its working principle includes client requests, query resolution, execution of queries and return results. Examples of usage include creating tables, inserting and querying data, and advanced features such as JOIN operations. Common errors involve SQL syntax, data types, and permissions, and optimization suggestions include the use of indexes, optimized queries, and partitioning of tables.

MySQL's position in databases and programming is very important. It is an open source relational database management system that is widely used in various application scenarios. 1) MySQL provides efficient data storage, organization and retrieval functions, supporting Web, mobile and enterprise-level systems. 2) It uses a client-server architecture, supports multiple storage engines and index optimization. 3) Basic usages include creating tables and inserting data, and advanced usages involve multi-table JOINs and complex queries. 4) Frequently asked questions such as SQL syntax errors and performance issues can be debugged through the EXPLAIN command and slow query log. 5) Performance optimization methods include rational use of indexes, optimized query and use of caches. Best practices include using transactions and PreparedStatemen

MySQL is chosen for its performance, reliability, ease of use, and community support. 1.MySQL provides efficient data storage and retrieval functions, supporting multiple data types and advanced query operations. 2. Adopt client-server architecture and multiple storage engines to support transaction and query optimization. 3. Easy to use, supports a variety of operating systems and programming languages. 4. Have strong community support and provide rich resources and solutions.

Compared with other programming languages, MySQL is mainly used to store and manage data, while other languages such as Python, Java, and C are used for logical processing and application development. MySQL is known for its high performance, scalability and cross-platform support, suitable for data management needs, while other languages have advantages in their respective fields such as data analytics, enterprise applications, and system programming.

MySQL index cardinality has a significant impact on query performance: 1. High cardinality index can more effectively narrow the data range and improve query efficiency; 2. Low cardinality index may lead to full table scanning and reduce query performance; 3. In joint index, high cardinality sequences should be placed in front to optimize query.

MySQL is suitable for small and large enterprises. 1) Small businesses can use MySQL for basic data management, such as storing customer information. 2) Large enterprises can use MySQL to process massive data and complex business logic to optimize query performance and transaction processing.
