infoAction.java
import java.util.List;
import com.sise.dssystem.vo.PageBean;
import Information;
import https://www.wendangku.net/doc/4c4568972.html,Server;
public class InfoAction{
private InfoServer ifs;//实际上这个ifs也是交给了spring管理的,所以大家要配置,不然会报错的。
private PageBean pb;
private int pagenum;
private List
public List
return infos;
}
public void setInfos(List
https://www.wendangku.net/doc/4c4568972.html,s = infos;
}
public InfoServer getIfs() {
return ifs;
}
public void setIfs(InfoServer ifs) {
this.ifs = ifs;
}
public PageBean getPb() {
return pb;
}
public void setPb(PageBean pb) {
this.pb = pb;
}
public int getPagenum() {
return pagenum;
}
public void setPagenum(int pagenum) {
this.pagenum = pagenum;
}
public String FindAllFoodInPage() throws Exception{
try {
if(pagenum==0){
pagenum = 1;
}
this.pbb = usm.findColPage(6, pagenum, uid);;//6代表分页的个数,pagenum代表是哪一页,是页面来得值,uid也是页面传的值,供大家动态分页属性用,很简单吧。
infos = pb.getList();
} catch (Exception e) {
return "failure";
}
return "result";
}
}
Information.java
自己随便写些参数咯。这是一个javaBean,交给spring管理的
InformationDao.java
import java.util.List;
import https://www.wendangku.net/doc/4c4568972.html,rmation;
public interface InformationDao {
//分页
public List
public int getInfosByCollect(int id)
}
InformationDaoImpl.java
import org.hibernate.Query;
import org.hibernate.Session;
import https://www.wendangku.net/doc/4c4568972.html,rmationDao;
import https://www.wendangku.net/doc/4c4568972.html,rmation;
import com.sise.dssystem.util.HibernateSessionUtil;
public class InformationDaoImpl implements InformationDao{
//需要导入Information 的javaBean
//收藏总数
public int getInfosByCollect(int id){
return getHibernateTemplate().find("from Information where uid=?", id).size();//这里的搜索条件自己加就行了,我只是用了uid
}
//查询特定用户的收藏数据并且分页,根据id搜索到的信息,其他属性javabean有说public List
Session session = null;
List
try {
session = HibernateSessionUtil.getSession();
Query query = session.createQuery("from Information where uid=?");
query.setParameter(0, id);
query.setFirstResult((currentPageNo - 1) * pageSize); // 设置开始行数
query.setMaxResults(pageSize); // 设置每页大小
list = query.list();//这里每一行都是一个1维数组
} catch (Exception e) {
e.printStackTrace();
throw e;
}
return list;
}
}
InfoServer.java
InfoServerImpl.java
import java.util.ArrayList;
import java.util.List;
//还有导入information JavaBean
import InfoServer;
public class InfoServer;Impl implements InfoServer{
private InformationDao infoDao;
public InformationDao getInfoDao() {
r eturn infoDao;
}
public void setInfoDao(InformationDao infoDao) {
t https://www.wendangku.net/doc/4c4568972.html,Dao = infoDao;
}
//分页的收藏信息
public PageBean findColPage(int pageSize, int currentPage,int id)throws Exception {
int allRow = infoDao.getInfosByCollect(id); //总记录数
int totalPage = PageBean.countTotalPage(pageSize, allRow);
PageBean pb = new PageBean();
pb.setList(infoDao.getInfosByCollectInPage(pageSize, currentPage, id));//调用Dao层的方法
pb.setPageSize(pageSize);//每页个数
pb.setCurrentPage(currentPage);//当前页
pb.setAllRow(allRow);//总数
pb.setTotalPage(totalPage);
pb.init();
return pb;
}
}
PageBean.java
package com.sise.dssystem.vo;
import java.util.List;
import Information;//注意这里的
pagebean是供jsp页面使用
以下信息你要改的是你需要为哪个bean分页就填写哪一个bean
public class PageBean {
private List
private int allRow; //总记录数
private int totalPage; //总页数
private int currentPage; //当前页
private int pageSize; //每页记录数
private boolean isFirstPage; //是否为第一页
private boolean isLastPage; //是否为最后一页
private boolean hasPreviousPage; //是否有前一页
private boolean hasNextPage; //是否有下一页
public List
r eturn list;
}
public void setList(List
t his.list = list;
}
public int getAllRow() {
return allRow;
}
public void setAllRow(int allRow) {
this.allRow = allRow;
}
public int getTotalPage() {
return totalPage;
}
public void setTotalPage(int totalPage) {
this.totalPage = totalPage;
}
public int getCurrentPage() {
return currentPage;
}
public void setCurrentPage(int currentPage) {
this.currentPage = currentPage;
}
public int getPageSize() {
return pageSize;
}
public void setPageSize(int pageSize) {
this.pageSize = pageSize;
}
/** *//**
* 初始化分页信息
*/
public void init(){
this.isFirstPage = isFirstPage();
this.isLastPage = isLastPage();
this.hasPreviousPage = isHasPreviousPage();
this.hasNextPage = isHasNextPage();
}
/** *//**
* 以下判断页的信息,只需getter方法(is方法)即可
* @return
*/
public boolean isFirstPage() {
return currentPage == 1; // 如是当前页是第1页
}
public boolean isLastPage() {
return currentPage == totalPage; //如果当前页是最后一页
}
public boolean isHasPreviousPage() {
return currentPage != 1; //只要当前页不是第1页
}
public boolean isHasNextPage() {
return currentPage != totalPage; //只要当前页不是最后1页
}
/** *//**
* 计算总页数,静态方法,供外部直接通过类名调用
* @param pageSize 每页记录数
* @param allRow 总记录数
* @return 总页数
*/
public static int countTotalPage(final int pageSize,final int allRow){
int totalPage = allRow % pageSize == 0 ? allRow/pageSize : allRow/pageSize+1;
return totalPage;
}
/** *//**
* 计算当前页开始记录
* @param pageSize 每页记录数
* @param currentPage 当前第几页
* @return 当前页开始记录号
*/
public static int countOffset(final int pageSize,final int currentPage){
final int offset = pageSize*(currentPage-1);
return offset;
}
/** *//**
* 计算当前页,若为0或者请求的URL中没有"?page=",则用1代替
* @param page 传入的参数(可能为空,即0,则返回1)
* @return 当前页
*/
public static int countCurrentPage(int page){
final int curPage = (page==0?1:page);
return curPage;
}
}
jsp页面.txt
//迭代输出infos的值
//分页
共
当前第
第一页上一页