hibernate一值多字段模糊查询
Posted Sincerity
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了hibernate一值多字段模糊查询相关的知识,希望对你有一定的参考价值。
- public Page getCoordByPage(Page queryHandler, TCoordinate conditions) {
- DetachedCriteria dc = DetachedCriteria.forClass(TCoordinate.class);
- if (conditions.getTAxis() != null) {
- dc.add(Restrictions.eq("TAxis", conditions.getTAxis()));
- }
- if(conditions.getCoordinateName()!=null){
- dc.add(Restrictions.like("coordinateName", conditions.getCoordinateName(),MatchMode.ANYWHERE).ignoreCase());
- }
- if(conditions.getParentCoordinate().getId()!=null){
- dc.add(Restrictions.eq("parentCoordinate.id", conditions.getParentCoordinate().getId()));
- }
- dc.addOrder(Order.desc("addDate"));
- Page page = this.coordinateDao.getPageByCriteria(queryHandler, dc);
- return page;
- }
两个条件之间or的用法【Restrictions.or(lhs, rhs)】:
- dc.add(Restrictions.or(Restrictions.like("country", comm.getCountry(),MatchMode.ANYWHERE).ignoreCase(),
- Restrictions.like("pinyin", comm.getCountry(),MatchMode.ANYWHERE).ignoreCase()));
超过两个,比如三个条件之间的or【Disjunction disjunction = Restrictions.disjunction();】:
- if(!ValidateUtils.isEmpty(comm.getCountry())){
- Disjunction disjunction = Restrictions.disjunction();
- disjunction.add(Restrictions.like("country", comm.getCountry(),MatchMode.ANYWHERE).ignoreCase());
- disjunction.add(Restrictions.like("pinyin", comm.getCountry(),MatchMode.ANYWHERE).ignoreCase());
- disjunction.add(Restrictions.like("jianpin", comm.getCountry(),MatchMode.ANYWHERE).ignoreCase());
- dc.add(disjunction);
- }
-
hibernate 查询match mode的四种模式
Hibernate 查询MatchMode的四种模式
MatchMode.START:字符串在最前面的位置.相当于"like ‘key%‘"
MatchMode.END:字符串在最后面的位置.相当于"like ‘%key‘"
MatchMode.ANYWHERE:字符串在中间匹配.相当于"like ‘%key%‘"
MatchMode.EXACT:字符串精确匹配.相当于"like ‘key‘"
以上是关于hibernate一值多字段模糊查询的主要内容,如果未能解决你的问题,请参考以下文章
java JPA with Hibernate模糊查询时类型为Long型出错