`
woshihlp
  • 浏览: 10713 次
  • 性别: Icon_minigender_1
  • 来自: 广州
最近访客 更多访客>>
社区版块
存档分类
最新评论

GenericDao备份

阅读更多

Interface
import java.io.Serializable;
import java.util.List;

public interface GenericDao<T> extends Serializable {
	void addEntity(T o);

	void delEntity(T o);

	void updateEntity(T o);

	T getEntity(Class<T> clz, Serializable pk);
	
	Object getEntity(String hql,Object[] objs);

	Integer getPages(String hql);
	
	Integer getPages(String hql,Integer deptid);
	
	int getBlurredPage(String hql, Object[] objs);
	
	List<T> getListbyHql(String hql,Object[] objs);
	
	List<T> getListbyHql(String hql,int page);
	
	List<T> getListbyPage(String hql, Object[] objs, int page);
	
	List<T> getBlurredList(String hql, Object[] objs, int page);
	
	List<T> getGuessList(String hql,Object[] objs);
	
	boolean cbObjs(Object[] objs, String hql);
	
	Integer getPages(String hql,String str);
	
	Integer getNpages(String hql);
	
	Integer GP(String hql,Object[] objs,int pg);
	
	String getStrbyhql(String hql, Object[] objs);
	
	void updateByhql(String hql, Object[] objs);
	
	public Integer getAPages(Object[] objs, String hql);
	
	Object getObjbystr(String hql,String str);
	
	Double getDouble(String hql,Object[] objs);
	
	List<T> getListbyHql(String hql,Integer estid);
	
	List<T> getListbyHql(String hql,String status,int page);
	
	List<T> getListbyHql(String hql, Object[] objs,int page,int pg);
	
}


Class
import java.io.Serializable;
import java.util.List;

import org.hibernate.Hibernate;
import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.topzh.persists.dao.GenericDao;

@SuppressWarnings("serial")
public class GenericDaoImpl<T> extends HibernateDaoSupport implements
		GenericDao<T> {

	/**
	 * 添加
	 * 
	 * @param Object
	 */
	public void addEntity(T o) {
		getHibernateTemplate().save(o);
	}

	/**
	 * 删除
	 * 
	 * @param Object
	 */
	public void delEntity(T o) {
		getHibernateTemplate().delete(o);
	}

	/**
	 * 根据主键查询单个对象
	 * 
	 * @param Class
	 * @param primary
	 *            key
	 * @return Object
	 */
	@SuppressWarnings("unchecked")
	public Object getEntity(Class clz, Serializable pk) {
		return (T) getHibernateTemplate().get(clz, pk);
	}
	
	public Object getEntity(String hql,Object[] objs) {
		Object obj = getHibernateTemplate().find(hql, objs).get(0);
		return obj;
	}

	/**
	 * 根据Hql查询
	 * 
	 * @param String
	 *            hql
	 * @param Object
	 *            [] objs
	 * @return list
	 */
	@SuppressWarnings("unchecked")
	public List<T> getListbyHql(String hql, Object[] objs) {
		try {
			List<T> list;
			if (objs != null) {
				list = getHibernateTemplate().find(hql, objs);
			} else {
				list = getHibernateTemplate().find(hql);
			}
			return list;
		} catch (RuntimeException e) {
			e.printStackTrace();
			return null;
		} finally {
			if (getSession().isOpen()) {
				getSession().close();
			}
		}
	}	

	@SuppressWarnings("unchecked")
	public List<T> getListbyHql(String hql, Integer estid) {
		Session session = this.getHibernateTemplate().getSessionFactory()
				.openSession();
		try {
			Query query = session.createQuery(hql);
			query.setParameter(0, estid, Hibernate.INTEGER);
			query.setFirstResult(0);
			query.setMaxResults(10);
			List<T> list = query.list();
			return list;
		} catch (RuntimeException e) {
			e.printStackTrace();
			return null;
		} finally {
			if (session.isOpen()) {
				session.close();
			}
		}
	}

	@SuppressWarnings("unchecked")
	public List<T> getListbyHql(String hql, String status,int page) {
		Session session = this.getHibernateTemplate().getSessionFactory()
				.openSession();
		try {
			Query query = session.createQuery(hql);
			query.setParameter(0, status, Hibernate.STRING);
			query.setFirstResult((page - 1) * 10);
			query.setMaxResults(10);
			List<T> list = query.list();
			return list;
		} catch (RuntimeException e) {
			e.printStackTrace();
			return null;
		} finally {
			if (session.isOpen()) {
				session.close();
			}
		}
	}
	public List<T> getListbyHql(String hql, Object[] objs,int page,int pg) {
		Session session = this.getHibernateTemplate().getSessionFactory()
				.openSession();
		try {
			Query query = session.createQuery(hql);
			if (null!=objs) {
				for (int i = 0,n=objs.length; i < n; i++) {
					query.setParameter(i, objs[i]);
				}
			}
			query.setFirstResult((page - 1) * pg);
			query.setMaxResults(pg);
			List<T> list = query.list();
			return list;
		} catch (RuntimeException e) {
			e.printStackTrace();
			return null;
		} finally {
			if (session.isOpen()) {
				session.close();
			}
		}
	}

	/**
	 * 根据HQL查单个String
	 */
	public String getStrbyhql(String hql, Object[] objs) {
		String str = (String) getHibernateTemplate().find(hql, objs).get(0);
		if (getSession().isOpen()) {
			getSession().close();
		}
		return str;
	}

	@SuppressWarnings("unchecked")
	public List<T> geStrList(String hql) {
		List<T> list = getHibernateTemplate().find(hql);
		if (getSession().isOpen()) {
			getSession().close();
		}
		return list;
	}

	public Object getObjbystr(String hql, String str) {
		Object obj = getHibernateTemplate().find(hql, str).get(0);
		getSession().close();
		return obj;
	}

	/**
	 * 根据HQL更新
	 */
	public void updateByhql(String hql, Object[] objs) {
		try {
			getHibernateTemplate().bulkUpdate(hql, objs);
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if (getSession().isOpen()) {
//				getHibernateTemplate().getSessionFactory().
				getSession().close();
			}
		}
	}

	/**
	 * 获取分页页数
	 * 
	 * @param String
	 *            hql
	 * @return int page
	 */
	public Integer getPages(String hql) {
		Session session = this.getHibernateTemplate().getSessionFactory()
				.openSession();
		Integer count = (Integer) session.createQuery(hql).uniqueResult();
		Integer pg = 35;
		Integer p = count % pg;
		Integer page = count / pg;
		if (p > 0) {
			page++;
		}
		session.close();
		return page;
	}

	public Integer getPages(String hql, Integer deptid) {
		Session session = this.getHibernateTemplate().getSessionFactory()
				.openSession();
		Query query = session.createQuery(hql);
		query.setParameter(0, deptid, Hibernate.INTEGER);
		Integer count = (Integer) query.uniqueResult();
		Integer pg = 35;
		Integer p = count % pg;
		Integer page = count / pg;
		if (p > 0) {
			page++;
		}
		session.close();
		return page;
	}

	public Integer getPages(String hql, String str) {
		Session session = this.getHibernateTemplate().getSessionFactory()
				.openSession();
		Query query = session.createQuery(hql);
		query.setParameter(0, str, Hibernate.STRING);
		Integer count = (Integer) query.uniqueResult();
		Integer pg = 35;
		Integer p = count % pg;
		Integer page = count / pg;
		if (p > 0) {
			page++;
		}
		session.close();
		return page;
	}

	public Integer getAPages(Object[] objs, String hql) {
		Session session = this.getHibernateTemplate().getSessionFactory()
				.openSession();
		Query query = session.createQuery(hql);
		Integer month = (Integer) objs[0];
		Integer year = (Integer) objs[1];
		if (month != -1) {
			query.setParameter(0, month, Hibernate.INTEGER);
			query.setParameter(1, year, Hibernate.INTEGER);
		}
		Integer count = (Integer) query.uniqueResult();
		Integer pg = 35;
		Integer p = count % pg;
		Integer page = count / pg;
		if (p > 0) {
			page++;
		}
		session.close();
		return page;
	}
	
	public Integer getNpages(String hql) {
		Session session = this.getHibernateTemplate().getSessionFactory()
				.openSession();
		Integer count = (Integer) session.createQuery(hql).uniqueResult();
		Integer pg = 10;
		Integer p = count % pg;
		Integer page = count / pg;
		if (p > 0) {
			page++;
		}
		session.close();
		return page;
	}
	
	public Integer GP(String hql,Object[] objs,int pg) {
		Session session = this.getHibernateTemplate().getSessionFactory().openSession();
		Query query = session.createQuery(hql);
		if (null!=objs) {
			for (int i = 0,n=objs.length; i < n; i++) {
				query.setParameter(i, objs[i]);
			}
		}
		Integer count = (Integer) query.uniqueResult();
		Integer p = count % pg;
		Integer page = count/pg;
		if (p>0) {
			page++;
		}
		session.close();
		return page;
	}

	/**
	 * 分页查询
	 * 
	 * @param String
	 *            hql
	 * @param Object
	 *            [] objs
	 * @param int page
	 * @return list
	 */
	@SuppressWarnings("unchecked")
	public List<T> getListbyPage(String hql, Object[] objs, int page) {
		List<T> list = null;
		Session session = this.getHibernateTemplate().getSessionFactory()
				.openSession();
		try {
			Query query = session.createQuery(hql);
			if (null != objs) {
				for (int i = 0, n = objs.length; i < n; i++) {
					query.setParameter(i, objs[i]);
				}
			}
			query.setFirstResult((page - 1) * 35);
			query.setMaxResults(35);
			list = query.list();
		} catch (RuntimeException e) {
			list = null;
			e.printStackTrace();
		} finally {
			if (session.isOpen()) {
				session.close();
			}
		}
		return list;
	}

	/**
	 * 模糊查询页数
	 * 
	 * @param hql
	 * @param objs
	 *            []
	 * @return page
	 */
	public int getBlurredPage(String hql, Object[] objs) {
		int page;
		Session session = this.getHibernateTemplate().getSessionFactory()
				.openSession();
		try {
			Query query = session.createQuery(hql);
			for (int i = 0, n = objs.length; i < n; i++) {
				query.setParameter(i, objs[i]);
			}
			Integer count = (Integer) query.uniqueResult();
			int pg = 35;
			int p = count % pg;
			page = count / pg;
			if (p > 0) {
				page++;
			}
			return page;
		} catch (Exception e) {
			e.printStackTrace();
			return 0;
		} finally {
			if (session.isOpen()) {
				session.close();
			}
		}
	}

	/**
	 * 模糊查询结果分页
	 * 
	 * @param hql
	 * @param objs
	 *            []
	 * @param page
	 * @return list
	 */
	@SuppressWarnings("unchecked")
	public List<T> getBlurredList(String hql, Object[] objs, int page) {
		Session session = this.getHibernateTemplate().getSessionFactory()
				.openSession();
		try {
			Query query = session.createQuery(hql);
			if (null != objs) {
				for (int i = 0, n = objs.length; i < n; i++) {
					query.setParameter(i, objs[i]);
				}
			}
			query.setFirstResult((page - 1) * 35);
			query.setMaxResults(35);
			List<T> list = query.list();
			return list;
		} catch (RuntimeException e) {
			e.printStackTrace();
			return null;
		} finally {
			if (session.isOpen()) {
				session.close();
			}
		}
	}

	/**
	 * 模糊查询
	 * 
	 * @param hql
	 * @param objs
	 * @param page
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public List<T> getGuessList(String hql, Object[] objs) {
		Session session = this.getHibernateTemplate().getSessionFactory()
				.openSession();
		try {
			Query query = session.createQuery(hql);
			if (null != objs) {
				for (int i = 0, n = objs.length; i < n; i++) {
					query.setParameter(i, objs[i]);
				}
			}
			List<T> list = query.list();
			return list;
		} catch (RuntimeException e) {
			e.printStackTrace();
			return null;
		} finally {
			if (session.isOpen()) {
				session.close();
			}
		}
	}

	/**
	 * 非空验证
	 * 
	 * @param objs
	 *            []
	 * @param hql
	 * @return boolean
	 */
	@SuppressWarnings("unchecked")
	public boolean cbObjs(Object[] objs, String hql) {
		boolean flag = false;
		try {
			List<T> list = getHibernateTemplate().find(hql, objs);
			flag = list.size() > 0 ? true : false;
		} catch (RuntimeException e) {
			flag = false;
			e.printStackTrace();
		} finally {
			if (getSession().isOpen()) {
				getSession().close();
			}
		}
		return flag;
	}

	/**
	 * 更新单个对象
	 * 
	 * @param Object
	 */
	public void updateEntity(T o) {
		getHibernateTemplate().saveOrUpdate(o);
		if (getSession().isOpen()) {
			getSession().close();
		}
	}

	public Double getDouble(String hql, Object[] objs) {
		Double c = (Double) getHibernateTemplate().find(hql, objs).get(0);
		if (getSession().isOpen()) {
			getSession().close();
		}
		return c;
	}
}


web.xml
<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath*:applicationContext.xml</param-value>
  </context-param>
  <listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>


strustConfig.xml
<action attribute="" input="/.jsp"
			name="Form" path="/" scope="request"
			type="org.springframework.web.struts.DelegatingActionProxy">
			<forward name="" path="/.do?doType=all" />
		</action>

<!-- Spring插件 -->
	<plug-in className="org.springframework.web.struts.ContextLoaderPlugIn">
		<set-property property="contextConfigLocation" value="classpath:applicationContext.xml" />
	</plug-in>


applicationContext.xml
        <!-- 注入GenericDAO -->
	<bean id="genericdao" class="com.topzh.persists.daoimpl.GenericDaoImpl">
		<property name="sessionFactory">
			<ref bean="SessionFactory" />
		</property>
	</bean>
	<!-- 注入Biz -->
	<bean id="biz" class="bizimpl.BizImpl">
		<constructor-arg>
			<ref bean="genericdao" />
		</constructor-arg>
	</bean>


BizImpl.class
        GenericDao dao;

	public BizImpl(GenericDao dao) {
		this.dao = dao;
	}

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics