查询往往都是模糊查询的,hibernate对此做了很好的封装,首先介绍一下几个常用的类1.Criteria 2.Criterion 3.Order
数据访问层
public Collection<Information> findInformationByCriteria(
final Collection<Criterion> criterions, final int firstResult, final int maxResults,
final Collection<Order> orders) {
return (Collection<Information>) this.getHibernateTemplate().execute(
new HibernateCallback() {
@Override
public Object doInHibernate(Session arg0)
throws HibernateException, SQLException {
Criteria criteria = arg0
.createCriteria(Information.class);
if (null != criterions) {
for (Criterion criterion : criterions) {
if (null != criterion) {
criteria.add(criterion);
}
}
}
if (0 < maxResults) {
criteria.setMaxResults(maxResults);
}
criteria.setFirstResult(firstResult);
if (null != orders) {
for (Order order : orders) {
if (null != order) {
criteria.addOrder(order);
}
}
}
return criteria.list();
}
});
}
Criteria 是由session对象创建,参数是实体对象.class,常用的方法有:
list()返回对象集合;criteria.setMaxResults(int a)每页显示条数最大条数;criteria.setFirstResult(int b)起始条数;
criteria.add()添加查询条件;Criterion 对象或 Order对象
下面是组织查询条件,也是由仓库来完成
private Collection<Criterion> getCriterias(int status,int infoType, String keyWords,
Category category) {
Collection<Criterion> criterions = new ArrayList<Criterion>();
if (0!=infoType) {
criterions.add(Property.forName("infoType").eq(infoType));
}
if (0 != status) {
criterions.add(Property.forName("status").eq(status));
} else {
criterions.add(Property.forName("status").eq(
ManagementConstants.NORMAL));
}
if (!StringUtils.isBlank(keyWords)) {
criterions.add(Property.forName("infoClass").eq(category));
}
if (!StringUtils.isBlank(keyWords)) {
criterions.add(Restrictions.or(Restrictions.like("title", keyWords,
MatchMode.ANYWHERE), Restrictions.like("content", keyWords,
MatchMode.ANYWHERE)));
}
return criterions;
}
/**
* 排序规则
*/
private List<Order> getOrders(String orderField, String orderRule) {
List<Order> orders = new ArrayList<Order>();
if (!StringUtil.isStringNull(orderField)
&& "DESC".equals(orderRule.toUpperCase())) {
orders.add(Order.desc(orderField));
}
if (!StringUtil.isStringNull(orderField)
&& "ASC".equals(orderRule.toUpperCase())) {
orders.add(Order.asc(orderField));
}
return orders;
}
Criterion 查询条件 criterions是Criterion的对象集合
criterions.add(Property.forName("status").eq( ManagementConstants.NORMAL));
Restrictions.like("content", keyWords,
MatchMode.ANYWHERE)));
eq是相等的意思 like是字符匹配,后面的参数MatchMode.ANYWHERE是任意位置匹配,还有前匹配START后匹配END
= eq() 等于
<> not(Exprission.eq()) 不等于
> gt() 大于
>= ge() 大于等于
< lt() 小于
<= le() 小于等于
is null isnull() 等于空值
is not null isNotNull() 非空值
like like() 字符串模式匹配
and and() 逻辑与
and conjunction() 逻辑与
or or() 逻辑或
or disjunction() 逻辑或
not not() 逻辑非
in() in() 等于列表中的某一个值
Order 是排列顺序 orders 是Order对象的集合
orders.add(Order.desc(orderField));这是按照属性orderField降序排列
最后是仓库里的方法
/**
* 根据标题和资讯分类查询
*
* @return
*/
public Collection<Information> findInformationByCritera(int status,
int infoType, String keyWords, Category category, int page, int maxResults,
String orderField, String orderRule) {
return informationDao.findInformationByCriteria(getCriterias(status,
infoType, keyWords, category), (page - 1) * maxResults, maxResults,
getOrders(orderField, orderRule));
}
数据访问层
public Collection<Information> findInformationByCriteria(
final Collection<Criterion> criterions, final int firstResult, final int maxResults,
final Collection<Order> orders) {
return (Collection<Information>) this.getHibernateTemplate().execute(
new HibernateCallback() {
@Override
public Object doInHibernate(Session arg0)
throws HibernateException, SQLException {
Criteria criteria = arg0
.createCriteria(Information.class);
if (null != criterions) {
for (Criterion criterion : criterions) {
if (null != criterion) {
criteria.add(criterion);
}
}
}
if (0 < maxResults) {
criteria.setMaxResults(maxResults);
}
criteria.setFirstResult(firstResult);
if (null != orders) {
for (Order order : orders) {
if (null != order) {
criteria.addOrder(order);
}
}
}
return criteria.list();
}
});
}
Criteria 是由session对象创建,参数是实体对象.class,常用的方法有:
list()返回对象集合;criteria.setMaxResults(int a)每页显示条数最大条数;criteria.setFirstResult(int b)起始条数;
criteria.add()添加查询条件;Criterion 对象或 Order对象
下面是组织查询条件,也是由仓库来完成
private Collection<Criterion> getCriterias(int status,int infoType, String keyWords,
Category category) {
Collection<Criterion> criterions = new ArrayList<Criterion>();
if (0!=infoType) {
criterions.add(Property.forName("infoType").eq(infoType));
}
if (0 != status) {
criterions.add(Property.forName("status").eq(status));
} else {
criterions.add(Property.forName("status").eq(
ManagementConstants.NORMAL));
}
if (!StringUtils.isBlank(keyWords)) {
criterions.add(Property.forName("infoClass").eq(category));
}
if (!StringUtils.isBlank(keyWords)) {
criterions.add(Restrictions.or(Restrictions.like("title", keyWords,
MatchMode.ANYWHERE), Restrictions.like("content", keyWords,
MatchMode.ANYWHERE)));
}
return criterions;
}
/**
* 排序规则
*/
private List<Order> getOrders(String orderField, String orderRule) {
List<Order> orders = new ArrayList<Order>();
if (!StringUtil.isStringNull(orderField)
&& "DESC".equals(orderRule.toUpperCase())) {
orders.add(Order.desc(orderField));
}
if (!StringUtil.isStringNull(orderField)
&& "ASC".equals(orderRule.toUpperCase())) {
orders.add(Order.asc(orderField));
}
return orders;
}
Criterion 查询条件 criterions是Criterion的对象集合
criterions.add(Property.forName("status").eq( ManagementConstants.NORMAL));
Restrictions.like("content", keyWords,
MatchMode.ANYWHERE)));
eq是相等的意思 like是字符匹配,后面的参数MatchMode.ANYWHERE是任意位置匹配,还有前匹配START后匹配END
= eq() 等于
<> not(Exprission.eq()) 不等于
> gt() 大于
>= ge() 大于等于
< lt() 小于
<= le() 小于等于
is null isnull() 等于空值
is not null isNotNull() 非空值
like like() 字符串模式匹配
and and() 逻辑与
and conjunction() 逻辑与
or or() 逻辑或
or disjunction() 逻辑或
not not() 逻辑非
in() in() 等于列表中的某一个值
Order 是排列顺序 orders 是Order对象的集合
orders.add(Order.desc(orderField));这是按照属性orderField降序排列
最后是仓库里的方法
/**
* 根据标题和资讯分类查询
*
* @return
*/
public Collection<Information> findInformationByCritera(int status,
int infoType, String keyWords, Category category, int page, int maxResults,
String orderField, String orderRule) {
return informationDao.findInformationByCriteria(getCriterias(status,
infoType, keyWords, category), (page - 1) * maxResults, maxResults,
getOrders(orderField, orderRule));
}