1.page类
public class Page implements Serializable {
public static final int DEFAULT_PAGE_SIZE = 10;
/** page record size */
private int pageSize = DEFAULT_PAGE_SIZE;
/** totoal record count */
private int recordCount;
/** total page */
private int totalPage;
/** current page */
private int currentPage = 1;
public int getCurrentPage() {
return currentPage < 1 ? 1 : currentPage;
}
public void setCurrentPage(int currentPage) {
this.currentPage = currentPage;
}
public int getPageSize() {
return pageSize;
}
public void setPageSize(int pageSize) {
this.pageSize = pageSize;
}
public int getTotalPage() {
return totalPage;
}
public void setTotalPage(int totalPage) {
this.totalPage = totalPage;
}
public int getRecordCount() {
return recordCount;
}
public void setRecordCount(int recordCount) {
this.recordCount = recordCount;
this.totalPage = (recordCount % getPageSize()) > 0 ? recordCount / getPageSize() + 1 : recordCount
/ getPageSize();
this.currentPage = (this.totalPage > this.currentPage || this.totalPage == 0) ? this.currentPage
: this.totalPage;
}
public boolean isLast() {
return getCurrentPage() == totalPage;
}
public boolean isFirst() {
return getCurrentPage() == 1;
}
public int getNextPage() {
return getCurrentPage() + 1;
}
public int getPrePage() {
return getCurrentPage() - 1;
}
public int[] iterate() {
int[] result = new int[totalPage];
for (int i = 1; i <= totalPage; i++) {
result[i - 1] = i;
}
return result;
}
}
2.
<pre name="code" class="java">private List find(final Page page, final Map<String,Object> params,final String sql) {
return getHibernateTemplate().executeFind(new HibernateCallback() {
public Object doInHibernate(Session session) throws HibernateException, SQLException {
Query query = getSession().createQuery(sql);
query.setReadOnly(true);
if (page != null && page.getPageSize() > 0) {
int start = (page.getCurrentPage() - 1) * page.getPageSize();
if (start >= page.getRecordCount())
start = page.getRecordCount() - 1;
if (start < 0)
start = 0;
query.setFirstResult(start);
query.setMaxResults(page.getPageSize());
}
if (params != null && params.size() > 0) {
Set<String> keySet = params.keySet();
for (String string : keySet) {
Object obj = params.get(string);
if(obj instanceof Collection<?>){
query.setParameterList(string, (Collection<?>)obj);
}else if(obj instanceof Object[]){
query.setParameterList(string, (Object[])obj);
}else{
query.setParameter(string, obj);
}
}
}
return query.list();
}
});
}
private int count(Page page, final Map<String,Object> params, final String sql) {
Object count = getHibernateTemplate().execute(new HibernateCallback() {
public Object doInHibernate(Session session) throws HibernateException, SQLException {
Query query = getSession().createQuery(sql);
query.setReadOnly(true);
if (params != null && params.size() > 0) {
Set<String> keySet = params.keySet();
for (String string : keySet) {
Object obj = params.get(string);
if(obj instanceof Collection<?>){
query.setParameterList(string, (Collection<?>)obj);
}else if(obj instanceof Object[]){
query.setParameterList(string, (Object[])obj);
}else{
query.setParameter(string, obj);
}
}
}
return query.uniqueResult();
}
});
int cnt = 0;
if (count != null)
cnt = Integer.parseInt(count.toString());
updatePage(page, cnt);
return cnt;
}
3.
public List<User> getUser(List<String> ids,Page page){
String hql = "from User where 1=1 ";
Map<String,Object> params=new HashMap<String, Object>();
if ( ids!=null && ids.size()>0) {
hql += " and id in (:ids) ";
params.put("ids",ids);
}
if (page != null) {
int count = count(page,params,"select count(*)"+hql);
if (count == 0) {
page.setCurrentPage(1);
return new ArrayList();
}
page.setRecordCount(count);
}
return find(page,params,hql);
}
}
</pre><pre name="code" class="java">