spring data jpa hql dynamic query Case

Objective: The condition of the various parameters, dynamic assembly hql in the where clause.

1. Implement the code

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

                                                       String imei) {
        List<WrapStatis> list = new ArrayList<>();
        try {
            CB CriteriaBuilder = entityManager.getCriteriaBuilder ();
             // WrapStatis query result is returned to the specified custom object 
            a 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);
             // add where condition 
            query.where (predicates);
             // specify query terms, behind select something 
            Expression <String> = cb.function of TIMESTR ( "the DATE_FORMAT", String. class , timePath, cb.parameter (String. class , "formatStr")); // formatted date 
            query.multiselect (of TIMESTR, statisName, statisNum, statisRate); // returns the column
 //             query.groupBy (root.get (conditionName) , of TIMESTR); // packet
 //             query.orderBy (cb.asc (of TIMESTR)); // Sort
            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 value: % D% Y-M-% by day formatting, printing is completed sql is performed:
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. The packaging is wrapStatis.class

  

public class WrapStatis {

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

    // GET omitted / set / constructor 

}

 

  

Guess you like

Origin www.cnblogs.com/jiangds/p/11004947.html