Hibernate data layer base class implementation

    Operation table as the extraction often add, modify, delete, query, inquiry paging, statistical and other business functions, the base class is formed, with the generic parameter passing, in favor of each data layer entity object inheritance.

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> {

	the sessionFactory the SessionFactory Private; 

	public the SessionFactory the getSessionFactory () { 
		return the sessionFactory; 
	} 

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

	Private to getCurrentSession the Session () { 
		return sessionFactory.getCurrentSession (); // get database link 
	} 

	/ ** 
	 * save an object 
	 * 
	 * @param JavaBean object to be saved T 
	 * 
	 * / 
	public save the Serializable (T O) { 
		return this.getCurrentSession () save (O);. 
	} 

	/ ** 
	 * delete an object 
	 * 
	 * @param T Object 
	 *           
	 * / 
	Public void Delete (T O) { 
		this.getCurrentSession () Delete (O);. 
	} 

	/ ** 
	 * modify an object 
	 * 
	 * @param target T 
	 *            
	 * / 
	public void Update (T O) { 
		this.getCurrentSession ( ) .Update (O); 
	} 

	/ ** 
	 * query object set 
	 * 
	 * @param Hql query format 
	 * @return object set 
	 * / 
	public List <T> Find (String HQL) { 
		return this.getCurrentSession () the createQuery (. HQL) .list (); 
	} 

	/ ** 
	 * query object set 
	 * 
	 * @param Hql format statement 
	 * @param map parameter 
	 * @return object set 
	 * / 
	public List <T> find (String hql, Map <String, Object> map) {
		. This.getCurrentSession Query Q = () the createQuery (HQL); 
		IF (Map = null!) { 
			The Set <String> map.keySet keySet = (); 
			for (String String: keySet) { 
				Object obj as map.get = (String ); 
				IF (<?> the instanceof Collection obj) { 
					<?> q.setParameterList (String, (Collection) obj); 
				} the else IF (the instanceof obj Object []) { 
					q.setParameterList (String, (Object []) obj ); 
				} the else { 
					q.setParameter (String, obj); 
				} 
			} 
		} 
		return q.list (); 
	} 

	/ ** 
	 * paging query object set 
	 * 
	 * @param Hql format statement 
	 * @param map参数
	 * @param p 
	 * @param PageSize number 
	 * @return object collection 
	 ** /
	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); 
		} the else {
				}
			}
		} 
		Return q.setFirstResult ((Page -. 1) rows *) .setMaxResults (rows) .list (); 
	} 

	/ ** 
	 * query specified object 
	 * 
	 * @param target 
	 * @param primary key 
	 * @return get the object 
	 * / 
	T GET public (Class <T> C, the Serializable ID) { 
		return (T) this.getCurrentSession () GET (C, ID);. 
	} 

	/ ** 
	 * query specified object 
	 * 
	 * @param HQL query 
	 * @param map parameter 
	 * @return corresponding object 
	 * / 
	public T GET (String HQL, the Map <String, Object> Map) { 
		List <T> L = this.find (HQL, Map); 
		! IF (L = null && l.size ( )> 0) { 
			return l.get (0); 
			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();
	}
}

 

Example calls

@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);	
	}
	 
}

  

 

Guess you like

Origin www.cnblogs.com/walkwithmonth/p/11030998.html