package cn.itsource.crm.dao;

import java.io.Serializable;
import java.sql.SQLException;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

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

import cn.itsource.crm.query.BaseQuery;
import cn.itsource.crm.utils.PageResult;

public class BaseDao<T> extends HibernateDaoSupport {
	public void save(T t) {
		getHibernateTemplate().save(t);
	}

	public void update(T t) {
		getHibernateTemplate().update(t);
	}

	/**
	 * 
	 * @param Serializable
	 *            id:Hibernate主键类型只要实现Serializable接口就可以使用 Long,String
	 */
	public void delete(Class<T> entityClass, Serializable id) {
		T t = get(entityClass, id);
		if (t != null)
			getHibernateTemplate().delete(t);
	}

	public T get(Class<T> entityClass, Serializable id) {
		return getHibernateTemplate().get(entityClass, id);
	}

	public List<T> getAll(Class<T> entityClass) {
		return getHibernateTemplate().loadAll(entityClass);
		// return getHibernateTemplate().find("from " + entityClass.getName());
	}

	public PageResult<T> findPageResult(final BaseQuery baseQuery) {
		// System.out.println("hql:" + baseQuery.getHql());
		// System.out.println("countHql:" + baseQuery.getCountHql());
		// System.out.println("param:" + baseQuery.getParamList());

		// 获取到原生Hibernate.Session对象做查询
		// 单例是否对应一个请求
		// getSession();// 单例
		// getSessionFactory().openSession();// 多例
		// getSessionFactory().getCurrentSession();// 单例
		// 上面获取的session都必须自己关闭,都不用

		// 下面都会自动关闭session.由spring关闭的
		// getHibernateTemplate().executeFind(action)// 多例
		// getHibernateTemplate().executeWithNewSession(action)// 多例

		/******************** 必须使用Native的 *********************/
		// getHibernateTemplate().executeWithNativeSession(action)// 单例

		// 1.count查询
		Long count = getHibernateTemplate().executeWithNativeSession(
				new HibernateCallback<Long>() {
					// 由spring把session注入进来
					public Long doInHibernate(Session session)
							throws HibernateException, SQLException {
						// counthql
						Query query = session.createQuery(baseQuery
								.getCountHql());
						// 加入条件
						builderParam(query, baseQuery.getParamList());
						return (Long) query.uniqueResult();
					}
				});
		if (count.intValue() == 0) {
			return new PageResult<T>();
		}

		int currentPage = baseQuery.getCurrentPage();
		int pageSize = baseQuery.getPageSize();
		int totalCount = count.intValue();
		final PageResult<T> pageResult = new PageResult<T>(currentPage,
				pageSize, totalCount);
		// 2.*查询
		List<T> rows = getHibernateTemplate().executeWithNativeSession(
				new HibernateCallback<List<T>>() {
					public List<T> doInHibernate(Session session)
							throws HibernateException, SQLException {
						// hql
						Query query = session.createQuery(baseQuery.getHql());
						// 加入条件
						builderParam(query, baseQuery.getParamList());
						// 分页:使用经过处理后的变量
						int first = (pageResult.getCurrentPage() - 1)
								* pageResult.getPageSize();
						int max = pageResult.getPageSize();
						query.setFirstResult(first).setMaxResults(max);
						return query.list();
					}
				});
		pageResult.setRows(rows);
		return pageResult;
	}

	private void builderParam(Query query, List paramList) {
		int index = 0;
		for (Object object : paramList) {
			query.setParameter(index++, object);
		}
	}

	// select count(o) from Employee o where o.name=?
	// select o.name from Employee o where o.name=?
	// select o from Employee o where o.name=?
	// select o.name,o.age from Employee o where o.name=?
	public List findByHql(String hql, Object... objects) {
		System.out.println("hql:" + hql);
		System.out.println("objects:" + Arrays.toString(objects));
		return getHibernateTemplate().find(hql, objects);
	}

	// 根据sql查询结果
	// 目前主要做报表支撑
	public List<Map> findMap(String hql, Object... params){
		return getHibernateTemplate().find(hql, params);
	}

}
最近下载更多
彭斌java  LV6 2022年12月2日
wanglinddad  LV55 2022年4月20日
wangyang520  LV6 2022年4月17日
487948123  LV8 2022年4月13日
crofsun  LV8 2022年4月5日
7z7z7z  LV5 2022年3月21日
lywang  LV7 2021年12月12日
wuhaigang  LV9 2021年10月20日
a1323617295  LV5 2020年12月17日
kook_82  LV9 2020年12月1日
最近浏览更多
cq1458824624 1月14日
暂无贡献等级
571818771  LV3 2024年12月17日
xxyylu  LV2 2024年12月4日
暂无贡献等级
quartz  LV8 2024年7月1日
y_x_happy  LV4 2024年6月25日
sunlea  LV20 2024年5月22日
李俊雄  LV3 2024年4月30日
爱丽淇  LV5 2024年3月18日
穿山甲1001  LV6 2023年12月16日
顶部 客服 微信二维码 底部
>扫描二维码关注最代码为好友扫描二维码关注最代码为好友