SSH框架下的分頁查詢
系統的整理了一下分頁查詢,如果有需要lib包的可以評論留郵箱。
pojo層
package pojo;
public class Xs {
private int id;
private String xh;
private String name;
private String age;
private String sex;
private String major;
private String college;
private String introduction;
private String photo;public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getXh() {
return xh;
}
public void setXh(String xh) {
this.xh = xh;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAge() {
return age;
}
public void setAge(String age) {
this.age = age;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public String getMajor() {
return major;
}
public void setMajor(String major) {
this.major = major;
}
public String getCollege() {
return college;
}
public void setCollege(String college) {
this.college = college;
}
public String getIntroduction() {
return introduction;
}
public void setIntroduction(String introduction) {
this.introduction = introduction;
}
public String getPhoto() {
return photo;
}
public void setPhoto(String photo) {
this.photo = photo;
}
}
hbm.xml配置
<?xml version="1.0" encoding="GB18030"?>
<hibernate-mapping package="pojo">
<class name="Xs" table="student">
<id name="id" column="id">
<generator class="native">
</generator>
</id>
<property name="xh" column="xh" />
<property name="name" column="name" />
<property name="age" column="age" />
<property name="sex" column="sex" />
<property name="major" column="major" />
<property name="college" column="college" />
<property name="introduction" column="introduction" />
<property name="photo" column="photo" />
</class>
</hibernate-mapping>
dao、daoimpl層
package dao;
import java.util.List;
import pojo.Xs;public interface XsDAO {
public int findCount();
public List<Xs> findByPage(int begin, int pageSize);
}
package dao.impl;
import dao.XsDAO;
import pojo.Xs;
public class XsDAOImpl extends HibernateTemplate implements XsDAO{
List<Xs> xss;
public List<Xs> getXss() {
return xss;
}
public void setXss(List<Xs> xss) {
this.xss = xss;
}
@Override
public int findCount() {
Criteria criteria = getSession().createCriteria(Xs.class);
criteria.setProjection(Projections.rowCount());
Long count = (Long) criteria.uniqueResult();
int a=count.intValue();
System.out.println("條件個數"+a);
return a;
}
@Override
public List<Xs> findByPage(int begin, int pageSize) {
DetachedCriteria criteria = DetachedCriteria.forClass(Xs.class);
List<Xs> list = this.findByCriteria(criteria, begin, pageSize);
return list;
}
}
service,serviceimpl
package service;
import pojo.Xs;
import com.PageBean;
public interface XsService {
public PageBean<Xs> findByPage(Integer currPage);
}
package service.impl;
import com.PageBean;
import dao.XsDAO;import service.XsService;
public class XsServiceImpl implements XsService{
XsDAO xsDAO;
public XsDAO getXsDAO() {
return xsDAO;
}
public void setXsDAO(XsDAO xsDAO) {
this.xsDAO = xsDAO;
}
@Override
public PageBean<Xs> findByPage(Integer currPage) {
PageBean<Xs> pageBean = new PageBean<Xs>();
//封裝當前的頁數:
pageBean.setCurrPage(currPage);
//封裝每頁顯示記錄數
int pageSize = 5;
pageBean.setPageSize(pageSize);
//封裝總記錄數:
int totalCount =xsDAO.findCount();
pageBean.setTotalCount(totalCount);
//封裝總頁數:
double tc = totalCount;
Double num = Math.ceil(tc / pageSize);
pageBean.setTotalPage(num.intValue());
//封裝每頁顯示的資料:
int begin = (currPage - 1)*pageSize;
List<Xs> list = xsDAO.findByPage(begin,pageSize);
pageBean.setList(list);
return pageBean;
}
}
action
package action;
import service.XsService;
import com.PageBean;
public class XsAction {
XsService xsService;
public XsService getXsService() {
return xsService;
}
public void setXsService(XsService xsService) {
this.xsService = xsService;
}private Integer currPage = 1;
public void setCurrPage(Integer currPage) {
this.currPage = currPage;
}
public String find(){
PageBean<Xs> pageBean = xsService.findByPage(currPage);
//將pageBean存入到值棧中
ActionContext.getContext().getValueStack().push(pageBean);
return "findXs";
}
}
pagebean封裝頁數
package com;
import java.util.List;
/**
* 分頁Bean
*/
public class PageBean<T> {
private int currPage; //當前頁數
private int pageSize; //每頁顯示的記錄數
private int totalCount; //總記錄數
private int totalPage; //總頁數
private List<T> list; //每頁顯示的資料
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 getTotalCount() {
return totalCount;
}
public void setTotalCount(int totalCount) {
this.totalCount = totalCount;
}
public int getTotalPage() {
return totalPage;
}
public void setTotalPage(int totalPage) {
this.totalPage = totalPage;
}
public List<T> getList() {
return list;
}
public void setList(List<T> list) {
this.list = list;
}
}
相關文章
- 【記錄】SSH分頁查詢功能
- SSH整合實現分頁查詢(兩種方式)
- 使用SSH完成條件及分頁查詢的主要程式碼
- MySQL的分頁查詢MySql
- Elasticsearch 分頁查詢Elasticsearch
- 分庫分表後的分頁查詢
- NET 集合分頁查詢
- AntDesignBlazor示例——分頁查詢Blazor
- ThinkPhp框架:分頁查詢PHP框架
- 分頁查詢優化優化
- SSH:查詢
- elasticsearch查詢之大資料集分頁查詢Elasticsearch大資料
- ssh的分頁操作
- MySQL分頁查詢優化MySql優化
- indexdb實現分頁查詢Index
- MySQL——優化巢狀查詢和分頁查詢MySql優化巢狀
- Oracle總結【SQL細節、多表查詢、分組查詢、分頁】OracleSQL
- 關於 groupBy 分組查詢的分頁處理
- 菜品條件分頁查詢
- Oracle資料庫中的分頁查詢Oracle資料庫
- 使用Mybatis-plus進行分頁查詢,沒有分頁效果,查詢的資料量超出每頁數量設定MyBatis
- SSH:hiberate實現資料的查詢(單查詢和全查詢)
- 資料庫全表查詢之-分頁查詢優化資料庫優化
- 流式查詢1. mybatis的遊標Cursor,分頁大資料查詢MyBatis大資料
- OData武裝你的WEBAPI-分頁查詢WebAPI
- SSM框架實現分頁查詢例子SSM框架
- MySQL分優化之超大頁查詢MySql優化
- Hibernate5.1+Sqlserver2000分頁查詢SQLServer
- c# winform 實現分頁查詢C#ORM
- (MySQL學習筆記)分頁查詢MySql筆記
- ElasticSearch - 分頁查詢方式二 【scroll】滾動查詢(kibana、Java示例)ElasticsearchJava
- 小書MybatisPlus第4篇-表格分頁與下拉分頁查詢MyBatis
- MySQL全面瓦解9:查詢的排序、分頁相關MySql排序
- 如何優雅地實現分頁查詢
- HBase學習之二: hbase分頁查詢
- 分頁查詢及其擴充應用案例
- SpringMVC+Spring Data JPA +Bootstrap 分頁實現和模糊查詢分頁SpringMVCboot
- 百億級資料分表後怎麼分頁查詢?