QBC 全称:Query By Criteria
HQL 全称:hibernate Query Language
HQL优点:与sql相近,可读性好,功能强大,效率高。
HQL缺点:字符串形式,只有在运行时才被解析,扩展性差。
QBC优点:提供面向对象的接口,编译时就可被解析,便于排错调试,扩展性好,允许用户扩展Criteria接口.
QBC缺点:可读性差,功能没有HQL强大,不支持报表查询和子查询。
三:
hibernate 查询match mode的四种模式
MatchMode.START:字符串在最前面的位置.相当于"like 'key%'"
MatchMode.END:字符串在最后面的位置.相当于"like '%key'"
MatchMode.ANYWHERE:字符串在中间匹配.相当于"like '%key%'"
MatchMode.EXACT:字符串精确匹配.相当于"like 'key'"
三:通常使用的Hibernate通常是三种:hql查询,QBC查询和QBE查询:
1、QBE(Qurey By Example)检索方式QBE是最简单的,但是功能也是最弱的,QBE的功能不是特别强大,仅在某些场合下有用。一个典型的使用场合就是在查询窗口中让用户输入一系列的查询条件,然后返回匹配的对象。QBE只支持=和like比较运算符,无法不大区间值,及其或的匹配。在这种情况下,还是采用HQL检索方式或QBC检索方式。
Java代码
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());
}
// 获取根据条件分页查询的总行数
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;
}
}
注意代码的第20行,即criteria.add(Example.create(object).enableLike());这一行,需将Example.create(object)调用.enableLike()方法,不然不能模糊查询。
在BO层将需要模糊查询的列用"%%"串起来,不然仍然和"="一样。
BO层代码:
Java代码
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;
}
执行SQL语句如下:
Sql代码
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 <= ?
所以只需将需模糊查询的列用“%%”链接即可。
2、QBC(Qurey By Criteria)检索方式
采用HQL检索方式时,在应用程序中需要定义基于字符串形式的HQL查询语句。QBC API提供了检索对象的另一种方式,它主要由Criteria接口、Criterion接口和Restrictions接口组成,它支持在运行时动态生成查询语句。比较常见的是两种传参方式:一种是用map传参,另一种是用Criterion…不定参数传参。
Map传参方式范例如下:
DAO层:
Java代码
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;
}
第二种方式:Criterion…不定参数传参方式。其代码如下所示:
DAO层代码:
Java代码
public Pager findPageByCriteria(int pageNo, int pageSize,
Criterion... criterions)
{
Pager pager = null;
try
{
Criteria criteria = this.getSession().createCriteria(
Class.forName(this.getEntity()));
if (criterions != null)
{
for (Criterion criterion : criterions)
{
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检索方式
HQL(Hibernate Query Language)是面向对象的查询语言,它和SQL查询语言有些相识。在Hibernate提供的各种检索方式中,HQL是使用最广的一种检索方式。
使用Query接口分页查询DAO代码:
Java代码
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;
}