import java.io.Serializable;
import java.sql.SQLException;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.springframework.dao.DataAccessException;
public interface ICommonDao<T> {
/**
* SQL通用操作
*
* @param sql
* @throws HibernateException
* @throws SQLException
*/
public void excuteBySql(String sql) throws HibernateException, SQLException;
/**
* SQL通用操作
*
* @param sql
* @param parameters
* @throws HibernateException
* @throws SQLException
*/
public void excuteBySql(String sql, HashMap<String, Object> parameters) throws HibernateException, SQLException;
/**
* 用SQL查询,返回列表
*
* @param sql
* @return
* @throws HibernateException
* @throws SQLException
*/
public List<Object[]> findListBySql(String sql) throws HibernateException, SQLException;
/**
* 用SQL和参数查询,返回列表
*
* @param sql
* @param parameters
* @return
* @throws HibernateException
* @throws SQLException
*/
public List<Object[]> findListBySql(String sql, HashMap<String, Object> parameters) throws HibernateException, SQLException;
/**
* 用SQL、页码和页大小查询,返回分页列表
*
* @param sql
* @param currentPage
* @param maxPerPage
* @return
* @throws HibernateException
* @throws SQLException
*/
public List<Object[]> pageListBySql(String sql, Integer currentPage, Integer maxPerPage) throws HibernateException, SQLException;
/**
* 用SQL、参数、页码和页大小查询,返回分页列表
* @param sql
* @param parameters
* @param currentPage
* @param maxPerPage
* @return
* @throws HibernateException
* @throws SQLException
*/
public List<Object[]> pageListBySql(String sql, HashMap<String, Object> parameters, Integer currentPage, Integer maxPerPage) throws HibernateException, SQLException;
/**
* 使用SQL查询记录总数
*
* @param sql
* @param parameters
* @return
* @throws HibernateException
* @throws SQLException
*/
public Integer getRecordNumberBySql(String sql, HashMap<String, Object> parameters) throws HibernateException, SQLException;
/**
* HQL通用操作
*
* @param sql
* @throws HibernateException
* @throws SQLException
*/
public void excuteByHql(String hql) throws HibernateException, SQLException;
/**
* HQL通用操作
*
* @param sql
* @throws HibernateException
* @throws SQLException
*/
public void excuteByHql(String hql,HashMap<String, Object> parameters) throws HibernateException, SQLException;
/**
* 用HQL查询,返回列表
*
* @param sql
* @param parameters
* @return
* @throws HibernateException
* @throws SQLException
*/
public List<T> findListByHql(String hql) throws HibernateException, SQLException;
/**
* 用HQL和参数查询,返回列表
*
* @param sql
* @param parameters
* @return
* @throws HibernateException
* @throws SQLException
*/
public List<T> findListByHql(String hql, HashMap<String, Object> parameters) throws HibernateException, SQLException;
/**
* 用HQL、页码和页大小查询,返回分页列表
* @param sql
* @param parameters
* @param currentPage
* @param maxPerPage
* @return
* @throws HibernateException
* @throws SQLException
*/
public List<T> pageListByHql(String hql, Integer currentPage, Integer maxPerPage) throws HibernateException, SQLException;
/**
* 用HQL、参数、页码和页大小查询,返回分页列表
* @param sql
* @param parameters
* @param currentPage
* @param maxPerPage
* @return
* @throws HibernateException
* @throws SQLException
*/
public List<T> pageListByHql(String hql, HashMap<String, Object> parameters, Integer currentPage, Integer maxPerPage) throws HibernateException, SQLException;
/**
* 使用HQL查询记录总数
*
* @param sql
* @param parameters
* @return
* @throws HibernateException
* @throws SQLException
*/
public Integer getRecordNumberByHql(String hql, HashMap<String, Object> parameters) throws HibernateException, SQLException;
/**
* 根据类型和ID获取对象
*
* @param clazz
* @param id
* @return
* @throws DataAccessException
*/
public Object get(Class<T> clazz, Serializable id) throws DataAccessException;
/**
* 删除对象
*
* @param obj
* @throws DataAccessException
*/
public void delete(Object obj) throws DataAccessException;
/**
* 根据类型和ID删除对象
*
* @param clazz
* @param id
* @throws DataAccessException
*/
public void delete(Class<T> clazz, Serializable id) throws DataAccessException;
/**
* 保存对象
*
* @param obj
* @throws DataAccessException
*/
public void save(Object obj) throws DataAccessException;
/**
* 更新对象,对象在库中必须有对应的ID
*
* @param obj
* @throws DataAccessException
*/
public void update(Object obj) throws DataAccessException;
/**
* 更新或插入
*
* @param obj
* @throws DataAccessException
*/
public void saveOrUpdate(Object obj) throws DataAccessException;
/**
* 批量更新或插入
*
* @param entities
* @throws DataAccessException
*/
public void saveOrUpdateAll(Collection<Object> entities) throws DataAccessException;
/**
* 获取hibernate的session
*
* @return
* @throws HibernateException
*/
public Session getsSession() throws HibernateException;
}
import java.io.Serializable;
import java.sql.SQLException;
import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.dao.DataAccessException;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
/**
* 数据库操作通用类
*
* @author Watson Xu
* @date 2012-11-20 下午03:25:24
*/
public class CommonDaoT<T> extends HibernateDaoSupport implements ICommonDao<T> {
@Override
public void excuteBySql(final String sql) throws HibernateException, SQLException {
excuteByHql(sql, null);
}
@Override
public void excuteBySql(final String sql, final HashMap<String, Object> parameters) throws HibernateException, SQLException {
getHibernateTemplate().executeWithNativeSession(new HibernateCallback() {
public Object doInHibernate(Session session) throws HibernateException {
Query query = session.createSQLQuery(sql);
if(parameters != null) {
Iterator<String> it = parameters.keySet().iterator();
while (it.hasNext()) {
String key = (String) it.next();
query.setParameter(key, parameters.get(key));
}
}
return query.executeUpdate();
}
});
}
@Override
public List<Object[]> findListBySql(String sql) throws HibernateException, SQLException {
return pageListBySql(sql, null, null, null);
}
@Override
public List<Object[]> findListBySql(String sql, HashMap<String, Object> parameters) throws HibernateException, SQLException {
return pageListBySql(sql, parameters, null, null);
}
@Override
public List<Object[]> pageListBySql(String sql, Integer currentPage, Integer maxPerPage) throws HibernateException, SQLException {
return pageListBySql(sql, null, currentPage, maxPerPage);
}
@Override
@SuppressWarnings("unchecked")
public List<Object[]> pageListBySql(final String sql, final HashMap<String, Object> parameters, final Integer currentPage, final Integer maxPerPage) throws HibernateException, SQLException {
return (List<Object[]>) this.getHibernateTemplate().executeWithNativeSession(new HibernateCallback() {
public List<Object[]> doInHibernate(Session session) throws HibernateException, SQLException {
List<Object[]> list = null;
Query query = session.createSQLQuery(sql);
if(parameters != null) {
Iterator<String> it = parameters.keySet().iterator();
while (it.hasNext()) {
String key = (String) it.next();
query.setParameter(key, parameters.get(key));
}
}
if(currentPage != null && maxPerPage != null) {
int start;
if (currentPage <= 0) {
start = 0;
} else {
start = (currentPage - 1) * maxPerPage;
}
query.setFirstResult(start);
query.setMaxResults(maxPerPage);
}
list = query.list();
return list;
}
});
}
@Override
public Integer getRecordNumberBySql(final String sql, final HashMap<String, Object> parameters) throws HibernateException, SQLException{
return (Integer)getHibernateTemplate().execute(new HibernateCallback(){
public Integer doInHibernate(Session session) throws HibernateException, SQLException {
int fromIndex = sql.indexOf("from");
if(fromIndex < 0) fromIndex = sql.indexOf("FROM");
String q = "SELECT COUNT(*) " + sql.substring(fromIndex);
Query query = session.createSQLQuery(q);
if(parameters != null) {
Iterator<String> it = parameters.keySet().iterator();
while (it.hasNext()) {
String key = (String) it.next();
query.setParameter(key, parameters.get(key));
}
}
return Integer.parseInt(query.uniqueResult().toString());
}
});
}
@Override
public void excuteByHql(final String hql) throws HibernateException, SQLException {
excuteByHql(hql, null);
}
@Override
public void excuteByHql(final String hql, final HashMap<String, Object> parameters) throws HibernateException, SQLException {
getHibernateTemplate().executeWithNativeSession(new HibernateCallback() {
public Object doInHibernate(Session session) throws HibernateException, SQLException {
Query query = session.createQuery(hql);
if(parameters != null) {
Iterator<String> it = parameters.keySet().iterator();
while (it.hasNext()) {
String key = (String) it.next();
query.setParameter(key, parameters.get(key));
}
}
return query.executeUpdate();
}
});
}
@Override
public List<T> findListByHql(String hql) throws HibernateException, SQLException {
return pageListByHql(hql, null, null, null);
}
@Override
public List<T> findListByHql(String hql, HashMap<String, Object> parameters) throws HibernateException, SQLException {
return pageListByHql(hql, parameters, null, null);
}
@Override
public List<T> pageListByHql(String hql, Integer currentPage, Integer maxPerPage) throws HibernateException, SQLException {
return pageListByHql(hql, null, currentPage, maxPerPage);
}
@SuppressWarnings("unchecked")
@Override
public List<T> pageListByHql(final String hql, final HashMap<String, Object> parameters, final Integer currentPage, final Integer maxPerPage) throws HibernateException, SQLException {
return (List<T>) this.getHibernateTemplate().executeWithNativeSession(new HibernateCallback() {
public List<T> doInHibernate(Session session) throws HibernateException, SQLException {
List<T> list = null;
Query query = session.createQuery(hql);
if(parameters != null) {
Iterator<String> it = parameters.keySet().iterator();
while (it.hasNext()) {
String key = (String) it.next();
query.setParameter(key, parameters.get(key));
}
}
if(currentPage != null && maxPerPage != null) {
int start;
if (currentPage <= 0) {
start = 0;
} else {
start = (currentPage - 1) * maxPerPage;
}
query.setFirstResult(start);
query.setMaxResults(maxPerPage);
}
list = query.list();
return list;
}
});
}
@Override
public Integer getRecordNumberByHql(final String hql, final HashMap<String, Object> parameters) throws HibernateException, SQLException {
return (Integer)getHibernateTemplate().execute(new HibernateCallback(){
public Integer doInHibernate(Session session) throws HibernateException, SQLException {
int fromIndex = hql.indexOf("from");
if(fromIndex < 0) fromIndex = hql.indexOf("FROM");
String q = "SELECT COUNT(*) " + hql.substring(fromIndex);
Query query = session.createQuery(q);
if(parameters != null) {
Iterator<String> it = parameters.keySet().iterator();
while (it.hasNext()) {
String key = (String) it.next();
query.setParameter(key, parameters.get(key));
}
}
return Integer.parseInt(query.uniqueResult().toString());
}
});
}
@Override
public Object get(Class<T> clazz, Serializable id) throws DataAccessException {
Object o = getHibernateTemplate().get(clazz, id);
return o;
}
@Override
public void delete(Object obj) throws DataAccessException {
getHibernateTemplate().delete(obj);
}
@Override
public void delete(Class<T> clazz, Serializable id) throws DataAccessException {
Object o = getHibernateTemplate().get(clazz, id);
getHibernateTemplate().delete(o);
}
@Override
public void save(Object obj) throws DataAccessException {
getHibernateTemplate().save(obj);
}
@Override
public void update(Object obj) throws DataAccessException {
getHibernateTemplate().update(obj);
}
@Override
public void saveOrUpdate(Object obj) throws DataAccessException {
getHibernateTemplate().saveOrUpdate(obj);
}
@Override
public void saveOrUpdateAll(Collection<Object> entities) throws DataAccessException {
getHibernateTemplate().saveOrUpdateAll(entities);
}
@Override
public Session getsSession() throws HibernateException {
return getSessionFactory().openSession();
}
}
|