import java.io.Serializable; |
import java.sql.SQLException; |
import java.util.Collection; |
import java.util.Iterator; |
import java.util.List; |
import java.util.Map; |
import org.apache.commons.lang.StringUtils; |
import org.hibernate.Criteria; |
import org.hibernate.HibernateException; |
import org.hibernate.LockMode; |
import org.hibernate.Query; |
import org.hibernate.SQLQuery; |
import org.hibernate.Session; |
import org.hibernate.SessionFactory; |
import org.hibernate.criterion.DetachedCriteria; |
import org.hibernate.criterion.Restrictions; |
import org.hibernate.persister.entity.AbstractEntityPersister; |
import org.hibernate.transform.Transformers; |
import org.springframework.beans.factory.annotation.Autowired; |
import org.springframework.orm.hibernate3.HibernateCallback; |
import org.springframework.orm.hibernate3.support.HibernateDaoSupport; |
import org.springframework.stereotype.Repository; |
import com.eshore.edms.common.util.EntityUtil; |
import com.eshore.edms.common.util.StringHelper; |
import com.googlecode.ehcache.annotations.Cacheable; |
/** |
* 通用数据访问对象. |
*/ |
@SuppressWarnings ( "unchecked" ) |
@Repository () |
public class CommonDAO extends HibernateDaoSupport { |
/** |
* 取当前管理的数据访问会话. |
* |
* @return hibernate会话对象 |
*/ |
public Session currentSession() { |
return this .getSession(); |
} |
|
/** |
* 装载实体 |
* |
* @param pClass |
* 实体类 |
* @param primaryKey |
* 实体主键 |
*/ |
|
public <T>T findById(Class<T> pClass, Serializable primaryKey) { |
return this .getHibernateTemplate().get(pClass, primaryKey); |
} |
|
/** |
* 根据指定参数查找 |
* @param <T> |
* @param cls |
* @param name |
* @param value |
* @return |
*/ |
public <T> List<T> findAllByProperty(Class<T> cls, String name, Object value) { |
List<T> lst= this .getHibernateTemplate().find( " from " +cls.getSimpleName()+ " where " +name+ "=?" , new Object[]{value}); |
return lst; |
} |
|
/** |
* 根据指定参数查找 |
* @param cls |
* @param name |
* @param value |
* @return |
*/ |
public <T> T findByProperty(Class<T> cls, String name, Object value) { |
List<?> lst = this .getHibernateTemplate().find( " from " + cls.getSimpleName() + " where " + name + "=?" , |
new Object[] { value }); |
if (lst != null && !lst.isEmpty()) { |
return (T) lst.get( 0 ); |
} else { |
return null ; |
} |
} |
|
/** |
* 根据指定参数查找 |
* @param cls |
* @param name |
* @param value |
* @return |
*/ |
public <T> List<T> findByProperty(Class<T> cls, Map<String, Object> param) { |
DetachedCriteria detachedCriteria = DetachedCriteria.forClass(cls, "entity" ); |
Criteria criteria = detachedCriteria.getExecutableCriteria( this .getSession()); |
for (Iterator<String> it = param.keySet().iterator(); it.hasNext();) { |
String key = (String) it.next(); |
Object value = param.get(key); |
if (value instanceof Collection) { |
criteria.add(Restrictions.in(key, (Collection<?>) value)); |
} else if (value instanceof Object[]) { |
criteria.add(Restrictions.in(key, (Object[]) value)); |
} else { |
criteria.add(Restrictions.eq(key, value)); |
} |
} |
return criteria.list(); |
} |
|
/** |
* 保存实体. |
* |
* @param pObject |
* 实体对象 |
* @return 实体主键 |
*/ |
public void save(Object pObject) { |
this .getHibernateTemplate().saveOrUpdate(pObject); |
} |
|
/** |
* 保存实体,利用主键自动除空 |
* @param pObject |
* @throws IllegalAccessException |
* @throws IllegalArgumentException |
*/ |
public void saveOrUpdate(Object pObject) { |
try { |
//如果主键为空,直接设置为null |
String primaryKeyValue = EntityUtil.getPrimaryKeyValue( |
currentSession(), pObject.getClass(), pObject); |
if (StringUtils.isBlank(primaryKeyValue)) { |
EntityUtil.setPrimaryKeyValue(currentSession(), |
pObject.getClass(), pObject, null ); |
} |
} catch (Exception e) { |
e.printStackTrace(); |
} |
this .getHibernateTemplate().saveOrUpdate(pObject); |
} |
|
/** |
* 批量保存 |
* @param entities |
*/ |
public void saveOrUpdateAll(Collection<?> entities){ |
this .getHibernateTemplate().saveOrUpdateAll(entities); |
} |
/** |
* 删除实体. |
* |
* @param pObject |
* 实体对象 |
*/ |
public void delete(Object pObject) { |
this .getHibernateTemplate().delete(pObject); |
} |
|
/** |
* 批量删 |
* @param entities |
*/ |
public void deleteAll(Collection<?> entities){ |
this .getHibernateTemplate().deleteAll(entities); |
} |
/** |
* 更新实体. |
* |
* @param pObject |
* 实体对象 |
*/ |
public void update(Object pObject) { |
this .getHibernateTemplate().update(pObject); |
} |
/** |
* 比较变化保存 |
*/ |
public Object merge(Object po) { |
return this .getSession().merge(po); |
} |
|
/** |
* 根据参数名称、参数值查询实体对象 |
* |
* @param queryString |
* @param paramNames |
* @param values |
* @return |
*/ |
|
public <T>List<T> findByNamedParam(String queryString, String paramName,Object value) { |
return this .getHibernateTemplate().findByNamedParam(queryString, paramName, value); |
} |
/** |
* 根据参数名称、参数值查询实体对象 |
* |
* @param queryString |
* @param paramNames |
* @param values |
* @return |
*/ |
|
public <T>List<T> findByNamedParam(String queryString, String[] paramNames, |
Object[] values) { |
return this .getHibernateTemplate().findByNamedParam(queryString, |
paramNames, values); |
} |
|
/** |
* |
* @param queryString |
* @return |
*/ |
public <T>List<T> find(String queryString){ |
return this .getHibernateTemplate().find(queryString); |
} |
|
|
public <T> List<T> find(String queryString,Object[] values){ |
return this .getHibernateTemplate().find(queryString,values); |
} |
|
public <T> List<T> find(String queryString,Object values){ |
return this .getHibernateTemplate().find(queryString,values); |
} |
|
public <T> List<T> findByHQL(String queryString, Map<String, Object> map) { |
Query query = currentSession().createQuery(queryString); |
if (map != null ) { |
query.setProperties(map); |
} |
return query.list(); |
} |
public <T> List<T> findBySQL(String queryString, Map<String, Object> map) { |
SQLQuery query = currentSession().createSQLQuery(queryString); |
if (map != null ) { |
query.setProperties(map); |
} |
return query.list(); |
} |
|
public <T> List<T> findBySQL(String queryString, Map<String, Object> map, Class<T> clazz) { |
SQLQuery query = currentSession().createSQLQuery(queryString); |
if (clazz != null ) { |
query.addEntity(clazz); |
} |
else { |
query.setResultTransformer(Transformers.ALIAS_TO_ENTITY_MAP); |
} |
if (map != null ) { |
query.setProperties(map); |
} |
return query.list(); |
} |
|
/** |
* 根据hql创建查询对象 |
* @param hql |
* @param values 数量量可变的参数,按顺序绑定. |
* @return |
*/ |
public Query createQuery(String hql, final Object... values) { |
Query query = currentSession().createQuery(hql); |
if (values != null ){ |
for ( int i = 0 ; i < values.length; i++) { |
query.setParameter(i, values[i]); |
} |
} |
return query; |
} |
|
/** |
* 根据hql创建查询对象 |
* @param hql |
* @param values 命名参数,按名称绑定. |
* @return |
*/ |
public Query createQuery(String hql, final Map<String, Object> values) { |
Query query = currentSession().createQuery(hql); |
if (values != null ){ |
query.setProperties(values); |
} |
return query; |
} |
|
|
|
/** |
* 执行HQL进行批量修改/删除操作. |
* |
* @param values 数量可变的参数,按顺序绑定. |
* @return 更新记录数. |
*/ |
public int batchHQLExecute( final String hql, final Object... values) { |
return createQuery(hql, values).executeUpdate(); |
} |
|
/** |
* 执行SQL进行批量修改/删除操作. |
* |
* @param values 数量可变的参数,按顺序绑定. |
* @return 更新记录数. |
*/ |
public int batchSQLExecute( final String sql, final Object... values) { |
return createSQLQuery(sql, values).executeUpdate(); |
} |
|
/** |
* 根据sql创建查询对象 |
* |
* @param queryString |
* @return |
*/ |
public SQLQuery createSQLQuery(String queryString, final Object... values) { |
SQLQuery query = currentSession().createSQLQuery(queryString); |
if (values != null ){ |
for ( int i = 0 ; i < values.length; i++) { |
query.setParameter(i, values[i]); |
} |
} |
return query; |
} |
|
|
/** |
* 根据sql创建查询对象 |
* |
* @param queryString |
* @return |
*/ |
public SQLQuery createSQLQuery(String queryString, final Map<String, Object> values) { |
SQLQuery query = currentSession().createSQLQuery(queryString); |
if (values != null ){ |
query.setProperties(values); |
} |
return query; |
} |
|
/** |
* 根据sql创建查询对象,并且返回指定的对象列表 |
* |
* @param queryString |
* @return |
*/ |
public SQLQuery createSQLQuery(String sql, final Map<String, Object> values, Class<?> clz) { |
SQLQuery query = this .getSession().createSQLQuery(sql); |
if (values != null ) { |
query.setProperties(values); |
} |
if (clz != null ) { |
query.addEntity(clz); |
} else { |
query.setResultTransformer(Transformers.ALIAS_TO_ENTITY_MAP); |
} |
return query; |
} |
|
/** |
* |
* @作者:庞健 |
* @类的说明:更新 |
* @MSN: |
* @2012-7-30 |
*/ |
public int updateSQLExecute(String sql,Object object){ |
Query query= this .getSession().createSQLQuery(sql); |
if (object!= null ){ |
query.setProperties(object); |
} |
return query.executeUpdate(); |
} |
/** |
* 锁定实体 |
* |
* @param entity |
* @param lockMode |
*/ |
public void refresh(Object entity, LockMode lockMode) { |
this .getHibernateTemplate().refresh(entity, lockMode); |
} |
|
/** |
* 手动提交事务,提交前会自动调用一个flush()函数,会把缓存中的数据全部输出去 |
* @author Liangr |
* Jan 28, 2014 |
* void |
*/ |
public void commit() { |
this .getSession().getTransaction().commit(); |
} |
/** |
* 刷新一级缓存区的内容,使之与数据库数据保持同步 |
*/ |
public void flush() { |
this .getHibernateTemplate().flush(); |
} |
/** |
* 将一级缓存中的所有持久化对象清除,释放其占用的内存资源 |
*/ |
public void clear() { |
this .getHibernateTemplate().clear(); |
} |
|
/** |
* 将对象从二级缓存中删除,持久化对象未清除 |
*/ |
@SuppressWarnings ({ "deprecation" }) |
public void evict(Class<?> clazz,Serializable poId) { |
this .getSession().getSessionFactory().evict(clazz, poId); |
} |
|
/** |
* 将对象从一级缓存中删除,持久化对象未清除 |
*/ |
public void evict(Object po) { |
this .getSession().evict(po); |
} |
|
|
/** |
* |
* @param sessionFactory |
*/ |
@Autowired |
public void setSession$Factory(SessionFactory sessionFactory) { |
super .setSessionFactory(sessionFactory); |
} |
|
public SessionFactory getSession$Factory() { |
return super .getSessionFactory(); |
} |
|
/** |
* 调用存储过程 |
* |
* @param procedureSql example: "{call pro_ADD_PARTITION_wareid(?,?)}" |
* @param values |
* @return |
*/ |
public <T> int callProcedure(String procedureSql, final Object... values) { |
SQLQuery query = currentSession().createSQLQuery(procedureSql); |
if (values != null ){ |
for ( int i = 0 ; i < values.length; i++) { |
query.setParameter(i, values[i]); |
} |
} |
int executeUpdate = query.executeUpdate(); |
return executeUpdate; |
} |
|
/** |
* 根据 序列 名称跟长度获取唯一编码 |
* @param sequenceName |
* @param totalLen |
* @return |
*/ |
public String getSequence( final String sequenceName, int totalLen) { |
Integer s = (Integer) getHibernateTemplate().execute( |
new HibernateCallback<Object>() { |
public Object doInHibernate(Session session) |
throws HibernateException, SQLException { |
SQLQuery query = session.createSQLQuery( "select " |
+ sequenceName + ".nextval from dual" ); |
return new Integer(query.list().get( 0 ).toString()); |
} |
}); |
return StringHelper.lpad( "" + s, totalLen, '0' ); |
} |
|
@Cacheable (cacheName= "system" ) |
public String getTableName(Class<?> clazz) { |
AbstractEntityPersister meta = (AbstractEntityPersister) this .getSessionFactory().getClassMetadata(clazz); |
// 实体名称 |
String entityName = meta.getTableName(); |
return entityName; |
} |
|
public Object execute(HibernateCallback<?> hibernateCallback) { |
return super .getHibernateTemplate().execute(hibernateCallback); |
} |
} |
初级程序员
by: jacksbon 发表于:2015-01-28 09:52:44 顶(0) | 踩(0) 回复
很好!!
回复评论