mysql分页:
select * from table limit, offset
分页使用了bootstrap的分页。
首先,我做了一个生成html的类,学习嘛,自己搞,当然也有写的不好的地方,等以后有提高了,再回头改改。
package com.don.utils;
import java.util.Iterator;
import java.util.Map;
public class HtmlTag {
/**
* html常见标签: div, p, ul, li, a, img, form, label, input, select,radio, checkbox
* html 常见通用属性:class, style(这里不考虑),data-xxx
* html 专属属性: href->a, src->img,video(这里不考虑),(action,method)->form(不考虑),
* (type, name, placehloder, readonly)->input, check->(radio, checkbox), selected->select
*
*/
/**
* 普通标签
* @param tagName
* @param tagContent
* @param options
* @return
*/
public String contentTag(String tagName, String tagContent, Map<String, String> options)
{
String htmlStart = makeStart(tagName, options);
String htmlEnd = makeDoubleEnd(tagName, tagContent);
return htmlStart.concat(htmlEnd);
}
/**
* link标签
* @param href
* @param tagContent
* @param options
* @return
*/
public String linkTag(String href, String tagContent, Map<String, String> options)
{
String htmlStart = makeStart("a", options);
String htmlHref = makeKeyValue("href", href);
String htmlEnd = makeDoubleEnd("a", tagContent);
return htmlStart.concat(htmlHref).concat(htmlEnd);
}
/**
* img标签
* @param src
* @param options
* @return
*/
public String imgTag(String src, Map<String, String> options)
{
String htmlStart = makeStart("img", options);
String htmlSrc = makeKeyValue("src", src);
String htmlEnd = makeSingleEnd();
return htmlStart.concat(htmlSrc).concat(htmlEnd);
}
/**
* input标签, radio和checkbox除外
* @param type
* @param name
* @param value
* @param options
* @return
*/
public String inputTag(String type, String name, String value, Map<String, String> options)
{
String htmlStart = makeStart("input", options);
String htmlType = makeKeyValue("type", type);
String htmlName = makeKeyValue("name", name);
String htmlValue = makeKeyValue("value", value);
String htmlEnd = makeSingleEnd();
return htmlStart.concat(htmlName).concat(htmlValue).concat(htmlEnd);
}
/**
* 处理键值对为字符串
* @param key
* @param value
* @return
*/
public String makeKeyValue(String key, String value)
{
StringBuilder keyValueStr = new StringBuilder();
keyValueStr.append(" ").append(key).append("=").append("\"").append(value).append("\"");
return keyValueStr.toString();
}
/**
* 处理标签属性,如class, data-xxx
* @param options
* @return
*/
private String makeOptions(Map<String, String> options)
{
if (options.size() > 0) {
StringBuilder optionStr = new StringBuilder();
for(Map.Entry<String, String> option:options.entrySet()) {
String attributeName = option.getKey();
String attributeValue = option.getValue();
optionStr.append(makeKeyValue(attributeName, attributeValue));
}
return optionStr.toString();
}
return null;
}
/**
* 标签开始, 如<a href="" data-xxx=""
* @param tagName
* @param options
* @return
*/
public String makeStart(String tagName, Map<String, String> options)
{
StringBuilder htmlStart = new StringBuilder();
htmlStart.append("<").append(tagName);
if (options.size() > 0) {
String optionStr = makeOptions(options);
htmlStart.append(optionStr);
}
return htmlStart.toString();
}
/**
* 标签闭合, 如 >连接</a>
* @param tagName
* @param tagContent
* @return
*/
public String makeDoubleEnd(String tagName, String tagContent)
{
StringBuilder htmlEnd = new StringBuilder();
htmlEnd.append(">").append(tagContent).append("</").append(tagName).append(">");
return htmlEnd.toString();
}
/**
* 单闭合
* @return
*/
public String makeSingleEnd()
{
return "/>";
}
}
然后就是分页
首先需要计算limit
limit = (page-1)* perPage
还要计算总页数
count = (totalCount + perPage - 1) / perPage
package com.don.utils;
import java.util.HashMap;
import java.util.Map;
public class Pagination {
/**
* <ul class="pagination">
* <li class="page-item"><a class="page-link" href="#">«</a></li>
* <li class="page-item"><a class="page-link" href="#">1</a></li>
* <li class="page-item"><a class="page-link" href="#">2</a></li>
* <li class="page-item active"><a class="page-link" href="#">3</a></li>
* <li class="page-item"><a class="page-link" href="#">4</a></li>
* <li class="page-item"><a class="page-link" href="#">5</a></li>
* <li class="page-item"><a class="page-link" href="#">»</a></li>
* </ul>
*/
private String urlPath = "";
private String queryString;
private int pageCount = 0;
private int perPage = 20; // 每页显示多少条, 默认20条
private String firstButtonText = "首页";
private String prevButtonText = "上一页";
private String nextButtonText = "下一页";
private String lastButtonText = "尾页";
public Pagination(String urlPath, String queryString)
{
this.urlPath = urlPath;
this.queryString = queryString;
}
/**
* 构造分页html
* @param page
* @return
*/
public String makePagination(int page, int totalCount)
{
if (totalCount == 0) {
return null;
}
// 如果只有一页就不显示分页
if (this.perPage > totalCount) {
return null;
}
// 处理url参数
makeUrl(page);
// 设置分页页数
setPageCount(totalCount);
StringBuilder pagination = new StringBuilder();
String firstItem = firstPaginationItem();
String prevItem = prevPaginationItem(page);
String rangeItems = rangePatinationItem(page);
String nextItem = nextPaginationItem(page);
String lastItem = lastPaginationItem();
pagination.append("<ul class=\"pagination\">");
pagination.append(firstItem);
if (prevItem != null) {
pagination.append(prevItem);
}
if (rangeItems != null) {
pagination.append(rangeItems);
}
if (nextItem != null) {
pagination.append(nextItem);
}
pagination.append(lastItem);
pagination.append("</ul>");
return pagination.toString();
}
/**
* 获取mysql的limit
* 如果要调用setPerPage方法,该方法一定要在setPerPage之后
* @param page
* @return
*/
public int getLimitNum(int page)
{
// (page-1)* perPage
if (this.perPage > 0) {
return (page - 1) * this.perPage;
}
return 0;
}
public void setPerPage(int perPage) {
if (perPage > 0) {
this.perPage = perPage;
}
}
public void setFirstButtonText(String firstButtonText) {
if (firstButtonText != null && firstButtonText.length() > 0) {
this.firstButtonText = firstButtonText;
}
}
public void setPrevButtonText(String prevButtonText) {
if (prevButtonText != null && prevButtonText.length() > 0) {
this.prevButtonText = prevButtonText;
}
}
public void setNextButtonText(String nextButtonText) {
if (nextButtonText != null && nextButtonText.length() > 0) {
this.nextButtonText = nextButtonText;
}
}
public void setLastButtonText(String lastButtonText) {
if (lastButtonText != null && lastButtonText.length() > 0) {
this.lastButtonText = lastButtonText;
}
}
/**
* 分页数量计算
*/
private void setPageCount(int totalCount)
{
// (totalCount + perPage - 1) / perPage;
if (this.perPage > 0) {
this.pageCount = (totalCount + this.perPage - 1) / this.perPage;
}
}
/**
* 构造a标签
* @param page
* @param text
* @return
*/
private String makePaginationLink(int page, String text)
{
if (this.urlPath != "") {
HtmlTag htmlTag = new HtmlTag();
Map<String, String> options = new HashMap<String, String>();
options.put("class", "page-link");
String linkUrl = makeUrl(page);
return htmlTag.linkTag(linkUrl, text, options);
}
return null;
}
/**
* 构造li标签
* @param page
* @param text
* @param isCurrentPage
* @return
*/
private String makePaginationItem(int page, String text, boolean isCurrentPage)
{
HtmlTag htmlTag = new HtmlTag();
Map<String, String> options = new HashMap<String, String>();
if (isCurrentPage) {
options.put("class", "page-item active");
} else {
options.put("class", "page-item");
}
String content = makePaginationLink(page, text);
return htmlTag.contentTag("li", content, options);
}
/**
* 构造首页
* @return
*/
private String firstPaginationItem()
{
return makePaginationItem(1, this.firstButtonText, false);
}
/**
* 构造上一页
* @param page
* @return
*/
private String prevPaginationItem(int page)
{
int prevPage = page - 1;
if (prevPage > 0) {
return makePaginationItem(prevPage, this.prevButtonText, false);
}
return null;
}
/**
* 构造下一页
* @param page
* @return
*/
private String nextPaginationItem(int page)
{
int nextPage = page + 1;
if (nextPage <= this.pageCount) {
return makePaginationItem(nextPage, this.nextButtonText, false);
}
return null;
}
/**
* 构造尾页
* @return
*/
private String lastPaginationItem()
{
return makePaginationItem(this.pageCount, this.lastButtonText, false);
}
/**
* 构造数字分页
* @param page
* @return
*/
private String rangePatinationItem(int page)
{
int numMin = makeRangeMin(page);
int numMax = makeRangeMax(page);
StringBuilder rangeItems = new StringBuilder();
for(int i = numMin; i <= numMax; i++) {
boolean isCurrentPage = page == i ? true : false;
rangeItems.append(makePaginationItem(i, String.valueOf(i), isCurrentPage));
}
return rangeItems.toString();
}
/**
* 获取数字分页最小值
* @param page
* @return
*/
private int makeRangeMin(int page)
{
int min = 1;
if (this.pageCount < 10) {
return min;
}
int minNum = page - 5;
if (minNum > 0) {
min = minNum;
}
return min;
}
/**
* 获取数字分页最大值
* @param page
* @return
*/
private int makeRangeMax(int page)
{
int max = this.pageCount;
if (this.pageCount < 10) {
return max;
}
int maxNum = page + 5;
if (maxNum < this.pageCount) {
max = maxNum;
}
return max;
}
/**
* 处理url
* @return
*/
private String makeUrl(int page)
{
// 处理url, 如果url带有page参数,则需要置换page参数
if (this.queryString != null && this.queryString.length() > 0){
// 处理page参数
String[] urlParameters = this.queryString.split("&");
String[] newUrlParameters = new String[urlParameters.length];
int i = 0;
for(String param:urlParameters) {
if (i < urlParameters.length) {
if (param.indexOf("page") == -1) {
newUrlParameters[i] = param;
} else {
newUrlParameters[i] = "page=".concat(String.valueOf(page));
}
}
i++;
}
String newQueryString = String.join("&", newUrlParameters);
return this.urlPath.concat("?").concat(newQueryString);
} else {
return this.urlPath.concat("?page=").concat(String.valueOf(page));
}
}
}
使用
String urlPath = request.getServletPath();
String queryString = request.getQueryString();
Pagination pagination = new Pagination(urlPath, queryString);
pagination.setPerPage(4);
pagination.setFirstButtonText("prev");
int pageLimit = pagination.getLimitNum(page);
List<Article> articles = articleService.getPageList(pageLimit, 4);
int count = articleService.getPageCount();
String htmlPagination = pagination.makePagination(page, count);