QueryDSL实现按日期时间查询

转载自:https://blog.csdn.net/qq_38230774/article/details/88870649

实现代码如下:

由QueryDSL编译后生成的实体类

QPropertyBill qPropertyBill  = QPropertyBill.propertyBill;
QUser qUser = QUser.user;

获取前端时间戳参数

 Long requestDate = Long.parseLong(String.valueOf(map.get(APIConstant.DATE)));

因为我要查询某一天的数据,所以将日期格式化到 日 为止
 

 Date date = new Date(requestDate );
 String queryDate = DateUtil.formatDate(date,"yyyy-MM-dd");

建立格式化模板,这里相当于sql语句DATE_FORMAT(qPropertyBill .createTime,'%Y-%m-%d')
 

StringTemplate dateExpr = Expressions.stringTemplate("DATE_FORMAT({0},'%Y-%m-%d')",qPropertyBill .createTime);

执行SQL

BooleanBuilder booleanBuilder = new BooleanBuilder(
                qPropertyBill.communityId.eq(communityId)
                .and(qPropertyBill.deleteFlag.eq(0)));
                //此处为两张表关联查询——通过订单中的userId关联出此订单用户信息
 QueryResults queryResults = queryFactory
                .select(
                        //将结果返回为自定义PropertyBillVo类型
                        Projections.bean(
                                PropertyBillVo.class
                                //将查到的订单的pkId属性映射到PropertyBillVo的orderId属性
                                ,qPropertyBill.pkId.as("orderId")
                                ,qPropertyBill.title
                                ,qPropertyBill.total
                                ,qPropertyBill.des
                                ,qPropertyBill.state
                                ,qUser.nickname
                                 //将关联到的用户的account属性映射到PropertyBillVo的phone属性
                                ,qUser.account.as("phone")
                        )
                )
                //此处也可以通过leftJoin来实现
                .from(qPropertyBill,qUser)
                .where(booleanBuilder.and(
                                qPropertyBill.userId.eq(qUser.pkId)
                                //获取一天范围内的数据
                                .and(dateExpr.eq(queryDate )))
                )
                .offset((pageNum-1)*pageSize)
                .limit(pageSize)
                .orderBy(qPropertyBill.createTime.desc())
                .fetchResults();
                //返回结果
                return queryResults.getResults();

 

你可能感兴趣的:(ORM框架)