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框架中的分頁查詢框架
- 【記錄】SSH分頁查詢功能
- SSH整合實現分頁查詢(兩種方式)
- 使用SSH完成條件及分頁查詢的主要程式碼
- MySQL的分頁查詢MySql
- Oracle的分頁查詢Oracle
- Lucene的分頁查詢
- Elasticsearch 分頁查詢Elasticsearch
- oracle分頁查詢Oracle
- java-用SSH框架實現將查詢的資訊分頁顯示Java框架
- 分庫分表後的分頁查詢
- ThinkPhp框架:分頁查詢PHP框架
- 分頁查詢優化優化
- NET 集合分頁查詢
- MySQL 多表查詢分頁MySql
- 分頁查詢重構
- Oracle分頁查詢格式Oracle
- 分頁查詢的排序問題排序
- SSH:查詢
- hibernate 通用分頁,查詢分頁的泛型類泛型
- javaee導測(ssh框架)分頁 mysql資料庫(帶條件查詢)Java框架MySql資料庫
- elasticsearch查詢之大資料集分頁查詢Elasticsearch大資料
- ssh的分頁操作
- indexdb實現分頁查詢Index
- AntDesignBlazor示例——分頁查詢Blazor
- MySQL分頁查詢優化MySql優化
- Oracle分頁查詢格式(十三)Oracle
- Oracle分頁查詢格式(十二)Oracle
- Oracle分頁查詢格式(十一)Oracle
- Oracle分頁查詢格式(八)Oracle
- Oracle分頁查詢格式(九)Oracle
- Oracle分頁查詢格式(十)Oracle
- oracle分頁查詢語句Oracle
- Oracle分頁查詢之order byOracle
- oracle sql分頁查詢(一)OracleSQL
- 分頁查詢初接觸