Hibernate 封装baseDao 常用方法

//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//

package com.uniwin.webkey.core.common.dao;

import com.uniwin.common.component.model.DataTable;
import com.uniwin.common.util.BeanUtils;
import com.uniwin.common.util.DateUtil;
import com.uniwin.common.util.ListUtils;
import com.uniwin.common.util.StringUtil;
import com.uniwin.hibernate.DynamicHibernateConfiguration;
import com.uniwin.hibernate.DynamicSessionFactory;
import com.uniwin.webkey.core.common.model.BaseDeleteModel;
import java.io.Serializable;
import java.lang.reflect.Method;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.Map.Entry;
import org.apache.commons.lang.StringUtils;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.metadata.ClassMetadata;
import org.hibernate.transform.Transformers;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.orm.hibernate3.HibernateTemplate;

//public class BaseDAO {
    protected Logger logger = LoggerFactory.getLogger(this.getClass());
    protected Class entityClass;
    @Autowired
    protected HibernateTemplate hibernateTemplate;
    public Session session = null;

    public BaseDAO() {
        Type type = this.getClass().getGenericSuperclass();

        try {
            Type trueType = ((ParameterizedType)type).getActualTypeArguments()[0];
            this.entityClass = (Class)trueType;
        } catch (Exception var3) {
        }

    }
    public DynamicHibernateConfiguration setHibernateConfig(String dialect, String driverClass, String connection_url, String username, String password, String show_sql, String schema) {
        DynamicHibernateConfiguration configuration = null;
        if (schema != null && schema != "") {
            configuration = new DynamicHibernateConfiguration(dialect, driverClass, connection_url, username, password, show_sql, schema);
        } else {
            configuration = new DynamicHibernateConfiguration(dialect, driverClass, connection_url, username, password, show_sql);
        }

        return configuration;
    }
    public void reflashDbConnection() {
        this.session = DynamicSessionFactory.getSession();
    }
    public Session getSession() {
        return this.session;
    }
    public Session getTempSession() {
        return DynamicSessionFactory.getSession();
    }
    public void dynamicSqlOperateBefore(String dialect, String driverClass, String connection_url, String username, String password, String show_sql, String schema) {
        DynamicHibernateConfiguration configuration = this.setHibernateConfig(dialect, driverClass, connection_url, username, password, show_sql, schema);
        DynamicSessionFactory.reflashSessionFactory(configuration);
        this.session = DynamicSessionFactory.getSession();
    }
    public T get(Serializable id) {
        return this.hibernateTemplate.get(this.entityClass, id);
    }
    public T get(String id) {
        return StringUtils.isBlank(id) ? null : this.hibernateTemplate.get(this.entityClass, id);
    }
    public Object get(Class clz, Serializable id) {
        return this.hibernateTemplate.get(clz, id);
    }
    public Serializable save(Object entity) {
        return this.hibernateTemplate.save(entity);
    }
    public String saveAndLog(Object entity) {
        String result = BeanUtils.GetModelAttAddInfo(entity);
        this.hibernateTemplate.save(entity);
        return result;
    }
    public void delete(Object entity) {
        this.hibernateTemplate.delete(entity);
    }
    public void deleteById(Serializable id) {
        this.hibernateTemplate.delete(this.get(id));
    }
    public String deleteByIdAndLog(Serializable id) {
        T t = this.get(id);
        String log = BeanUtils.GetModelAttInfo(t);
        this.hibernateTemplate.delete(t);
        return log;
    }
    public void ldelete(Object entity) {
        if (entity instanceof BaseDeleteModel) {
            Method tm = null;

            try {
                tm = entity.getClass().getMethod("setIsDelete", String.class);
                tm.invoke(entity, "1");
                tm = entity.getClass().getMethod("setOperTime", String.class);
                tm.invoke(entity, DateUtil.getDateTimeString(new Date()));
                this.update(entity);
            } catch (Exception var4) {
                var4.printStackTrace();
            }
        }

    }
    public void ldeleteById(Serializable id) {
        T t = this.get(id);
        if (t instanceof BaseDeleteModel) {
            this.ldelete(t);
        }

    }
    public String ldeleteByIdAndLog(Serializable id) {
        String log = "";
        T t = this.get(id);
        if (t instanceof BaseDeleteModel) {
            log = BeanUtils.GetModelAttInfo(t);
            this.ldelete(t);
        }

        return log;
    }
    public void update(Object entity) {
        this.hibernateTemplate.update(entity);
    }
    public String updateAndLog(Object entity) {
        String result = "";
        String modelIdName = this.getModelIdName(entity);
        String idVal = (String)BeanUtils.invokeGetter(entity, modelIdName);
        if (StringUtil.isNotEmpty(idVal)) {
            Object oldObj = this.get(entity.getClass(), idVal);
            result = BeanUtils.GetModelAttChangeInfo(oldObj, entity);
            BeanUtils.copyToModel(entity, oldObj);
            this.hibernateTemplate.update(oldObj);
        }

        return result;
    }
    public void saveOrUpdate(Object entity) {
        this.hibernateTemplate.saveOrUpdate(entity);
    }
    public List findAll() {
        return this.hibernateTemplate.loadAll(this.entityClass);
    }
    public int count() {
        return this.findAll().size();
    }
    public int executeHql(String hql, Object... params) {
        return this.hibernateTemplate.bulkUpdate(hql, params);
    }
    public int executeHqlByParams(String hql, Map params) {
        Session session = this.hibernateTemplate.getSessionFactory().getCurrentSession();
        Query query = session.createQuery(hql);
        this.setParameter(query, params);
        return query.executeUpdate();
    }
    public int executeSql(String sql, Object... params) {
        Session session = this.hibernateTemplate.getSessionFactory().getCurrentSession();
        Query query = session.createSQLQuery(sql);

        for(int i = 0; i < params.length; ++i) {
            query.setParameter(i, params[i]);
        }

        return query.executeUpdate();
    }
    public int executeSql(String dialect, String driverClass, String connection_url, String username, String password, String show_sql, String schema, String sql, Object... params) {
        this.dynamicSqlOperateBefore(dialect, driverClass, connection_url, username, password, show_sql, schema);
        this.session.beginTransaction();
        Query query = this.session.createSQLQuery(sql);

        int iNum;
        for(iNum = 0; iNum < params.length; ++iNum) {
            query.setParameter(iNum, params[iNum]);
        }

        iNum = query.executeUpdate();
        this.session.getTransaction().commit();
        return iNum;
    }
    public int executeSqlByParams(String sql, Map params) {
        Session session = this.hibernateTemplate.getSessionFactory().getCurrentSession();
        Query query = session.createSQLQuery(sql);
        this.setParameter(query, (Map)params);
        return query.executeUpdate();
    }
    public int executeSqlByParams(String dialect, String driverClass, String connection_url, String username, String password, String show_sql, String schema, String sql, Map params) {
        this.dynamicSqlOperateBefore(dialect, driverClass, connection_url, username, password, show_sql, schema);
        this.session.beginTransaction();
        Query query = this.session.createSQLQuery(sql);
        this.setParameter(query, (Map)params);
        int iNum = query.executeUpdate();
        this.session.getTransaction().commit();
        return iNum;
    }
    public int insertBySQL(String sTableName, Map mData) {
        Session session = this.hibernateTemplate.getSessionFactory().getCurrentSession();
        String strSql = this.getInsertSQL(sTableName, mData);
        Query query = session.createSQLQuery(strSql);
        return query.executeUpdate();
    }
    public int updateBySQL(String sTableName, Map mData, String sCondition) {
        Session session = this.hibernateTemplate.getSessionFactory().getCurrentSession();
        String strSql = this.getUpdateSQL(sTableName, mData, sCondition);
        Query query = session.createSQLQuery(strSql);
        return query.executeUpdate();
    }
    public List find(String hql, Object... params) {
        return this.hibernateTemplate.find(hql, params);
    }
    public List findByParams(String hql, Map params) {
        Query query = this.hibernateTemplate.getSessionFactory().getCurrentSession().createQuery(hql);
        this.setParameter(query, params);
        return query.list();
    }
    public List findByPage(String hql, int first, int max, Object... params) {
        Query query = this.hibernateTemplate.getSessionFactory().getCurrentSession().createQuery(hql);

        for(int i = 0; i < params.length; ++i) {
            query.setParameter(i, params[i]);
        }

        query.setFirstResult(first);
        query.setMaxResults(max);
        return query.list();
    }
    public List findByPageAndParams(String hql, int first, int max, Map params) {
        Query query = this.hibernateTemplate.getSessionFactory().getCurrentSession().createQuery(hql);
        this.setParameter(query, params);
        query.setFirstResult(first);
        query.setMaxResults(max);
        return query.list();
    }
    public List findBySql(String sql, Object... params) {
        Session session = this.hibernateTemplate.getSessionFactory().getCurrentSession();
        Query query = session.createSQLQuery(sql);

        for(int i = 0; i < params.length; ++i) {
            query.setParameter(i, params[i]);
        }

        query.setResultTransformer(Transformers.ALIAS_TO_ENTITY_MAP);
        List> listMap = query.list();
        List newListMap = new ArrayList();
        Iterator var8 = listMap.iterator();

        while(var8.hasNext()) {
            Map mp = (Map)var8.next();
            Map newMap = new HashMap();
            Iterator var11 = mp.entrySet().iterator();

            while(var11.hasNext()) {
                Entry entry = (Entry)var11.next();
                newMap.put(((String)entry.getKey()).toUpperCase(), entry.getValue());
            }

            newListMap.add(newMap);
        }

        return newListMap;
    }
    public List findBySqlNoChange(String sql, Object... params) {
        Session session = this.hibernateTemplate.getSessionFactory().getCurrentSession();
        Query query = session.createSQLQuery(sql);

        for(int i = 0; i < params.length; ++i) {
            query.setParameter(i, params[i]);
        }

        query.setResultTransformer(Transformers.ALIAS_TO_ENTITY_MAP);
        return query.list();
    }
    public List findBySql(String dialect, String driverClass, String connection_url, String username, String password, String show_sql, String schema, String sql, Object... params) {
        this.dynamicSqlOperateBefore(dialect, driverClass, connection_url, username, password, show_sql, schema);
        Query query = this.session.createSQLQuery(sql);

        for(int i = 0; i < params.length; ++i) {
            query.setParameter(i, params[i]);
        }

        query.setResultTransformer(Transformers.ALIAS_TO_ENTITY_MAP);
        List> listMap = query.list();
        List newListMap = new ArrayList();
        Iterator var14 = listMap.iterator();

        while(var14.hasNext()) {
            Map mp = (Map)var14.next();
            Map newMap = new HashMap();
            Iterator var17 = mp.entrySet().iterator();

            while(var17.hasNext()) {
                Entry entry = (Entry)var17.next();
                newMap.put(((String)entry.getKey()).toUpperCase(), entry.getValue());
            }

            newListMap.add(newMap);
        }

        return newListMap;
    }
    public List findBySqlAndParams(String sql, Map params) {
        Query query = this.hibernateTemplate.getSessionFactory().getCurrentSession().createSQLQuery(sql);
        this.setParameter(query, (Map)params);
        query.setResultTransformer(Transformers.ALIAS_TO_ENTITY_MAP);
        List> listMap = query.list();
        List newListMap = new ArrayList();
        Iterator var7 = listMap.iterator();

        while(var7.hasNext()) {
            Map mp = (Map)var7.next();
            Map newMap = new HashMap();
            Iterator var10 = mp.entrySet().iterator();

            while(var10.hasNext()) {
                Entry entry = (Entry)var10.next();
                newMap.put(((String)entry.getKey()).toUpperCase(), entry.getValue());
            }

            newListMap.add(newMap);
        }

        return newListMap;
    }
    public List findBySqlAndParamsNoChange(String sql, Map params) {
        Query query = this.hibernateTemplate.getSessionFactory().getCurrentSession().createSQLQuery(sql);
        this.setParameter(query, (Map)params);
        query.setResultTransformer(Transformers.ALIAS_TO_ENTITY_MAP);
        return query.list();
    }
    public List findBySqlAndParams(String dialect, String driverClass, String connection_url, String username, String password, String show_sql, String schema, String sql, Map params) {
        this.dynamicSqlOperateBefore(dialect, driverClass, connection_url, username, password, show_sql, schema);
        Query query = this.session.createSQLQuery(sql);
        this.setParameter(query, (Map)params);
        query.setResultTransformer(Transformers.ALIAS_TO_ENTITY_MAP);
        List> listMap = query.list();
        List newListMap = new ArrayList();
        Iterator var14 = listMap.iterator();

        while(var14.hasNext()) {
            Map mp = (Map)var14.next();
            Map newMap = new HashMap();
            Iterator var17 = mp.entrySet().iterator();

            while(var17.hasNext()) {
                Entry entry = (Entry)var17.next();
                newMap.put(((String)entry.getKey()).toUpperCase(), entry.getValue());
            }

            newListMap.add(newMap);
        }

        return newListMap;
    }
    public List findBySqlAndParamsToObjls(String sql, Map params) throws Exception {
        Query query = this.hibernateTemplate.getSessionFactory().getCurrentSession().createSQLQuery(sql);
        this.setParameter(query, (Map)params);
        query.setResultTransformer(Transformers.ALIAS_TO_ENTITY_MAP);
        List> listMap = query.list();
        List newListMap = new ArrayList();
        Iterator var7 = listMap.iterator();

        while(var7.hasNext()) {
            Map mp = (Map)var7.next();
            Map newMap = new HashMap();
            Iterator var10 = mp.entrySet().iterator();

            while(var10.hasNext()) {
                Entry entry = (Entry)var10.next();
                newMap.put(((String)entry.getKey()).toUpperCase(), entry.getValue());
            }

            newListMap.add(newMap);
        }

        List ls = ListUtils.listmpToListobj(newListMap, this.entityClass);
        return ls;
    }
    public List findBySqlAndPage(String sql, int first, int max, Object... params) {
        Query query = this.hibernateTemplate.getSessionFactory().getCurrentSession().createSQLQuery(sql);

        for(int i = 0; i < params.length; ++i) {
            query.setParameter(i, params[i]);
        }

        query.setFirstResult(first);
        query.setMaxResults(max);
        query.setResultTransformer(Transformers.ALIAS_TO_ENTITY_MAP);
        List> listMap = query.list();
        List newListMap = new ArrayList();
        Iterator var9 = listMap.iterator();

        while(var9.hasNext()) {
            Map mp = (Map)var9.next();
            Map newMap = new HashMap();
            Iterator var12 = mp.entrySet().iterator();

            while(var12.hasNext()) {
                Entry entry = (Entry)var12.next();
                newMap.put(((String)entry.getKey()).toUpperCase(), entry.getValue());
            }

            newListMap.add(newMap);
        }

        return newListMap;
    }
    public List findBySqlAndPage(String dialect, String driverClass, String connection_url, String username, String password, String show_sql, String schema, String sql, int first, int max, Object... params) {
        this.dynamicSqlOperateBefore(dialect, driverClass, connection_url, username, password, show_sql, schema);
        Query query = this.session.createSQLQuery(sql);

        for(int i = 0; i < params.length; ++i) {
            query.setParameter(i, params[i]);
        }

        query.setFirstResult(first);
        query.setMaxResults(max);
        query.setResultTransformer(Transformers.ALIAS_TO_ENTITY_MAP);
        List> listMap = query.list();
        List newListMap = new ArrayList();
        Iterator var16 = listMap.iterator();

        while(var16.hasNext()) {
            Map mp = (Map)var16.next();
            Map newMap = new HashMap();
            Iterator var19 = mp.entrySet().iterator();

            while(var19.hasNext()) {
                Entry entry = (Entry)var19.next();
                newMap.put(((String)entry.getKey()).toUpperCase(), entry.getValue());
            }

            newListMap.add(newMap);
        }

        return newListMap;
    }
    public List findBySqlAndPageToObjls(String sql, int first, int max, Object... params) throws Exception {
        Query query = this.hibernateTemplate.getSessionFactory().getCurrentSession().createSQLQuery(sql);

        for(int i = 0; i < params.length; ++i) {
            query.setParameter(i, params[i]);
        }

        query.setFirstResult(first);
        query.setMaxResults(max);
        query.setResultTransformer(Transformers.ALIAS_TO_ENTITY_MAP);
        List> listMap = query.list();
        List newListMap = new ArrayList();
        Iterator var9 = listMap.iterator();

        while(var9.hasNext()) {
            Map mp = (Map)var9.next();
            Map newMap = new HashMap();
            Iterator var12 = mp.entrySet().iterator();

            while(var12.hasNext()) {
                Entry entry = (Entry)var12.next();
                newMap.put(((String)entry.getKey()).toUpperCase(), entry.getValue());
            }

            newListMap.add(newMap);
        }

        List ls = ListUtils.listmpToListobj(newListMap, this.entityClass);
        return ls;
    }
    public List findBySqlAndPageAndParams(String sql, int first, int max, Map params) {
        Query query = this.hibernateTemplate.getSessionFactory().getCurrentSession().createSQLQuery(sql);
        this.setParameter(query, (Map)params);
        query.setFirstResult(first);
        query.setMaxResults(max);
        query.setResultTransformer(Transformers.ALIAS_TO_ENTITY_MAP);
        List> listMap = query.list();
        List newListMap = new ArrayList();
        Iterator var9 = listMap.iterator();

        while(var9.hasNext()) {
            Map mp = (Map)var9.next();
            Map newMap = new HashMap();
            Iterator var12 = mp.entrySet().iterator();

            while(var12.hasNext()) {
                Entry entry = (Entry)var12.next();
                newMap.put(((String)entry.getKey()).toUpperCase(), entry.getValue());
            }

            newListMap.add(newMap);
        }

        return newListMap;
    }
    public List findBySqlAndPageAndParams(String dialect, String driverClass, String connection_url, String username, String password, String show_sql, String schema, String sql, int first, int max, Map params) {
        this.dynamicSqlOperateBefore(dialect, driverClass, connection_url, username, password, show_sql, schema);
        Query query = this.session.createSQLQuery(sql);
        this.setParameter(query, (Map)params);
        query.setFirstResult(first);
        query.setMaxResults(max);
        query.setResultTransformer(Transformers.ALIAS_TO_ENTITY_MAP);
        List> listMap = query.list();
        List newListMap = new ArrayList();
        Iterator var16 = listMap.iterator();

        while(var16.hasNext()) {
            Map mp = (Map)var16.next();
            Map newMap = new HashMap();
            Iterator var19 = mp.entrySet().iterator();

            while(var19.hasNext()) {
                Entry entry = (Entry)var19.next();
                newMap.put(((String)entry.getKey()).toUpperCase(), entry.getValue());
            }

            newListMap.add(newMap);
        }

        return newListMap;
    }
    public List findBySqlAndPageAndParamsToObjls(String sql, int first, int max, Map params) throws Exception {
        Query query = this.hibernateTemplate.getSessionFactory().getCurrentSession().createSQLQuery(sql);
        this.setParameter(query, (Map)params);
        query.setFirstResult(first);
        query.setMaxResults(max);
        query.setResultTransformer(Transformers.ALIAS_TO_ENTITY_MAP);
        List> listMap = query.list();
        List newListMap = new ArrayList();
        Iterator var9 = listMap.iterator();

        while(var9.hasNext()) {
            Map mp = (Map)var9.next();
            Map newMap = new HashMap();
            Iterator var12 = mp.entrySet().iterator();

            while(var12.hasNext()) {
                Entry entry = (Entry)var12.next();
                newMap.put(((String)entry.getKey()).toUpperCase(), entry.getValue());
            }

            newListMap.add(newMap);
        }

        List ls = ListUtils.listmpToListobj(newListMap, this.entityClass);
        return ls;
    }
    public int count(String hql, Object... params) {
        return Integer.parseInt(this.getUniqueResult(hql, params).toString());
    }
    public int countByParams(String hql, Map params) {
        return Integer.parseInt(this.getUniqueResultByParams(hql, params).toString());
    }
    public int countBySql(String sql, Object... params) {
        return Integer.parseInt(this.getUniqueResultBySql(sql, params).toString());
    }
    public int countBySql(String dialect, String driverClass, String connection_url, String username, String password, String show_sql, String schema, String sql, Object... params) {
        return Integer.parseInt(this.getUniqueResultBySql(dialect, driverClass, connection_url, username, password, show_sql, schema, sql, params).toString());
    }
    public int countBySqlAndParams(String sql, Map params) {
        return Integer.parseInt(this.getUniqueResultBySqlAndParams(sql, params).toString());
    }
    public int countBySqlAndParams(String dialect, String driverClass, String connection_url, String username, String password, String show_sql, String schema, String sql, Map params) {
        return Integer.parseInt(this.getUniqueResultBySqlAndParams(dialect, driverClass, connection_url, username, password, show_sql, schema, sql, params).toString());
    }
    public Object getUniqueResult(String hql, Object... params) {
        Query query = this.hibernateTemplate.getSessionFactory().getCurrentSession().createQuery(hql);
        this.setParameter(query, params);
        return query.uniqueResult();
    }
    public Object getUniqueResultByParams(String hql, Map params) {
        Query query = this.hibernateTemplate.getSessionFactory().getCurrentSession().createQuery(hql);
        this.setParameter(query, params);
        return query.uniqueResult();
    }
    public Object getUniqueResultBySql(String sql, Object... params) {
        Query query = this.hibernateTemplate.getSessionFactory().getCurrentSession().createSQLQuery(sql);

        for(int i = 0; i < params.length; ++i) {
            query.setParameter(i, params[i]);
        }

        return query.uniqueResult();
    }
    public Object getUniqueResultBySql(String dialect, String driverClass, String connection_url, String username, String password, String show_sql, String schema, String sql, Object... params) {
        this.dynamicSqlOperateBefore(dialect, driverClass, connection_url, username, password, show_sql, schema);
        Query query = this.session.createSQLQuery(sql);

        for(int i = 0; i < params.length; ++i) {
            query.setParameter(i, params[i]);
        }

        return query.uniqueResult();
    }
    private void setParameter(Query query, Object... params) {
        if (params.length == 1 && params[0] instanceof Collection) {
            List list = (List)params[0];

            for(int i = 0; i < list.size(); ++i) {
                query.setParameter(i, list.get(i));
            }
        } else {
            for(int i = 0; i < params.length; ++i) {
                query.setParameter(i, params[i]);
            }
        }

    }
    public Object getUniqueResultBySqlAndParams(String sql, Map params) {
        Query query = this.hibernateTemplate.getSessionFactory().getCurrentSession().createSQLQuery(sql);
        this.setParameter(query, (Map)params);
        return query.uniqueResult();
    }

    public Object getUniqueResultBySqlAndParams(String dialect, String driverClass, String connection_url, String username, String password, String show_sql, String schema, String sql, Map params) {
        this.dynamicSqlOperateBefore(dialect, driverClass, connection_url, username, password, show_sql, schema);
        Query query = this.session.createSQLQuery(sql);
        this.setParameter(query, (Map)params);
        return query.uniqueResult();
    }
    private Query setParameter(Query query, Map map) {
        if (map != null) {
            Set keySet = map.keySet();
            Iterator var5 = keySet.iterator();

            while(true) {
                while(true) {
                    while(var5.hasNext()) {
                        String string = (String)var5.next();
                        Object obj = map.get(string);
                        if (obj instanceof Collection) {
                            if (obj != null && ((Collection)obj).size() > 0) {
                                query.setParameterList(string, (Collection)obj);
                            } else {
                                query.setParameter(string, (Object)null);
                            }
                        } else if (obj instanceof Object[]) {
                            if (obj != null && ((Object[])obj).length > 0) {
                                query.setParameterList(string, (Object[])obj);
                            } else {
                                query.setParameter(string, (Object)null);
                            }
                        } else {
                            query.setParameter(string, obj);
                        }
                    }

                    return query;
                }
            }
        } else {
            return query;
        }
    }
    public DataTable findDT(String hql, DataTable dt, Object... params) {
        int first = dt.getiDisplayStart();
        int max = dt.getiDisplayLength();
        List result = this.findByPage(this.genOrderSql(hql, dt), first, max, params);
        hql = this.getCountSql(hql);
        List listCnt = this.find(hql, params);
        int total = Integer.parseInt(String.valueOf(listCnt.get(0)));
        dt.setAaData(result);
        dt.setiTotalRecords(total);
        dt.setiTotalDisplayRecords(total);
        return dt;
    }
    public DataTable findDTByParams(String hql, DataTable dt, Map params) {
        int first = dt.getiDisplayStart();
        int max = dt.getiDisplayLength();
        List result = this.findByPageAndParams(this.genOrderSql(hql, dt), first, max, params);
        hql = this.getCountSql(hql);
        List listCnt = this.findByParams(hql, params);
        int total = Integer.parseInt(String.valueOf(listCnt.get(0)));
        dt.setAaData(result);
        dt.setiTotalRecords(total);
        dt.setiTotalDisplayRecords(total);
        return dt;
    }
    public DataTable findDTBySql(String sql, DataTable dt, Object... params) {
        int first = dt.getiDisplayStart();
        int max = dt.getiDisplayLength();
        List result = this.findBySqlAndPage(this.genOrderSql(sql, dt), first, max, params);
        sql = this.getCountSql(sql);
        List listCnt = this.findBySql(sql, params);
        int total = Integer.parseInt(((Map)listCnt.get(0)).get("CNT").toString());
        dt.setAaData(result);
        dt.setiTotalRecords(total);
        dt.setiTotalDisplayRecords(total);
        return dt;
    }
    public DataTable findDTBySql(String dialect, String driverClass, String connection_url, String username, String password, String show_sql, String schema, String sql, DataTable dt, Object... params) {
        int first = dt.getiDisplayStart();
        int max = dt.getiDisplayLength();
        List result = this.findBySqlAndPage(dialect, driverClass, connection_url, username, password, show_sql, schema, this.genOrderSql(sql, dt), first, max, params);
        sql = this.getCountSql(sql);
        List listCnt = this.findBySql(dialect, driverClass, connection_url, username, password, show_sql, schema, sql, params);
        int total = Integer.parseInt(((Map)listCnt.get(0)).get("CNT").toString());
        dt.setAaData(result);
        dt.setiTotalRecords(total);
        dt.setiTotalDisplayRecords(total);
        return dt;
    }
    public DataTable findDTBySql(String sql, DataTable dt, Map params) {
        int first = dt.getiDisplayStart();
        int max = dt.getiDisplayLength();
        List result = this.findBySqlAndPageAndParams(this.genOrderSql(sql, dt), first, max, params);
        sql = this.getCountSql(sql);
        List listCnt = this.findBySqlAndParams(sql, params);
        int total = Integer.parseInt(((Map)listCnt.get(0)).get("CNT").toString());
        dt.setAaData(result);
        dt.setiTotalRecords(total);
        dt.setiTotalDisplayRecords(total);
        return dt;
    }
    public DataTable findDTBySql(String dialect, String driverClass, String connection_url, String username, String password, String show_sql, String schema, String sql, DataTable dt, Map params) {
        int first = dt.getiDisplayStart();
        int max = dt.getiDisplayLength();
        List result = this.findBySqlAndPageAndParams(dialect, driverClass, connection_url, username, password, show_sql, schema, this.genOrderSql(sql, dt), first, max, params);
        sql = this.getCountSql(sql);
        List listCnt = this.findBySqlAndParams(dialect, driverClass, connection_url, username, password, show_sql, schema, sql, params);
        int total = Integer.parseInt(((Map)listCnt.get(0)).get("CNT").toString());
        dt.setAaData(result);
        dt.setiTotalRecords(total);
        dt.setiTotalDisplayRecords(total);
        return dt;
    }
    private String genOrderSql(String sql, DataTable dt) {
        try {
            if (dt.getSortField() != null && !dt.getSortField().equals("")) {
                if (StringUtil.ignoreLastIndexOf(sql, " order") != -1 && StringUtil.ignoreLastIndexOf(sql, " order") > sql.lastIndexOf(")")) {
                    sql = sql.substring(0, StringUtil.ignoreLastIndexOf(sql, " order"));
                }

                sql = sql + " order by " + dt.getSortField() + " " + dt.getSortType();
            }
        } catch (Exception var4) {
            var4.printStackTrace();
        }

        return sql;
    }
    private String getCountSql(String sql) {
        sql = "select count(*) as cnt " + sql.substring(sql.indexOf("from") == -1 ? sql.indexOf("FROM") : sql.indexOf("from"));
        if (StringUtil.ignoreLastIndexOf(sql, " order") != -1 && StringUtil.ignoreLastIndexOf(sql, " order") > sql.lastIndexOf(")")) {
            sql = sql.substring(0, StringUtil.ignoreLastIndexOf(sql, " order"));
        }

        return sql;
    }
    private String getModelIdName(Object obj) {
        ClassMetadata meta = this.hibernateTemplate.getSessionFactory().getClassMetadata(obj.getClass());
        return meta.getIdentifierPropertyName();
    }
    private String getInsertSQL(String sTableName, Map mData) {
        if (mData.size() != 0 && sTableName != null && sTableName != "") {
            Iterator key = mData.keySet().iterator();
            StringBuffer sql1 = null;
            StringBuffer sql2 = null;
            String sTmp = null;

            while(true) {
                while(true) {
                    while(key.hasNext()) {
                        if (sTmp == null) {
                            sTmp = key.next().toString();
                            sql1 = new StringBuffer(sTmp);
                            if (mData.get(sTmp) != null && mData.get(sTmp) != "") {
                                sql2 = new StringBuffer(mData.get(sTmp).toString());
                            } else {
                                sql2 = new StringBuffer("null");
                            }
                        } else {
                            sTmp = key.next().toString();
                            sql1.append(",").append(sTmp);
                            if (mData.get(sTmp) != null && mData.get(sTmp) != "") {
                                sql2.append(",").append(mData.get(sTmp).toString());
                            } else {
                                sql2.append(",").append("null");
                            }
                        }
                    }

                    return "INSERT INTO " + sTableName + " (" + sql1 + ") VALUES (" + sql2 + ")";
                }
            }
        } else {
            return null;
        }
    }
    private String getUpdateSQL(String sTableName, Map mData, String sCondition) {
        if (mData.size() != 0 && sTableName != null && sTableName != "") {
            Iterator key = mData.keySet().iterator();
            StringBuffer sql = null;
            String sTmp = null;

            while(true) {
                while(true) {
                    while(key.hasNext()) {
                        if (sTmp == null) {
                            sTmp = key.next().toString();
                            sql = new StringBuffer(sTmp);
                            if (mData.get(sTmp) != null && mData.get(sTmp) != "") {
                                sql.append("=" + mData.get(sTmp).toString());
                            } else {
                                sql.append("=null");
                            }
                        } else {
                            sTmp = key.next().toString();
                            sql.append(",").append(sTmp);
                            if (mData.get(sTmp) != null && mData.get(sTmp) != "") {
                                sql.append("=").append(mData.get(sTmp).toString());
                            } else {
                                sql.append("=").append("null");
                            }
                        }
                    }

                    if (sCondition != null && sCondition != "") {
                        return "UPDATE " + sTableName + " SET " + sql + " WHERE " + sCondition;
                    }

                    return "UPDATE " + sTableName + " SET " + sql;
                }
            }
        } else {
            return null;
        }
    }
}

你可能感兴趣的:(Hibernate 封装baseDao 常用方法)