SSM框架基础封装:BaseDao(IBaseDao)、BaseAction
IBaseDao--BaseDao:
package com.hfxt.dao.impl;
import java.io.Serializable;
import java.math.BigInteger;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.hibernate.Query;
import org.hibernate.Session;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.dao.DataAccessException;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import org.springframework.transaction.annotation.Transactional;
import com.hfxt.common.Pager;
import com.hfxt.common.Validity;
import com.hfxt.dao.IBaseDao;
import com.hfxt.exception.HibernateDaoSupportException;
public class BaseDao<T, PK extends Serializable> extends HibernateDaoSupport implements IBaseDao<T, PK> {
protected Logger log = LoggerFactory.getLogger(this.getClass());
private boolean isCacheQueries = true;
public boolean isCacheQueries() {
return isCacheQueries;
}
public void setCacheQueries(boolean isCacheQueries) {
this.isCacheQueries = isCacheQueries;
}
/**
* The <code>add(T entity)</code> method is add object to database.
* @param entity
* if you want to add entity.
* @throws HibernateDaoSupportException
* Throws HibernateDaoSupportException when accessing and
* manipulating database happen exception.
*/
public PK add(T entity) throws HibernateDaoSupportException {
if (null == entity) {
throw new HibernateDaoSupportException("Param(#"
+ this.getClass().getName() + ") with value null");
}
try {
PK pk = (PK) this.getHibernateTemplate().save(entity);
log.debug("Executing save method is successful!");
return pk;
} catch (DataAccessException e) {
log.error("Error saving entity:{}", e);
throw new HibernateDaoSupportException("Error saving (#"
+ this.getClass().getName() + "#):" + e);
}
}
/**
* 更新或保存实体
* @param entity
*/
public void saveOrUpdateEntity(T entity) {
this.getHibernateTemplate().saveOrUpdate(entity);
}
/**
* The <code>delete(T entity)</code> method is delete object to database.
* @param entity
* if you want to delete entity.
* @throws HibernateDaoSupportException
* Throws HibernateDaoSupportException when accessing and
* manipulating database happen exception.
*/
public void delete(T entity) throws HibernateDaoSupportException {
if (null == entity) {
throw new HibernateDaoSupportException("Param(#"
+ this.getClass().getName() + ") with value null");
}
try {
this.getHibernateTemplate().delete(entity);
log.debug("Execute delete method is successful!");
} catch (DataAccessException e) {
log.error("Error deleting entity:{}", e);
throw new HibernateDaoSupportException("Error deleting (#"
+ this.getClass().getName() + "#):" + e);
}
}
/**
* The <code>find(T entity)</code> method is find object according object
* type.
* @param entity
* if you want to find class condition.
* @return List<T> according entity to find object's connection.
* @throws HibernateDaoSupportException
* Throws HibernateDaoSupportException when accessing and
* manipulating database happen exception.
*/
@SuppressWarnings("unchecked")
public List<T> find(T entity) throws HibernateDaoSupportException {
if (null == entity) {
throw new HibernateDaoSupportException("Param(#"
+ this.getClass().getName() + ") with value null");
}
List lists = null;
try {
if (isCacheQueries) {
this.getHibernateTemplate().setCacheQueries(true);
} else {
isCacheQueries = true;
this.getHibernateTemplate().setCacheQueries(false);
}
lists = (List<T>) this.getHibernateTemplate().findByExample(entity);
log.debug("Execute find method is successful!");
} catch (DataAccessException e) {
log.error("Error finding entity: {}", e);
throw new HibernateDaoSupportException("Error finding (#"
+ this.getClass().getName() + "#):" + e);
}
return lists;
}
/**
* find object's collection with class
* @param clazz
* according class
* @return Object's connection
* @throws HibernateDaoSupportException
* when accessing and manipulating database happen exception
*/
@SuppressWarnings("unchecked")
public List<T> find(Class<T> clazz) throws HibernateDaoSupportException {
if (null == clazz) {
throw new HibernateDaoSupportException(
"Param(#clazz) with value null");
}
String hql = "FROM " + clazz.getName();
List<T> lists = null;
try {
if (isCacheQueries) {
this.getHibernateTemplate().setCacheQueries(true);
} else {
isCacheQueries = true;
this.getHibernateTemplate().setCacheQueries(false);
}
lists = (List<T>) this.getHibernateTemplate().find(hql);
log.debug("Execute find method is successful!");
} catch (DataAccessException e) {
log.error("Error finding entity:{}", e);
throw new HibernateDaoSupportException("Error finding (#"
+ this.getClass().getName() + "#):" + e);
}
return lists;
}
/**
* The <code>findById(PK id)</code> method is find object according primary
* key.
* @param id
* if you want to find object's primary key
* @return T insject object
* @throws HibernateDaoSupportException
* Throws HibernateDaoSupportException when accessing and
* manipulating database happen exception.
*/
@SuppressWarnings("unchecked")
public T findById(PK id, Class<T> clazz)
throws HibernateDaoSupportException {
if (null == id) {
throw new HibernateDaoSupportException("PK with value null");
}
T entity = null;
String hql = "FROM " + clazz.getName() + " n where n.id = ";
if (id instanceof String) {
hql += "'" + id + "'";
} else {
hql += id;
}
try {
// use 2 leave cache
entity = (T) this.getUniqueBeanResult(hql);
log.debug("Execute findById method is successful!");
} catch (DataAccessException e) {
log.error("Error finding entity:{}", e);
throw new HibernateDaoSupportException("Error finding ("
+ this.getClass().getName() + "):" + e);
}
return entity;
}