--------------------------------------------------
package org.hbm.cinema.page;
//比较符 ()
public enum Compare {
EQ,
GT,
LT,
GE,
LE,
NE,
LIKE
}
--------------------------------------------------
package org.hbm.cinema.page;
//排序方向
public enum Direct {
ASC,
DESC
}
--------------------------------------------------
package org.hbm.cinema.page;
import java.io.Serializable;
//条件
public class Condition implements Serializable {
private String propertyName; // 属性
private Compare cp = Compare.EQ; //比较呼
private Object propertyValue; // 属性值
public String getPropertyName() {
return propertyName;
}
public void setPropertyName(String propertyName) {
this.propertyName = propertyName;
}
public Compare getCp() {
return cp;
}
public void setCp(Compare cp) {
this.cp = cp;
}
public Object getPropertyValue() {
return propertyValue;
}
public void setPropertyValue(Object propertyValue) {
this.propertyValue = propertyValue;
}
public Condition(String propertyName, Compare cp, Object propertyValue) {
super();
this.propertyName = propertyName;
this.cp = cp;
this.propertyValue = propertyValue;
}
public Condition() {
super();
}
}
--------------------------------------------------
package org.hbm.cinema.page;
import java.io.Serializable;
//排序
public class Order implements Serializable {
private String propertyName; //按那个进行排序
private Direct direct = Direct.ASC; //排序方向
public String getPropertyName() {
return propertyName;
}
public void setPropertyName(String propertyName) {
this.propertyName = propertyName;
}
public Direct getDirect() {
return direct;
}
public void setDirect(Direct direct) {
this.direct = direct;
}
public Order(String propertyName, Direct direct) {
super();
this.propertyName = propertyName;
this.direct = direct;
}
public Order() {
super();
}
public Order(String propertyName) {
super();
this.propertyName = propertyName;
}
}
--------------------------------------------------
package org.hbm.cinema.page;
import java.io.Serializable;
import java.util.List;
//泛型分页信息类
public class PageInfo<T> implements Serializable {
/**
*
*/
private static final long serialVersionUID = -4026351129192551762L;
private Class<T> classzz;
public Class<T> getClasszz() {
return classzz;
}
public void setClasszz(Class<T> classzz) {
this.classzz = classzz;
}
/*条件部分*/
private int pageSize = 10; //页大小
private int pageIndex = 1; //页号
private java.util.List<Condition> conditions = new java.util.ArrayList<Condition>();
private java.util.List<Order> orders = new java.util.ArrayList<Order>();;
/*分页结果部分*/
private int recordCount;
private int pageCount;
private java.util.List<T> result;
public int getPageSize() {
return pageSize;
}
public void setPageSize(int pageSize) {
this.pageSize = pageSize;
}
public int getPageIndex() {
return pageIndex;
}
public void setPageIndex(int pageIndex) {
this.pageIndex = pageIndex;
}
public java.util.List<Condition> getConditions() {
return conditions;
}
public void setConditions(java.util.List<Condition> conditions) {
this.conditions = conditions;
}
public java.util.List<Order> getOrders() {
return orders;
}
public void setOrders(java.util.List<Order> orders) {
this.orders = orders;
}
public int getRecordCount() {
return recordCount;
}
public void setRecordCount(int recordCount) {
this.recordCount = recordCount;
}
public int getPageCount() {
return pageCount;
}
public void setPageCount(int pageCount) {
this.pageCount = pageCount;
}
public java.util.List<T> getResult() {
return result;
}
public void setResult(java.util.List<T> result) {
this.result = result;
}
public PageInfo(Class<T> classzz, int pageSize, int pageIndex,
List<Condition> conditions, List<Order> orders) {
super();
this.classzz = classzz;
this.pageSize = pageSize;
this.pageIndex = pageIndex;
this.conditions = conditions;
this.orders = orders;
}
public PageInfo(Class<T> classzz) {
super();
this.classzz = classzz;
}}
--------------------------------------------------
-----------------------------------------------------------------
/*
* 分页方法(包括 处理条件,排序后 的分页信息)
*基于hibernate 的对象查询,但是不适合 连接查询, (使用lazy 性能上不可取)
* */
public void pager(PageInfo<T> pi){
if (pi==null || pi.getClasszz()==null){
throw new RuntimeException("分页基本条件不全");
}
Session session = null;
try {
session = HibernateSessionFactory.getSession();
Criteria qbc = session.createCriteria(pi.getClasszz());
//1.设置条件
List<Condition> list = pi.getConditions();
//调用方法。。。
prepareCondition(qbc,list.toArray(new Condition[]{}));
//2.计算总条数
qbc.setProjection(Projections.rowCount());
pi.setRecordCount(
(Integer)qbc.uniqueResult()
);
//3.总页数
pi.setPageCount(
pi.getRecordCount()%pi.getPageSize()==0?
pi.getRecordCount()/pi.getPageSize():
pi.getRecordCount()/pi.getPageSize()+1
);
//4.清空投影查询的设置
qbc.setProjection(null);
//5.处理排序
//调用方法。。。。。。
prepareOrder(qbc,((List<Order>)pi.getOrders()).toArray(new Order[]{}));
//6.获得分页结果
pi.setResult(
qbc
.setFirstResult((pi.getPageIndex()-1)*pi.getPageSize())
.setMaxResults(pi.getPageSize())
.list()
);
} catch (HibernateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
if(session!=null)session.close();
}
}
---------------------------------------------------------------------------------------------------------
/*
* 处理条件
* */
private void prepareCondition(Criteria qbc,Condition...conditions){
if (conditions==null || conditions.length==0)
return;
for (Condition cdt : conditions) {
switch (cdt.getCp()) {
case EQ:
qbc.add(Restrictions.eq(cdt.getPropertyName(), cdt.getPropertyValue()));
break;
case GT:
qbc.add(Restrictions.gt(cdt.getPropertyName(), cdt.getPropertyValue()));
break;
case LT:
qbc.add(Restrictions.lt(cdt.getPropertyName(), cdt.getPropertyValue()));
break;
case GE:
qbc.add(Restrictions.ge(cdt.getPropertyName(), cdt.getPropertyValue()));
break;
case LE:
qbc.add(Restrictions.le(cdt.getPropertyName(), cdt.getPropertyValue()));
break;
case LIKE:
qbc.add(Restrictions.like(cdt.getPropertyName(), cdt.getPropertyValue().toString(), MatchMode.ANYWHERE));
break;
default:
break;
}
}
}
-------------------------------------------------------------------
/*处理排序*/
private void prepareOrder(Criteria qbc,Order...orders){
if (orders==null || orders.length==0)
return;
for (Order ord : orders) {
qbc.addOrder(
ord.getDirect()==Direct.ASC?
org.hibernate.criterion.Order.asc(ord.getPropertyName()):
org.hibernate.criterion.Order.desc(ord.getPropertyName())
);
}
}