本篇文章给大家带来的内容是关于javaweb如何实现分页查询?javaweb实现分页查询的代码,有一定的参考价值,有需要的朋友可以参考一下,希望对你有所帮助。
Javaweb分页技术实现
分页技术就是通过SQL语句(如下)来获取数据,具体实现看下面代码
//分页查询语句 select * from 表名 where limit page , count; 和 //获取表中的总数据,确定页数 select count(*) from 表名;

不说废话直接上代码
前端代码:
立即学习“Java免费学习笔记(深入)”;
功能简介:1.用户留言功能2.用户定货功能3.定制货货功能4.定制网页样式和其实设置(比如主页)5.强大的管理功能(现在的程序都是管理功能大于应用功能:)6.管理功能支持查看订货单,留言,分页,删除等功能管理页面:login.asp管理密码:admin
Title
后台servlet代码:
/**
* @author hh
* @Date 2018/9/12
*/
@WebServlet("/getCity")
public class PageServlet extends HttpServlet {
@Override
protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//获取当前页参数,第一次访问为空
String currPage = req.getParameter("currenPage");
// 判断,如果为空,则设置为1
if (currPage == null || "".equals(currPage.trim())) {
currPage = "1";
}
//调用service返回分页类实例
PageBean pageBean=new PageService().getPage(currPage);
//设置相应文本类型
resp.setContentType("application/json;charset=utf-8");
//响应前端
resp.getWriter().print(new Gson().toJson(pageBean));
}
} City 实体类:
package edu.nf.demo.entity;
/**
* @author hh
* @Date 2018/9/12
*/
public class City {
private String cityId;
private String cityEn;
private String cityCn;
private String countryCode;
private String countryEn;
private String countryCn;
private String provinceEn;
private String provinceCn;
public String getCityId() {
return cityId;
}
public void setCityId(String cityId) {
this.cityId = cityId;
}
public String getCityEn() {
return cityEn;
}
public void setCityEn(String cityEn) {
this.cityEn = cityEn;
}
public String getCityCn() {
return cityCn;
}
public void setCityCn(String cityCn) {
this.cityCn = cityCn;
}
public String getCountryCode() {
return countryCode;
}
public void setCountryCode(String countryCode) {
this.countryCode = countryCode;
}
public String getCountryEn() {
return countryEn;
}
public void setCountryEn(String countryEn) {
this.countryEn = countryEn;
}
public String getCountryCn() {
return countryCn;
}
public void setCountryCn(String countryCn) {
this.countryCn = countryCn;
}
public String getProvinceEn() {
return provinceEn;
}
public void setProvinceEn(String provinceEn) {
this.provinceEn = provinceEn;
}
public String getProvinceCn() {
return provinceCn;
}
public void setProvinceCn(String provinceCn) {
this.provinceCn = provinceCn;
}
}自己写的一个类,专门用于分页查询用的:
package edu.nf.demo.entity; import java.util.List; /** * @author hh * @Date 2018/9/12 */ public class PageBean{ /** * 当前页, 默认显示第一页 */ private Integer currntPage = 1; /** * 查询返回的行数(每页显示的行数),默认每页显示10行 */ private int pageCount = 10; /** * 总记录数 */ private int totalCount; /** * 总页数 = 总记录数/每页显示的行数(+1) */ private int totalPage; /** * 分页查询的数据,运用泛型,可以重复利用 */ private List pageData; public int getTotalPage() { if (totalCount % pageCount == 0) { totalPage = totalCount / pageCount; } else { totalPage = totalCount / pageCount + 1; } return totalPage; } public void setTotalPage(int totalPage) { this.totalPage = totalPage; } public int getCurrntPage() { return currntPage; } public void setCurrntPage(int currntPage) { this.currntPage = currntPage; } public int getPageCount() { return pageCount; } public void setPageCount(int pageCount) { this.pageCount = pageCount; } public int getTotalCount() { return totalCount; } public void setTotalCount(int totalCount) { this.totalCount = totalCount; } public List getPageData() { return pageData; } public void setPageData(List pageData) { this.pageData = pageData; } }
后台service,逻辑业务层:
/**
* @author hh
* @Date 2018/9/12
*/
public class PageService {
public PageBean getPage(String currPage){
//类型转换 当前页数
Integer currenPage = Integer.valueOf(currPage);
//实例化分页类
PageBean pageBean = new PageBean();
//实例化CityDaoImpl类
CityDaoImpl cityDao=new CityDaoImpl();
//数据库第几行开始查询
int startPage=(currenPage-1)*pageBean.getPageCount();
//查询多少行数据 分页类里默认30行
int selectCount=pageBean.getPageCount();
//查询数据库获取分页返回的数据 : select * from regional_info limit startPage,selectCount
List list=cityDao.listCity(startPage,selectCount);
//获取总数
int cityCount=cityDao.getCityCount();
//设置查询的数据
pageBean.setPageData(list);
//共多少行
pageBean.setTotalCount(cityCount);
//设置总页数
pageBean.setTotalPage(cityCount/pageBean.getPageCount()+1);
return pageBean;
}
}










