Hibernate 数据层基类实现

    提取经常操作表如新增、修改、删除、查询、分页查询、统计等业务功能,形成基类,用泛型传参,有利于每个实体对象数据层继承。

package com.base.dao;

import java.io.Serializable;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.annotation.Resource;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.criterion.CriteriaSpecification;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

/**
 * 数据库操作接口实现类,where条件使用命名参数
 *  
 */
@Repository("baseDao")
@SuppressWarnings("all")
public class BaseDao<T> {

	private SessionFactory sessionFactory;

	public SessionFactory getSessionFactory() {
		return sessionFactory;
	}

	@Autowired
	public void setSessionFactory(SessionFactory sessionFactory) {
		this.sessionFactory = sessionFactory;
	}

	private Session getCurrentSession() {
		return sessionFactory.getCurrentSession();//获取数据库链接
	}

	/**
	 * 保存一个对象
	 * 
	 * @param T  要保存的JavaBean 对象
	 * 
	 */ 
	public Serializable save(T o) {
		return this.getCurrentSession().save(o); 
	}

	/**
	 * 删除一个对象
	 * 
	 * @param T  对象
	 *           
	 */ 
	public void delete(T o) {
		this.getCurrentSession().delete(o);
	}

	/**
	 * 修改一个对象
	 * 
	 * @param T  对象
	 *           
	 */ 
	public void update(T o) {
		this.getCurrentSession().update(o);
	} 

	/**
	 * 查询对象集合
	 * 
	 * @param Hql格式查询语句
	 * @return 对象集合
	 */ 
	public List<T> find(String hql) {
		return this.getCurrentSession().createQuery(hql).list();
	}

	/**
	 * 查询对象集合
	 * 
	 * @param Hql格式语句
	 * @param map参数
	 * @return 对象集合
	 */ 
	public List<T> find(String hql, Map<String, Object> map) {
		Query q = this.getCurrentSession().createQuery(hql);
		if (map != null) {
			Set<String> keySet = map.keySet();
			for (String string : keySet) {
				Object obj = map.get(string);
				if (obj instanceof Collection<?>) {
					q.setParameterList(string, (Collection<?>) obj);
				} else if (obj instanceof Object[]) {
					q.setParameterList(string, (Object[]) obj);
				} else {
					q.setParameter(string, obj);
				}
			}
		}
		return q.list();
	}

	/**
	 * 分页查询对象集合
	 * 
	 * @param Hql格式语句
	 * @param map参数
	 * @param 页码
	 * @param 每页记录数
	 * @return 对象集合
	 **/
	public List<T> find(String hql, Map<String, Object> map, Integer page, Integer rows) {
		if (page == null || page < 1) {
			page = 1;
		}
		if (rows == null || rows < 1) {
			rows = 10;
		}
		Query q = this.getCurrentSession().createQuery(hql);
		if (map != null) {
			Set<String> keySet = map.keySet();
			for (String string : keySet) {
				Object obj = map.get(string);
				if (obj instanceof Collection<?>) {
					q.setParameterList(string, (Collection<?>) obj);
				} else if (obj instanceof Object[]) {
					q.setParameterList(string, (Object[]) obj);
				} else {
					q.setParameter(string, obj);
				}
			}
		}
		return q.setFirstResult((page - 1) * rows).setMaxResults(rows).list();
	}

	/**
	 * 查询指定对象
	 * 
	 * @param 对象
	 * @param 主键值
	 * @return 获取对象
	 */
	public T get(Class<T> c, Serializable id) {
		return (T) this.getCurrentSession().get(c, id);
	}

	/**
	 * 查询指定对象
	 * 
	 * @param hql查询语句
	 * @param map参数
	 * @return 相应对象
	 */ 
	public T get(String hql, Map<String, Object> map) {
		List<T> l = this.find(hql, map);
		if (l != null && l.size() > 0) {
			return l.get(0);
		} else {
			return null;
		}
	}

	/**
	 * 查询记录数
	 * 
	 * @param hql查询语句
	 * @param map参数
	 * @return 记录数
	 */ 
	public Long count(String hql, Map<String, Object> map) {
		Query q = this.getCurrentSession().createQuery(hql);
		if (map != null) {
			Set<String> keySet = map.keySet();
			for (String string : keySet) {
				Object obj = map.get(string);
				if (obj instanceof Collection<?>) {
					q.setParameterList(string, (Collection<?>) obj);
				} else if (obj instanceof Object[]) {
					q.setParameterList(string, (Object[]) obj);
				} else {
					q.setParameter(string, obj);
				}
			}
		}
		return (Long) q.uniqueResult();
	}

	/**
	 * 执行更新语句
	 * 
	 * @param hql查询语句
	 * @param map参数
	 * @return 响应数目
	 */ 
	public Integer executeHql(String hql, Map<String, Object> map) {
		Query q = this.getCurrentSession().createQuery(hql);
		if (map != null) {
			Set<String> keySet = map.keySet();
			for (String string : keySet) {
				Object obj = map.get(string);
				if (obj instanceof Collection<?>) {
					q.setParameterList(string, (Collection<?>) obj);
				} else if (obj instanceof Object[]) {
					q.setParameterList(string, (Object[]) obj);
				} else {
					q.setParameter(string, obj);
				}
			}
		}
		return q.executeUpdate();
	}

	/**
	 * 执行查询语句
	 * 
	 * @param hql查询语句
	 * @param map 参数
	 * @return 响应数目
	 */
	public List<Object> queryHql(String hql, Map<String, Object> map) {
		Query q = this.getCurrentSession().createQuery(hql);
		if (map != null) {
			Set<String> keySet = map.keySet();
			for (String string : keySet) {
				Object obj = map.get(string);
				if (obj instanceof Collection<?>) {
					q.setParameterList(string, (Collection<?>) obj);
				} else if (obj instanceof Object[]) {
					q.setParameterList(string, (Object[]) obj);
				} else {
					q.setParameter(string, obj);
				}
			}
		} 
		return q.list();
	}

	/**
	 * 执行查询语句
	 * 
	 * @param hql查询语句
	 * @param map 参数
	 * @return Map记录返回集合
	 */
	public List<Object> queryHqlMap(String hql, Map<String, Object> map) {
		Query q = this.getCurrentSession().createQuery(hql);
		if (map != null) {
			Set<String> keySet = map.keySet();
			for (String string : keySet) {
				Object obj = map.get(string);
				if (obj instanceof Collection<?>) {
					q.setParameterList(string, (Collection<?>) obj);
				} else if (obj instanceof Object[]) {
					q.setParameterList(string, (Object[]) obj);
				} else {
					q.setParameter(string, obj);
				}
			}
		}
		q.setResultTransformer(CriteriaSpecification.ALIAS_TO_ENTITY_MAP);
		return q.list();
	}
}

 

调用示例

@Repository("softwareDao")
public class SoftwareDao extends BaseDao<Software> {
	
	public void delete(String terminalId) {
	  String hql = "delete from Software WHERE terminalId = :terminalId";
	  Map<String, Object> paramMap = new HashMap<String, Object>(); 
	  paramMap.put("terminalId", terminalId);
	  executeHql(hql, paramMap);	
	}
	 
}

  

 

原文地址:https://www.cnblogs.com/walkwithmonth/p/11030998.html