欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

hibernate通用方法

程序员文章站 2022-04-15 22:12:04
...


/**
* 根据sql生产制定的entityClass类型对象
* @param sql
* @param entityClass
* @param scalarList
* @param conditions
* @return
*/
public List<?> findObjectBySql(final String sql,final Class<?> entityClass,final List<HibernateQueryContion> scalarList,final HibernateQueryContion... conditions){
List<?> result = (List<?>)getHibernateTemplate().execute(new HibernateCallback() {
public Object doInHibernate(Session session) throws HibernateException, SQLException {
SQLQuery query = session.createSQLQuery(sql);

for(HibernateQueryContion condition:conditions){
Object value = condition.getValue();
String propertyName = condition.getPropertyName();
if(value instanceof Collection){
query.setParameterList(propertyName, (Collection)value);
}else{
query.setParameter(propertyName, value);
}
}
if(scalarList != null && scalarList.size() > 0){
for(HibernateQueryContion scalar:scalarList){
query.addScalar(scalar.getPropertyName(), scalar.getHiberateType());
}
query.setResultTransformer(Transformers.aliasToBean(entityClass));
}
return query.list();
}
});
return result;
}

/**
* 根据提供的HQL进行条件查询
* @param list
* @param hql
* @return
*/
public List<?> findByConditions(final List<HibernateQueryContion> list,final String hql){
List<?> result = (List<?>) getHibernateTemplate().execute(new HibernateCallback() {
public Object doInHibernate(Session session) throws HibernateException, SQLException {
Query select = session.createQuery(hql);
Iterator<HibernateQueryContion> iterator = list.iterator();
while(iterator.hasNext()){
HibernateQueryContion condition = iterator.next();
Object value = condition.getValue();
if(value instanceof Collection){
select.setParameterList(condition.getPropertyName(), (Collection)value);
}else{
select.setParameter(condition.getPropertyName(), condition.getValue());
}
}
return select.list();
}
});
return result;
}
/**
* 根据提供的HQL进行分页条件查询
* @param list
* @param hql
* @param pageSize
* @param firstResult
* @return
*/
public List<?> findByConditions(final List<HibernateQueryContion> list,final String hql,final Integer pageSize,final Integer firstResult){
List<?> result = (List<?>) getHibernateTemplate().execute(new HibernateCallback() {
public Object doInHibernate(Session session) throws HibernateException, SQLException {
Query select = session.createQuery(hql);
Iterator<HibernateQueryContion> iterator = list.iterator();
while (iterator.hasNext()) {
HibernateQueryContion condition = iterator.next();
Object value = condition.getValue();
if (value instanceof Collection) {
select.setParameterList(condition.getPropertyName(), (Collection) value);
} else {
select.setParameter(condition.getPropertyName(), condition.getValue());
}
}
select.setMaxResults(pageSize);
select.setFirstResult(firstResult);
return select.list();
}
});
return result;
}
/**
* 统计某个实例的总数
* @param entityClass
* @param countParamsName
* @return
*/
public int getCountNum(Class<?> entityClass,String countParamsName){
Criterion[] caseArray = new Criterion[0] ;
Criteria c = createCriteria(entityClass, caseArray);
c.setProjection(Projections.count(countParamsName));
Integer result = (Integer)c.uniqueResult();
return result;
}
/**
* 根据提供的条件统计总数,不能进行模糊统计
* @param entityClass
* @param countParamsName
* @param conditions
* 调用方式如下:
* <pre>
* dao.getCountNum(entityClass,countParamsName)
* dao.getCountNum(entityClass,countParamsName,arg0);
* dao.getCountNum(entityClass,countParamsName,arg0,arg1);
* dao.getCountNum(entityClass,countParamsName,new Object[arg0,arg1,arg2])
* </pre>
* @return
*/
public int getCountNum(Class<?> entityClass,String countParamsName,HibernateQueryContion... conditions){
Criteria c = createCriteria(entityClass,conditions);
c.setProjection(Projections.count(countParamsName));
Integer result = (Integer)c.uniqueResult();
return result;
}


/**
* 查询entityClass类型对象的所有记录
* @param entityClass
* @return
*/
public List<?> findObjects(Class<?> entityClass){
Criteria criteria = createCriteria(entityClass);
return criteria.list();
}
/**
* 根据条件查询对应的类属性集合
* @param entityClass
* @param propertyNames
* @param conditions
* @return
* 返回结果如下:
* List<Object[]> objArrayList;object数组中第一个记录对应
* propertyNames的第一个属性的值,
* 例如:
* String[] propertyNames = new String[4]{"id","name","age","address"};
* .....
* 返回结果中Object[]对应的:
* for(Object[] objs:objArrayList){
* ...
* idValue = objs[0];
* nameValue = objs[1];
* ...
* addressValue = objs[3];
*/
public List<?> findObjectsByCondition(Class<?> entityClass,String[] propertyNames,HibernateQueryContion... conditions){
Criteria criteria = createCriteria(entityClass,conditions);
ProjectionList proList = Projections.projectionList();
for (String propertyName : propertyNames) {
proList.add(Projections.property(propertyName));
}
criteria.setProjection(proList);
return criteria.list();
}
/**
* 根据条件查询对应的类属性集合
* @param entityClass
* @param propertyNames
* @param conditions
* @return
* 返回结果如下:
* List<T> objArrayList;
*/
public List<?> findObjectsByCondition(Class<?> entityClass,String propertyName,HibernateQueryContion... conditions){
Criteria criteria = createCriteria(entityClass, conditions);
criteria.setProjection(Projections.property(propertyName));
return criteria.list();
}
/**
* 根据entityClass类型创建Criteria实例
* @param entityClass
* @return
*/
public Criteria createCriteria(Class<?> entityClass){
SessionFactory sessionFactory = getHibernateTemplate().getSessionFactory();
Session session = sessionFactory.getCurrentSession();
Criteria criteria = session.createCriteria(entityClass);
return criteria;
}
/**
* 根据查询条件创建Criterion实例集合
* @param conditions
* @return
*/
public Criteria createCriteria(Class<?> entityClass,HibernateQueryContion... conditions) {
SessionFactory sessionFactory = getHibernateTemplate().getSessionFactory();
Session session = sessionFactory.getCurrentSession();
Criteria criteria = session.createCriteria(entityClass);
for (HibernateQueryContion condition : conditions) {
switch (HibernateQueryContion.queryType.valueOf(condition.getType())) {
case in:
criteria.add(Restrictions.in(condition.getPropertyName(), (Collection) condition.getValue()));
break;
case eq:
criteria.add(Restrictions.eq(condition.getPropertyName(), condition.getValue()));
break;
case like:
criteria.add(Restrictions.like(condition.getPropertyName(), condition.getValue()));
break;
case asc:
criteria.addOrder(Order.asc(condition.getPropertyName()));
break;
case ne:
criteria.add(Restrictions.ne(condition.getPropertyName(), condition.getValue()));
break;
case isNull:
criteria.add(Restrictions.isNull(condition.getPropertyName()));
default:
break;
}
}
return criteria;
}


package com.hikvision.cms.scaffolding.dao;

import java.util.List;

import org.hibernate.type.NullableType;


public class HibernateQueryContion{
public static final String EQ = "eq";
public static final String LIKE = "like";
public static final String IN = "in";
public static final String ASC = "asc";
public static final String NE = "ne";
public static final String ISNULL = "isNull";

private Object value;
private String propertyName;
private String type;
private NullableType hiberateType;

public HibernateQueryContion(String propertyName, Object value) {
super();
this.propertyName = propertyName;
this.value = value;
this.type = HibernateQueryContion.EQ;
}
public HibernateQueryContion(String propertyName, Object value,String type) {
super();
this.propertyName = propertyName;
this.value = value;
this.setType(type);
}

public HibernateQueryContion(String propertyName, NullableType hiberateType) {
super();
this.propertyName = propertyName;
this.hiberateType = hiberateType;
}
public Object getValue() {
return value;
}
public void setValue(Object value) {
this.value = value;
}
public void setPropertyName(String propertyName) {
this.propertyName = propertyName;
}
public String getPropertyName() {
return propertyName;
}
public void setType(String type) {
this.type = type;
}
public String getType() {
return type;
}
public void setHiberateType(NullableType hiberateType) {
this.hiberateType = hiberateType;
}
public NullableType getHiberateType() {
return hiberateType;
}
public static enum queryType{
eq,like,in,asc,ne,isNull;
public queryType getQueryTypeEnum(String type){
return valueOf(type);
}
}
}