package org.cdaccp.em.pager;
import java.io.Serializable;
import java.util.List;
/**分页信息类
* 封装分页条件和结果
* */
public class PageInfo<T> implements Serializable {
/**
*
*/
private static final long serialVersionUID = -4026351129192551762L;
/*条件部分*/
private Class<T> 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 java.util.List<Fetch> fetchs = new java.util.ArrayList<Fetch>();
public java.util.List<Fetch> getFetchs() {
return fetchs;
}
public void setFetchs(java.util.List<Fetch> fetchs) {
this.fetchs = fetchs;
}
/*分页结果部分*/
private int recordCount;
private int pageCount;
private java.util.List<T> result;
public Class<T> getClasszz() {
return classzz;
}
public void setClasszz(Class<T> classzz) {
this.classzz = classzz;
}
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;
}
}
=====================================================================================================
package org.cdaccp.em.pager;
import java.io.Serializable;
/**筛选条件类*/
public class Condition implements Serializable {
private String propertyName;
//比较符号 Compare是个枚举
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.cdaccp.em.pager;
/**条件比较操作符【时间关系没有封装完】*/
public enum Compare {
EQ,
GT,
LT,
GE,
LE,
NE,
LIKE
}
=======================================================================================================
package org.cdaccp.em.pager;
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.cdaccp.em.pager;
/**排序方式*/
public enum Direct {
ASC,
DESC
}
==================================================================================================
package org.cdaccp.em.pager;
import java.io.Serializable;
/**多表连接抓取策略类*/
public class Fetch implements Serializable {
/**属性名*/
private String fetchPropertyName;
/**别名*/
private String aliasName;
/**抓取方式*/
private FetchMode fetchMode = FetchMode.INNER_JOIN;
public FetchMode getFetchMode() {
return fetchMode;
}
public void setFetchMode(FetchMode fetchMode) {
this.fetchMode = fetchMode;
}
public String getFetchPropertyName() {
return fetchPropertyName;
}
public void setFetchPropertyName(String fetchPropertyName) {
this.fetchPropertyName = fetchPropertyName;
}
public String getAliasName() {
return aliasName;
}
public void setAliasName(String aliasName) {
this.aliasName = aliasName;
}
public Fetch(String fetchPropertyName, String aliasName) {
super();
this.fetchPropertyName = fetchPropertyName;
this.aliasName = aliasName;
}
public Fetch() {
super();
}
public Fetch(String fetchPropertyName, String aliasName, FetchMode fetchMode) {
super();
this.fetchPropertyName = fetchPropertyName;
this.aliasName = aliasName;
this.fetchMode = fetchMode;
}
}
========================================================================================
package org.cdaccp.em.pager;
/**抓取模式*/
public enum FetchMode {
/**内连接*/
INNER_JOIN,
/**左外连接*/
LEFT_JOIN
}
==================================hibernate session 回调-==============================================
package org.cdaccp.em.dao;
import org.hibernate.Session;
public interface IHibernateCallback {
public Object doInHibernate(Session session);
}
===================================公共通用方法 分页也在============================================
package org.cdaccp.em.dao;
import java.io.Serializable;
import java.sql.SQLException;
import java.util.List;
import javax.annotation.Resource;
import org.cdaccp.em.pager.*;
import org.hibernate.Criteria;
import org.hibernate.FetchMode;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.criterion.CriteriaSpecification;
import org.hibernate.criterion.MatchMode;
import org.hibernate.criterion.Projection;
import org.hibernate.criterion.ProjectionList;
import org.hibernate.criterion.Projections;
import org.hibernate.criterion.Restrictions;
import org.hibernate.transform.ResultTransformer;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.orm.hibernate3.HibernateTemplate;
import org.springframework.stereotype.*;
/**
* @author jt
* @author 63t06
* @version 1.0
* */
@Repository("commonDao")
@SuppressWarnings("unchecked")
public class CommonDao<T extends Serializable> {
@Resource(name = "hibernateTemplate")
private HibernateTemplate hibernateTemplate;
public void setHibernateTemplate(HibernateTemplate hibernateTemplate) {
this.hibernateTemplate = hibernateTemplate;
}
public HibernateTemplate getHibernateTemplate() {
return hibernateTemplate;
}
@Deprecated
public Session getSession(){
return hibernateTemplate.getSessionFactory().openSession();
}
public CommonDao() {
// TODO Auto-generated constructor stub
}
/**分页方法*/
public void pager(final PageInfo pi){
if (pi==null || pi.getClasszz()==null){
throw new RuntimeException("分页基本条件不全");
}
hibernateTemplate.execute(new HibernateCallback() {
@Override
public Object doInHibernate(Session session) {
Criteria qbc = session.createCriteria(pi.getClasszz());
//0.处理抓取策略
prepareFetch(qbc,pi.getFetchs());
//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);
qbc.setResultTransformer(CriteriaSpecification.DISTINCT_ROOT_ENTITY);
//5.处理排序
prepareOrder(qbc,((List<Order>)pi.getOrders()).toArray(new Order[]{}));
//6.获得分页结果
pi.setResult(
qbc
.setFirstResult((pi.getPageIndex()-1)*pi.getPageSize())
.setMaxResults(pi.getPageSize())
.list()
);
return null;
}
});
}
/**处理分页条件*/
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())
);
}
}
/**处理抓取策略*/
private void prepareFetch(Criteria qbc,java.util.List<Fetch> fetchList){
if (fetchList==null)
return;
for (Fetch fetch : fetchList) {
qbc.setFetchMode(fetch.getFetchPropertyName(), FetchMode.JOIN);
if (null!=fetch.getAliasName() && !"".equals(fetch.getAliasName())){
qbc.createAlias(
fetch.getFetchPropertyName(),
fetch.getAliasName(),
fetch.getFetchMode()==org.cdaccp.em.pager.FetchMode.INNER_JOIN?CriteriaSpecification.INNER_JOIN:CriteriaSpecification.LEFT_JOIN
);
}
}
}
/**多条件查询*/
public List<T> findByProperties(final Class classzz,final Condition...conditions){
if (conditions==null || conditions.length==0){
return findAll(classzz);
}
return
(List<T>)hibernateTemplate.execute(new HibernateCallback() {
@Override
public Object doInHibernate(Session session) throws HibernateException,
SQLException {
Criteria qbc =session.createCriteria(classzz);
prepareCondition(qbc,conditions);
return qbc.list();
}
});
}
/**保存一个临时对象[用户]*/
public void persist(T obj) {
hibernateTemplate.save(obj);
}
public void remove(T obj) {
hibernateTemplate.delete(obj);
}
public void update(T obj) {
hibernateTemplate.update(obj);
}
public T getObject(Class classzz,Serializable oid){
return (T)hibernateTemplate.get(classzz, oid);
}
public List findAll(Class classzz) {
// TODO Auto-generated method stub
return hibernateTemplate.loadAll(classzz);
}
}