SSH BaseDao代码

/*********************************************** * 基础DAO,所有的方法都在此实现 * * @author chen.lin * @version 1.0 ************************************************/
@SuppressWarnings("all")
//@Repository(value="baseDAO")
public class BaseDAO<T> extends HibernateDaoSupport implements IBaseDAO<T> {

    private Class<T> clazz;

    private Class<T> getClazz(){
        if (clazz == null) {//获取泛型的Class对象
            clazz = ((Class<T>) (((ParameterizedType)(this.getClass().getGenericSuperclass())).getActualTypeArguments()[0]));
        }
        return clazz;
    }

    @Resource(name="sessionFactory")
    public void setSuperSessionFactory(SessionFactory sessionFactory){
        super.setSessionFactory(sessionFactory);
    }

    @Override
    public void add(T t) {
        this.getHibernateTemplate().save(t);
    }

    @Override
    public void del(Serializable id) {
        T t = load(id);
        this.getHibernateTemplate().delete(t);
    }

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

    @Override
    public T load(Serializable id) {
        return this.getHibernateTemplate().load(getClazz(), id);
    }

    @Override
    public T get(Serializable id) {
        return this.getHibernateTemplate().get(getClazz(), id);
    }


    @Override
    public List<T> list(String hql, Object... objs) {
        Query query = getQuery(hql, objs);
        return query.list();
    }

    @Override
    public List<T> list(String hql) {
        return list(hql,null);
    }

    /** * 获得hibernate查询query * @param hql * @param objs * @return */
    private Query getQuery(String hql, Object... objs){
        Query query = this.getSession().createQuery(hql);
        if (objs != null && objs.length > 0) {
            for (int i = 0; i < objs.length; i++) {
                query.setParameter(i, objs[i]); 
            }
        }
        return query;
    }

    /** * 获得查询总数的hql * @param hql * @return */
    private String getCountHQL(String hql){
        String s = hql.substring(0,hql.indexOf("from"));
        if(s==null||"".equals(s.trim())) {
            hql = "select count(*) "+hql;
        } else {
            hql = hql.replace(s, "select count(*) ");
        }
        hql = hql.replace("fetch", "");
        if (hql.contains("order")) {
            hql = hql.substring(0, hql.indexOf("order"));
        }
        System.out.println("hql----------->" + hql);
        return hql;
    }

    @Override
    public Pager<T> find(String hql, Object... objs) {
        Integer pageSize = SystemContext.getPageSize();
        Integer pageOffset = SystemContext.getPageOffset();
        if(pageSize<=0) pageSize = 10;
        if(pageOffset<0) pageOffset = 0;
        Query q = getQuery(hql, objs);
        q.setFirstResult(pageOffset).setMaxResults(pageSize);
        String cHql = getCountHQL(hql);
        Query cq = getQuery(cHql, objs);
        Pager<T> pager = new Pager<T>();
        pager.setPageOffset(pageOffset);
        pager.setPageSize(pageSize);

        List<T> datas = q.list();
        pager.setDatas(datas);
        long totalRecord = (Long)cq.uniqueResult();
        System.out.println("totalRecord--->" + totalRecord);
        pager.setTotalRecord(Integer.parseInt(totalRecord + ""));
        pager.setTotalPage(Integer.parseInt(((totalRecord + pageSize - 1) / pageSize) + ""));
        return pager;
    }

    @Override
    public Pager<T> find(String hql) {
        return this.find(hql,null);
    }

    @Override
    public Pager<Object> findObj(String hql, Object... objs) {
        Integer pageSize = SystemContext.getPageSize();
        Integer pageOffset = SystemContext.getPageOffset();
        if(pageSize<=0) pageSize = 10;
        if(pageOffset<0) pageOffset = 0;
        Query q = getQuery(hql, objs);
        q.setFirstResult(pageOffset).setMaxResults(pageSize);
        String cHql = getCountHQL(hql);
        Query cq = getQuery(cHql, objs);
        Pager<Object> pager = new Pager<Object>();
        pager.setPageOffset(pageOffset);
        pager.setPageSize(pageSize);
        List<Object> datas = q.list();
        pager.setDatas(datas);
        long totalRecord = (Long)cq.uniqueResult();
        System.out.println("totalRecord--->" + totalRecord);
        pager.setTotalRecord(Integer.parseInt(totalRecord + ""));
        pager.setTotalPage(Integer.parseInt(((totalRecord + pageSize - 1) / pageSize) + ""));
        return pager;
    }

    @Override
    public Pager<Object> findObj(String hql) {
        return this.findObj(hql,null);
    }

    @Override
    public Object queryByHql(String hql, Object... objs) {
        Query query = getQuery(hql, objs);
        List list = query.list();
        if (list != null && list.size() > 0) {
            return list.get(0);
        }
        return null;
        //return this.getHibernateTemplate().find(hql, objs).get(0);
    }

    @Override
    public Object queryByHql(String hql) {
        return queryByHql(hql,null);
    }

    @Override
    public void executeByHql(String hql, Object... objs) {
        getQuery(hql, objs).executeUpdate();
    }

    @Override
    public void executeByHql(String hql) {
        executeByHql(hql,null);
    }

    @Override
    public List<Object> findALL(String hql, Object... objs) {
        return this.getHibernateTemplate().find(hql, objs);
    }

    @Override
    public List<Object> findALL(String hql) {
        return findALL(hql,null);
    }

    @Override
    public void addObj(Object obj) {
        this.getHibernateTemplate().save(obj);
    }

    @Override
    public void updateObj(Object obj) {
        this.getHibernateTemplate().update(obj);
    }

    @Override
    public void del(T t) {
        this.getHibernateTemplate().delete(t);
    }

    @Override
    public List<Object> listByObj(String hql, Object... objs) {
        return this.getHibernateTemplate().find(hql, objs);
    }



}

你可能感兴趣的:(ssh,baseDao)