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<T> {
    protected Logger logger = LoggerFactory.getLogger(this.getClass());
    protected Class<T> 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<T> 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<String, Object> 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<String, Object> 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<String, Object> 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<String, Object> 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<String, Object> params) {
        Query query = this.hibernateTemplate.getSessionFactory().getCurrentSession().createQuery(hql);
        this.setParameter(query, params);
        query.setFirstResult(first);
        query.setMaxResults(max);
        return query.list();
    }
    public List<Map> 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<Map<String, String>> listMap = query.list();
        List<Map> newListMap = new ArrayList();
        Iterator var8 = listMap.iterator();

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

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

            newListMap.add(newMap);
        }

        return newListMap;
    }
    public List<Map> 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<Map> 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<Map<String, String>> listMap = query.list();
        List<Map> newListMap = new ArrayList();
        Iterator var14 = listMap.iterator();

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

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

            newListMap.add(newMap);
        }

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

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

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

            newListMap.add(newMap);
        }

        return newListMap;
    }
    public List<Map> findBySqlAndParamsNoChange(String sql, Map<String, Object> 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<Map> findBySqlAndParams(String dialect, String driverClass, String connection_url, String username, String password, String show_sql, String schema, String sql, Map<String, Object> 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<Map<String, String>> listMap = query.list();
        List<Map> newListMap = new ArrayList();
        Iterator var14 = listMap.iterator();

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

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

            newListMap.add(newMap);
        }

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

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

            while(var10.hasNext()) {
                Entry<String, String> 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<Map> 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<Map<String, String>> listMap = query.list();
        List<Map> newListMap = new ArrayList();
        Iterator var9 = listMap.iterator();

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

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

            newListMap.add(newMap);
        }

        return newListMap;
    }
    public List<Map> 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<Map<String, String>> listMap = query.list();
        List<Map> newListMap = new ArrayList();
        Iterator var16 = listMap.iterator();

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

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

            newListMap.add(newMap);
        }

        return newListMap;
    }
    public List<T> 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<Map<String, String>> listMap = query.list();
        List<Map> newListMap = new ArrayList();
        Iterator var9 = listMap.iterator();

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

            while(var12.hasNext()) {
                Entry<String, String> 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<Map> findBySqlAndPageAndParams(String sql, int first, int max, Map<String, Object> 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<Map<String, String>> listMap = query.list();
        List<Map> newListMap = new ArrayList();
        Iterator var9 = listMap.iterator();

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

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

            newListMap.add(newMap);
        }

        return newListMap;
    }
    public List<Map> findBySqlAndPageAndParams(String dialect, String driverClass, String connection_url, String username, String password, String show_sql, String schema, String sql, int first, int max, Map<String, Object> 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<Map<String, String>> listMap = query.list();
        List<Map> newListMap = new ArrayList();
        Iterator var16 = listMap.iterator();

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

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

            newListMap.add(newMap);
        }

        return newListMap;
    }
    public List<T> findBySqlAndPageAndParamsToObjls(String sql, int first, int max, Map<String, Object> 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<Map<String, String>> listMap = query.list();
        List<Map> newListMap = new ArrayList();
        Iterator var9 = listMap.iterator();

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

            while(var12.hasNext()) {
                Entry<String, String> 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<String, Object> 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<String, Object> 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<String, Object> 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<String, Object> 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<String, Object> 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<String, Object> 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<String, Object> map) {
        if (map != null) {
            Set<String> 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<String, Object> 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<Map> 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<Map> 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<String, Object> 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<Map> 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<String, Object> 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<Map> 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;
        }
    }
}

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 214,313评论 6 496
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 91,369评论 3 389
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 159,916评论 0 349
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 57,333评论 1 288
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 66,425评论 6 386
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 50,481评论 1 292
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 39,491评论 3 412
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 38,268评论 0 269
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 44,719评论 1 307
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 37,004评论 2 328
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 39,179评论 1 342
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 34,832评论 4 337
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 40,510评论 3 322
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 31,153评论 0 21
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 32,402评论 1 268
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 47,045评论 2 365
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 44,071评论 2 352

推荐阅读更多精彩内容