利用DetachedCriteria构建HQL参数动态匹配
此文章是基于 搭建Jquery+SpringMVC+Spring+Hibernate+MySQL平台
一. 类文件介绍
1. CriteriaBuilder.java,DetachedCriteria构建类
package com.ims.persistence.base; import java.math.BigDecimal; import java.sql.Types; import java.util.Map; import org.apache.commons.lang3.StringUtils; import org.hibernate.criterion.Criterion; import org.hibernate.criterion.DetachedCriteria; import org.hibernate.criterion.MatchMode; import org.hibernate.criterion.Restrictions; import com.ims.common.DateUtil; public class CriteriaBuilder { private DetachedCriteria detachedCriteria; private Map<String, ?> parmasMap; public CriteriaBuilder(DetachedCriteria detachedCriteria, Map<String, ?> parmasMap){ this.detachedCriteria = detachedCriteria; this.parmasMap = parmasMap; } public DetachedCriteria addCriterion(String propertyName,String paramName){ return addCriterion(propertyName,paramName,Expression.EQ); } public DetachedCriteria addCriterion(String propertyName,String paramName,Expression expression){ return addCriterion(propertyName, paramName, Types.VARCHAR, expression); } public DetachedCriteria addCriterion(String propertyName,String paramName, Integer propertyType){ return addCriterion(propertyName,paramName,propertyType,Expression.EQ); } public DetachedCriteria addCriterion(String propertyName,String paramName, Integer propertyType, Expression expression){ if(parmasMap!=null){ Criterion criterion = buildCriterion(propertyName,paramName,propertyType,expression); if(null!=criterion){ detachedCriteria.add(criterion); } } return detachedCriteria; } private Criterion buildCriterion(String propertyName,String paramName,Integer propertyType,Expression expression){ boolean propertyBlank = true; Object paramValue = parmasMap.get(paramName); if(paramValue!=null && StringUtils.isNotBlank(paramValue.toString())){ propertyBlank = false; } Criterion criterion = null; if(!propertyBlank){ switch(propertyType){ case Types.INTEGER: paramValue = Integer.valueOf(paramValue.toString()); break; case Types.VARCHAR: paramValue = paramValue.toString(); break; case Types.DATE: paramValue = DateUtil.stringToDate(paramValue.toString()); break; case Types.DECIMAL: paramValue = new BigDecimal(paramValue.toString()); break; } switch(expression){ case EQ: criterion = Restrictions.eq(propertyName, paramValue); break; case NE: criterion = Restrictions.ne(propertyName, paramValue); break; case GT: criterion = Restrictions.gt(propertyName, paramValue); break; case GE: criterion = Restrictions.ge(propertyName, paramValue); break; case LT: criterion = Restrictions.lt(propertyName, paramValue); break; case LE: criterion = Restrictions.le(propertyName, paramValue); break; } } return criterion; } public DetachedCriteria addLikeCriterion(String propertyName,String paramName){ return addLikeCriterion(propertyName,paramName,MatchMode.ANYWHERE); } public DetachedCriteria addLikeCriterion(String propertyName,String paramName,MatchMode matchMode){ if(parmasMap!=null){ Criterion criterion = buildLikeCriterion(propertyName,paramName,matchMode); if(null!=criterion){ detachedCriteria.add(criterion); } } return detachedCriteria; } public Criterion buildLikeCriterion(String propertyName,String paramName,MatchMode matchMode){ Criterion criterion = null; Object paramValue = parmasMap.get(paramName); if(paramValue!=null && StringUtils.isNotBlank(paramValue.toString())){ criterion = Restrictions.like(propertyName, paramValue.toString(), matchMode); } return criterion; } }
2. Expression.java,比较运算符枚举类
package com.ims.persistence.base; public enum Expression { /** 等于 */ EQ, /** 不等于 */ NE, /** 大于 */ GT, /** 大于等于 */ GE, /** 小于 */ LT, /** 小于等于 */ LE; }
二. 使用方法介绍
1. 使用如类:WarehouseBSImpl.java
public class WarehouseBSImpl implements WarehouseBS{ private DetachedCriteria buildCriteria(DetachedCriteria detachedCriteria, Map<String, Object> params){ CriteriaBuilder criteriaBuilder = new CriteriaBuilder(detachedCriteria, params); criteriaBuilder.addCriterion("warehouse.projectCode", "projectCode"); return detachedCriteria; } @Override public List<Map<String, Object>> getWarehouses(String projectCode) { Map<String, Object> params = new HashMap<String, Object>(); params.put("projectCode", projectCode); DetachedCriteria detachedCriteria = DetachedCriteria.forClass(Warehouse.class, "warehouse"); buildCriteria(detachedCriteria, params); ProjectionList pList = Projections.projectionList(); pList.add(Projections.property("warehouse.warehouseCode").as("value")); pList.add(Projections.property("warehouse.warehouseName").as("text")); detachedCriteria.setProjection(pList); detachedCriteria.setResultTransformer(Transformers.ALIAS_TO_ENTITY_MAP); return warehouseDao.findByCriteria(detachedCriteria); } }