spring data jpa hql動態查詢案例

目的:根據入參條件不一樣,動態組裝hql裏的where語句。sql

1. 實現代碼ui

  public List<WrapStatis> queryStatisCriteriaBuilder(String startTime, String endTime, String collectName,spa

                                                       String imei) {
        List<WrapStatis> list = new ArrayList<>();
        try {
            CriteriaBuilder cb = entityManager.getCriteriaBuilder();
            //WrapStatis指定了查詢結果返回至自定義對象
            CriteriaQuery<WrapStatis> query = cb.createQuery(WrapStatis.class);
            Root<StatisEntity> root = query.from(StatisEntity.class);
            Path<Calendar> timePath = root.get("createTime");
            Path<String> statisName = root.get("statisName");
            Path<String> statisNum = root.get("statisNum");
            Path<Double> statisRate = root.get("statisRate");
            List<Predicate> predicateList = new ArrayList<Predicate>();
            Date startDate = DateUtils.parse(startTime,DateUtils.YMD_DASH_WITH_TIME);
            Date endDate = DateUtils.parse(endTime,DateUtils.YMD_DASH_WITH_TIME);
            if (startTime != null) {
                predicateList.add(cb.between(root.get("createTime"),startDate,endDate));
            }
            if(StringUtils.isNotEmpty(collectName)){
                predicateList.add(cb.equal(root.get("collectName"),collectName));
            }

            if(StringUtils.isNotEmpty(imei) && !imei.equals("all")){
                predicateList.add(cb.equal(root.get("imei"),imei));
            }
            Predicate[] predicates = new Predicate[predicateList.size()];
            predicates = predicateList.toArray(predicates);
            //加上where條件
            query.where(predicates);
            //指定查詢項,select後面的東西
            Expression<String> timeStr = cb.function("DATE_FORMAT", String.class, timePath, cb.parameter(String.class, "formatStr"));//格式化日期
            query.multiselect(timeStr,statisName,statisNum, statisRate);//返回列
//            query.groupBy(root.get(conditionName),timeStr);//分組
//            query.orderBy(cb.asc(timeStr));//排序
            TypedQuery<WrapStatis> typedQuery = entityManager.createQuery(query);
typedQuery.setParameter(
"formatStr", Constant.STATIS_DAY); list = typedQuery.getResultList(); } catch (ParseException e) { log.error("call StatisService queryStatisCriteriaBuilder is error", e); } return list; }
Constant.STATIS_DAY值爲:%Y-%m-%d按天格式化,執行完成打印的sql爲:
select date_format(statisenti0_.create_time, ?) as col_0_0_, 
    statisenti0_.statis_name as col_1_0_, 
    statisenti0_.statis_num as col_2_0_, 
    statisenti0_.statis_rate as col_3_0_ 
        from statis statisenti0_ 
        where (statisenti0_.create_time between ? and ?) 
        and statisenti0_.collect_name=? 
        and statisenti0_.kepler_version=? 
        and statisenti0_.device_brand=? 
        and statisenti0_.rom_version=? 
        and statisenti0_.alipay_version=?

2. 包裝類wrapStatis.class爲code

  

public class WrapStatis {

    private String date;
    private String statisName;
    private String statisNum;
    private Double statisRate;

    //省略get/set/構造方法

}
相關文章
相關標籤/搜索