ssh的mysql分页查询_SSH分页+Mysql

结构+效果图

0818b9ca8b590ca3270a3433284dd417.png

0818b9ca8b590ca3270a3433284dd417.png

Page.java :

public class Page {

/** 是否有上一页 */

private boolean hasPrePage;

/** 是否有下一页 */

private boolean hasNextPage;

/** 每页的数量 */

private int everyPage;

/** 总页数 */

private int totalPage;

/** 当前页 */

private int currentPage;

/** 起始点 */

private int beginIndex;

/** 总记录数 */

private int totalCount;

/**

* @return totalCount

*/

public int getTotalCount() {

return totalCount;

}

/**

* @param totalCount

* 要设置的 totalCount

*/

public void setTotalCount(int totalCount) {

this.totalCount = totalCount;

}

/** The default constructor */

public Page() {

}

/**

* construct the page by everyPage

*

* @param everyPage

* */

public Page(int everyPage) {

this.everyPage = everyPage;

}

/** The whole constructor */

public Page(boolean hasPrePage, boolean hasNextPage, int everyPage,

int totalPage, int currentPage, int beginIndex, int totalCount) {

this.hasPrePage = hasPrePage;

this.hasNextPage = hasNextPage;

this.everyPage = everyPage;

this.totalPage = totalPage;

this.currentPage = currentPage;

this.beginIndex = beginIndex;

this.totalCount = totalCount;

}

/**

* @return Returns the beginIndex.

*/

public int getBeginIndex() {

return beginIndex;

}

/**

* @param beginIndex

* The beginIndex to set.

*/

public void setBeginIndex(int beginIndex) {

this.beginIndex = beginIndex;

}

/**

* @return Returns the currentPage.

*/

public int getCurrentPage() {

return currentPage;

}

/**

* @param currentPage

* The currentPage to set.

*/

public void setCurrentPage(int currentPage) {

this.currentPage = currentPage;

}

/**

* @return Returns the everyPage.

*/

public int getEveryPage() {

return everyPage;

}

/**

* @param everyPage

* The everyPage to set.

*/

public void setEveryPage(int everyPage) {

this.everyPage = everyPage;

}

/**

* @return Returns the hasNextPage.

*/

public boolean getHasNextPage() {

return hasNextPage;

}

/**

* @param hasNextPage

* The hasNextPage to set.

*/

public void setHasNextPage(boolean hasNextPage) {

this.hasNextPage = hasNextPage;

}

/**

* @return Returns the hasPrePage.

*/

public boolean getHasPrePage() {

return hasPrePage;

}

/**

* @param hasPrePage

* The hasPrePage to set.

*/

public void setHasPrePage(boolean hasPrePage) {

this.hasPrePage = hasPrePage;

}

/**

* @return Returns the totalPage.

*

*/

public int getTotalPage() {

return totalPage;

}

/**

* @param totalPage

* The totalPage to set.

*/

public void setTotalPage(int totalPage) {

this.totalPage = totalPage;

}

@Override

public String toString() {

return "Page [hasPrePage(是否有上一页)=" + hasPrePage

+ ", hasNextPage(是否有下一页)=" + hasNextPage

+ ", everyPage(每页的数量)=" + everyPage + ", totalPage(总页数)="

+ totalPage + ", currentPage(当前页)=" + currentPage

+ ", beginIndex(起始点)=" + beginIndex + ", totalCount(总记录数)="

+ totalCount + "]";

}

}

PageUtil.java

public class PageUtil {

/**

* Use the origin page to create a new page

*

* @param page

* @param totalRecords

* @return

*/

public static Page createPage(Page page, int totalRecords) {

return createPage(page.getEveryPage(), page.getCurrentPage(),

totalRecords);

}

/**

* the basic page utils not including exception handler

*

* @param everyPage

* @param currentPage

* @param totalRecords

* @return page

*/

public static Page createPage(int everyPage, int currentPage, int totalRecords) {

everyPage = getEveryPage(everyPage);

currentPage = getCurrentPage(currentPage);

int beginIndex = getBeginIndex(everyPage, currentPage);

int totalPage = getTotalPage(everyPage, totalRecords);

boolean hasNextPage = hasNextPage(currentPage, totalPage);

boolean hasPrePage = hasPrePage(currentPage);

return new Page(hasPrePage, hasNextPage, everyPage, totalPage,

currentPage, beginIndex, totalRecords);

}

private static int getEveryPage(int everyPage) {

return everyPage == 0 ? 10 : everyPage;

}

private static int getCurrentPage(int currentPage) {

return currentPage == 0 ? 1 : currentPage;

}

private static int getBeginIndex(int everyPage, int currentPage) {

return (currentPage - 1) * everyPage;

}

private static int getTotalPage(int everyPage, int totalRecords) {

int totalPage = 0;

if (totalRecords % everyPage == 0)

totalPage = totalRecords / everyPage;

else

totalPage = totalRecords / everyPage + 1;

return totalPage;

}

private static boolean hasPrePage(int currentPage) {

return currentPage == 1 ? false : true;

}

private static boolean hasNextPage(int currentPage, int totalPage) {

return currentPage == totalPage || totalPage == 0 ? false : true;

}

}

PageAction

public class PageAction extends ActionSupport {

private StudentDAO studentDao = null;

private List list = null;

private Integer count = null;

private Page page = null;

private Integer everyPage = 3;// 默认每页显示3条数据

private Integer currentPage = 1;// 默认显示第一页

public Integer getEveryPage() {

return everyPage;

}

public void setEveryPage(Integer everyPage) {

this.everyPage = everyPage;

}

public Integer getCurrentPage() {

return currentPage;

}

public void setCurrentPage(Integer currentPage) {

this.currentPage = currentPage;

}

public void setStudentDao(StudentDAO studentDao) {

this.studentDao = studentDao;

}

public List getList() {

return list;

}

public void setList(List list) {

this.list = list;

}

public Integer getCount() {

return count;

}

public void setCount(Integer count) {

this.count = count;

}

public Page getPage() {

return page;

}

public void setPage(Page page) {

this.page = page;

}

public String execute() throws Exception {

page = PageUtil.createPage(everyPage, currentPage,

studentDao.getProductCount());

System.out.println(page);

list = studentDao.getProductByPage(page);// 得到分页数据

return "success";

}

}

DaoImpl

public List getProductByPage(final Page page) {

return this.getHibernateTemplate().executeFind(new HibernateCallback() {

public Object doInHibernate(Session session) throws HibernateException, SQLException {

Query query = session.createQuery("from Student");

query.setFirstResult(page.getBeginIndex());

query.setMaxResults(page.getEveryPage());

return query.list();

}

});

}

public int getProductCount() {

List list = this.getHibernateTemplate().find("from Student");

return list.size();

}

jsp

SSH分页案例
ID姓名性别年龄学校
${student.id }${student.name }${student.sex }${student.age }${student.school }

每页显示:

3

5

8

  条

当前页数:[${page.currentPage }/${page.totalPage }]

首页

上一页

下一页

末页

总记录数:${page.totalCount }  条

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值