利用DetachedCriteria構建HQL參數動態匹配,detachedcriteria

來源:互聯網
上載者:User

利用DetachedCriteria構建HQL參數動態匹配,detachedcriteria

此文章是基於 搭建SpringMVC+Spring+Hibernate平台

 

1. DetachedCriteria構建類:CriteriaBuilder.java

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;    }}
View Code


2. 比較子枚舉類:Expression.java

package com.ims.persistence.base;public enum Expression {    /** 等於  */    EQ,    /** 不等於 */    NE,    /** 大於 */    GT,    /** 大於等於 */    GE,    /** 小於 */    LT,    /** 小於等於 */    LE;}
View Code


3. 使用如類:WarehouseBSImpl.java

package com.ims.service.xxx.impl;import java.util.HashMap;import java.util.List;import java.util.Map;import org.hibernate.criterion.DetachedCriteria;import org.hibernate.criterion.ProjectionList;import org.hibernate.criterion.Projections;import org.hibernate.transform.Transformers;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.stereotype.Service;import com.ims.persistence.base.CriteriaBuilder;import com.ims.persistence.dao.xxx.WarehouseDao;import com.ims.persistence.model.xxx.Warehouse;import com.ims.service.xxx.WarehouseBS;@Service("warehouseBS")public class WarehouseBSImpl implements WarehouseBS{    @Autowired    private WarehouseDao warehouseDao;        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);    }}
View Code

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.