📄 paginationdao.java
字号:
package com.yang.pagination;
import java.util.List;
import net.sf.hibernate.Query;
import net.sf.hibernate.Session;
import net.sf.hibernate.Transaction;
import com.yang.common.HibernateSessionFactory;
public class PaginationDao {
private Pagination page = null;
private String tableName = null;
private String qualification = null;
public PaginationDao(Pagination page, String tableName) {
super();
this.page = page;
this.tableName = tableName;
try {
this.setTotalRows();
} catch (Exception e) {
e.printStackTrace();
}
}
public PaginationDao(Pagination page, String tableName, String qualification) {
super();
this.page = page;
this.tableName = tableName;
this.qualification = qualification;
try {
this.setTotalRows();
} catch (Exception e) {
e.printStackTrace();
}
}
public String getQualification() {
return qualification;
}
public void setQualification(String qualification) {
this.qualification = qualification;
}
public String getTableName() {
return tableName;
}
public void setTableName(String tableName) {
this.tableName = tableName;
}
public int setTotalRows() throws Exception {
Session session = null;
Transaction ts = null;
int totalRows = 0;
try {
session = HibernateSessionFactory.getSession();
ts = session.beginTransaction();
String sql = "select count(*) from " + tableName;
Query query = session.createQuery(sql);
totalRows = Integer.parseInt(query.uniqueResult().toString());
ts.commit();
page.setTotalRows(totalRows);
} catch (Exception e) {
e.printStackTrace();
if (ts != null) {
ts.rollback();
}
} finally {
session.close();
}
return totalRows;
}
public List getDestinationPage(int destinationPage) throws Exception {
List lst = null;
Session session = null;
Transaction ts = null;
try {
if (destinationPage > page.getTotalPages() || destinationPage <= 0) {
throw new PaginationException("不能取得目标页的数据!");
}
session = HibernateSessionFactory.getSession();
ts = session.beginTransaction();
String sql = null;
if (qualification == null || "".equals(qualification)) {
sql = "select p from " + tableName + " as p";
} else {
sql = "select p from " + tableName + " as p " + qualification;
}
Query query = session.createQuery(sql);
int firstResult = (destinationPage - 1) * page.getPageSize();
query.setFirstResult(firstResult);
query.setMaxResults(page.getPageSize());
lst = query.list();
ts.commit();
page.setCurrentPage(destinationPage);
} catch (Exception e) {
e.printStackTrace();
if (ts != null) {
ts.rollback();
}
} finally {
session.close();
}
return lst;
}
public List getPreviousPage() throws Exception {
List lst = null;
int previousPage = page.getPreviousPage();
if (page.isHasPervious() == false) {
throw new PaginationException("不能取得上一页的数据!");
}
lst = this.getDestinationPage(previousPage);
return lst;
}
public List getNextPage() throws Exception {
List lst = null;
int nextPage = page.getNextPage();
if (page.isHasNext() == false) {
throw new PaginationException("不能取得下一页的数据!");
}
lst = this.getDestinationPage(nextPage);
return lst;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -