Hibernate is usually three kinds: hql query, QBC query and QBE query:

There are usually three types of Hibernate commonly used: hql query, QBC query and QBE query:
1. QBE (Qurey By Example) retrieval method
QBE is the simplest, but the function is also the weakest. The function of QBE is not particularly powerful, only in Useful in some situations. A typical use case is to let the user enter a series of query conditions in the query window, and then return the matching objects. QBE only supports = and like comparison operators, and cannot match large interval values ​​and their or. In this case, the HQL retrieval method or the QBC retrieval method is still used.
Java code  
  
public Pager findPageByExample(int pageNo, int pageSize, Object object)   
{   
    Pager pager = null;   
    try  
    {   
        Criteria criteria = this.getSession().createCriteria(   
                Class.forName(this.getEntity()));   
  
        if (object ! = null)   
        {   
            criteria.add(Example.create(object).enableLike());   
        }   
  
        // Get the total number of rows for paging query based on criteria   
        int rowCount = (Integer) criteria.setProjection(   
                Projections.rowCount()).uniqueResult();   
        criteria.setProjection(null);   
  
        criteria.setFirstResult((pageNo - 1) * pageSize);   
        criteria.setMaxResults(pageSize);   
  
        List result = criteria.list();   
  
        pager = new Pager(pageSize, pageNo, rowCount, result);   
  
    } catch (RuntimeException re)   
    {   
        throw re;   
    } finally  
    {   
        return pager;   
    }   
  
}  


Pay attention to the 20th line of the code, namely criterion.add(Example.create(object).enableLike()); In this line, you need to call the .enableLike() method from Example.create(object), otherwise the query cannot be fuzzy.
String the columns that need fuzzy query with "%%" at the BO layer, otherwise it will still be the same as "=".
BO layer code:
Java code  
  
public Pager getInfoByQuery(int pageNo, int pageSize, String mendName,   
        String specialty, String post)   
{   
  
    EicMend eicMend = new EicMend();   
    if (mendName != null && mendName.length() > 0)   
    {   
        eicMend.setMendname("%" + mendName + "%");   
    }   
    if (specialty != null && specialty.length() > 0)   
    {   
        eicMend.setSpecialty(specialty);   
    }   
    if (post != null && post.length() > 0)   
    {   
        eicMend.setPost(post);   
    }   
  
    Pager pager = erpManagerDao   
            .findPageByExample(pageNo, pageSize, eicMend);   
    return pager;   
}  


Execute the SQL statement as follows:
Sql code  
Hibernate : select count(*) as y0_ from YJZX.EIC_MEND this_ where    
(this_.MENDNAME like ? and this_.POST like ?)   
  
Hibernate: select * from ( select this_.MENDID as MENDID23_0_,  …   
this_.EXPERTREMARK as EXPERTR28_23_0_ from YJZX. EIC_MEND this_ where    
(this_.MENDNAME like ? and this_.POST like ?) ) where rownum <= ?  


So just link the columns that need fuzzy query with "%%".


2. When the HQL retrieval method is adopted in the QBC (Qurey By Criteria) retrieval method
       , the HQL query statement based on the string form needs to be defined in the application program. The QBC API provides another way to retrieve objects. It mainly consists of the Criteria interface, the Criterion interface and the Restrictions interface. It supports dynamic generation of query statements at runtime. There are two common ways to pass parameters: one is to use map to pass parameters, and the other is to use Criterion...indefinite parameters to pass parameters.
An example of Map parameter passing is as follows:
DAO layer:
Java code  
  
public Pager findPageByCriteria(int pageNo, int pageSize, Map map)   
{   
    Pager pager = null;   
    try  
    {   
        Criteria criteria = this.getSession().createCriteria(   
                Class.forName(this. getEntity()));   
  
        if (map != null)   
        {   
            Set<String> keys = map.keySet();   
            for (String key : keys)   
            {   
                criteria.add(Restrictions.like(key, map.get(key)));   
            }   
        }   
  
        // 获取根据条件分页查询的总行数   
        int rowCount = (Integer) criteria.setProjection(   
                Projections.rowCount()).uniqueResult();   
        criteria.setProjection(null);   
  
        criteria.setFirstResult((pageNo - 1) * pageSize);   
        criteria.setMaxResults(pageSize);   
  
        List result = criteria.list();   
  
        pager = new Pager(pageSize, pageNo, rowCount, result);   
  
    } catch (RuntimeException re)   
    {   
        throw re;   
    } finally  
    {   
        return pager;   
    }   
  
}  


Map传参方式对应BO层代码:
Java代码  
  
public Pager getInfoByQuery2(int pageNo, int pageSize, String mendName,   
        String specialty, String post)   
{   
  
    Map map = new HashMap();   
  
    if (mendName != null && mendName.length() > 0)   
    {   
        map.put("mendname", "%" + mendName + "%");   
    }   
    if (specialty != null && specialty.length() > 0)   
    {   
        map.put("specialty", specialty);   
    }   
    if (post != null && post.length() >0)   
    {   
        map.put("post", post);   
    }   
  
    Pager pager = erpManagerDao.findPageByCriteria(pageNo, pageSize, map);   
    return pager;   
}  


The second way: Criterion...indefinite parameter passing method. Its code is as follows:
DAO layer code:
Java code  
  
public Pager findPageByCriteria(int pageNo, int pageSize,   
        Criterion... criterias)   
{   
    Pager pager = null;   
    try  
    {   
        Criteria criteria = this.getSession().createCriteria(   
                Class.forName (this.getEntity()));   
        if (criterions != null)   
        {   
            for (Criterion criterion : criterias)   
            {   
                if (criterion != null)   
                {   
                    criteria.add(criterion);   
                }   
  
            }   
        }   
  
        // 获取根据条件分页查询的总行数   
        int rowCount = (Integer) criteria.setProjection(   
                Projections.rowCount()).uniqueResult();   
        criteria.setProjection(null);   
  
        criteria.setFirstResult((pageNo - 1) * pageSize);   
        criteria.setMaxResults(pageSize);   
  
        List result = criteria.list();   
  
        pager = new Pager(pageSize, pageNo, rowCount, result);   
  
    } catch (RuntimeException re)   
    {   
        throw re;   
    } finally  
    {   
        return pager;   
    }   
  
}  


Criterion…不定参数传参方式对应BO层代码:
Java代码  
  
public Pager getInfoByQuery3(int pageNo, int pageSize, String mendName,   
        String specialty, String post)   
{   
    Criterion criterion1 = null, criterion2 = null, criterion3 = null;   
    if (mendName != null && mendName.length() > 0)   
    {   
        criterion1 = Restrictions.ilike("mendname", mendName,   
                MatchMode.ANYWHERE);   
    }   
  
    if (specialty != null && specialty.length() > 0)   
    {   
        criterion2 = Restrictions.ilike("specialty", specialty,   
                MatchMode.EXACT);   
    }   
  
    if (post != null && post.length() > 0)   
    {   
        criterion3 = Restrictions.ilike("post", post, MatchMode.EXACT );   
    }   
  
    Pager pager = erpManagerDao.findPageByCriteria(pageNo, pageSize,   
            criterion1, criterion2, criterion3);   
  
    return pager;   
}  


3. HQL retrieval method


HQL (Hibernate Query Language) is an object-oriented query language, which is somewhat familiar with SQL query language . Among the various retrieval methods provided by Hibernate, HQL is the most widely used retrieval method.
Use the Query interface to paginate the DAO code:
Java code  
  
public List<Object> findPageByQuery(int pageNo, int pageSize, String hql,   
        Map map)   
{   
    List<Object> result = null;   
    try  
    {   
        Query query = this.getSession().createQuery(hql);   
  
        Iterator it = map.keySet().iterator();   
        while (it.hasNext())   
        {   
            Object key = it.next();   
            query.setParameter(key.toString(), map.get(key));   
        }   
  
        query.setFirstResult((pageNo - 1) * pageSize);   
        query.setMaxResults(pageSize);   
  
        result = query.list();   
  
    } catch (RuntimeException re)   
    {   
        throw re;   
    }   
    return result;   
}  


查询所有记录数的DAO代码:
Java代码  
  
public int getTotalCount(String hql, Map map)   
{   
    try  
    {   
        Query query = this.getSession().createQuery(hql);   
  
        Iterator it = map.keySet().iterator();   
        while (it.hasNext())   
        {   
            Object key = it.next();   
            query.setParameter(key.toString(), map.get(key));   
        }   
  
        Integer i = (Integer) query.list().get(0);   
        return i;   
    } catch (RuntimeException re)   
    {   
        throw re;   
    }   
  
}  


BO层代码:
Java代码  
  
public Pager getInfoByQuery(int pageNo, int pageSize, String expertName,   
        String expertSpecialty, String post)   
{   
    StringBuffer hql = new StringBuffer();   
    hql.append("select count(expertid) from EicExpert where 1=1 ");   
  
    Map map = new HashMap();   
  
    if (expertName != null && expertName.length() > 0)   
    {   
        map.put("expertname", "%" + expertName + "%");   
        hql.append("and expertname like :expertname ");   
    }   
    if (expertSpecialty != null && expertSpecialty.length() > 0)   
    {   
        map.put("expertspecialty", expertSpecialty);   
        hql.append("and expertspecialty like :expertspecialty ");   
    }   
    if (post != null && post.length() > 0)   
    {   
        map.put("post", post);   
        hql.append("and post like :post ");   
    }   
  
    String queryHql = hql.substring(22);   
    List result = erpManagerDao.findPageByQuery(pageNo, pageSize,   
            queryHql, map);   
    int rowCount = erpManagerDao.getTotalCount(hql.toString(), map);   
  
    Pager pager = new Pager(pageSize, pageNo, rowCount, result);   
    return pager;   

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325812269&siteId=291194637