package org.accp.mhouse.dao.hbimpl;
import java.io.Serializable;
import java.util.List;
import org.accp.mhouse.dao.HibernateSessionFactory;
import org.accp.mhouse.dao.IHibernateCallback;
import org.accp.mhouse.pager.Condition;
import org.accp.mhouse.pager.Direct;
import org.accp.mhouse.pager.Fetch;
import org.accp.mhouse.pager.Order;
import org.accp.mhouse.pager.PageInfo;
import org.hibernate.Criteria;
import org.hibernate.FetchMode;
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;
@SuppressWarnings("unchecked")
public class CommonDao<T extends Serializable> {
private Class<T> classzz;
public CommonDao() {
// TODO Auto-generated constructor stub
}
public CommonDao(Class<T> classzz) {
// TODO Auto-generated constructor stub
this.classzz = classzz;
}
/**分页方法*/
public void pager(final PageInfo pi){
if (pi==null || pi.getClasszz()==null){
throw new RuntimeException("分页基本条件不全");
}
execute(new IHibernateCallback() {
@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.accp.mhouse.pager.FetchMode.INNER_JOIN?CriteriaSpecification.INNER_JOIN:CriteriaSpecification.LEFT_JOIN
);
}
}
}
/**通过回调执行任何Hibernate操作*/
public Object execute(IHibernateCallback hibernateCallback){
Session session = null;
Transaction ts = null;
Object result = null;
try {
session = HibernateSessionFactory.getSession();
ts = session.beginTransaction();
if (hibernateCallback!=null)
result = hibernateCallback.doInHibernate(session);
ts.commit();
} catch(Exception e){
ts.rollback();
throw new RuntimeException(e.getMessage());
} finally {
if (session!=null) session.close();
}
return result;
}
/**多条件查询*/
public List<T> findByProperties(Condition...conditions){
if (conditions==null || conditions.length==0){
return findAll();
}
Session session = null;
try {
session = HibernateSessionFactory.getSession();
Criteria qbc = session.createCriteria(classzz);
prepareCondition(qbc,conditions);
return qbc.list();
} catch (Exception e) {
throw new RuntimeException(e.getMessage());
} finally {
if (session!=null) session.close();
}
}
/**保存一个临时对象[用户]*/
public void persist(T obj) {
// TODO Auto-generated method stub
Session session = null;
Transaction ts = null;
try {
session = HibernateSessionFactory.getSession();
ts = session.beginTransaction();
session.save(obj);
ts.commit();
} catch(Exception e){
ts.rollback();
throw new RuntimeException(e.getMessage());
} finally {
if (session!=null) session.close();
}
}
public void remove(T obj) {
// TODO Auto-generated method stub
Session session = null;
Transaction ts = null;
try {
session = HibernateSessionFactory.getSession();
ts = session.beginTransaction();
session.delete(obj);
ts.commit();
} catch(Exception e){
ts.rollback();
throw new RuntimeException(e.getMessage());
} finally {
if (session!=null) session.close();
}
}
public void update(T obj) {
// TODO Auto-generated method stub
Session session = null;
Transaction ts = null;
try {
session = HibernateSessionFactory.getSession();
ts = session.beginTransaction();
session.update(obj);
ts.commit();
} catch(Exception e){
ts.rollback();
throw new RuntimeException(e.getMessage());
} finally {
if (session!=null) session.close();
}
}
public T getObject(Serializable oid){
Session session = null;
try {
session = HibernateSessionFactory.getSession();
return (T)session.get(classzz, oid);
} catch (Exception e) {
throw new RuntimeException(e.getMessage());
} finally {
if (session!=null) session.close();
}
}
public List<T> findAll() {
// TODO Auto-generated method stub
Session session = null;
try {
session = HibernateSessionFactory.getSession();
return session.createCriteria(classzz).list();
} catch (Exception e) {
throw new RuntimeException(e.getMessage());
} finally {
if (session!=null) session.close();
}
}
}
- 1
- 2
- 3
前往页