1 查询demo
@ServiceLog(description = "律所案件列表")
private MyPage<LegallfOrderInfoPageOut> selectLSOrderPage(LegalLfOrderInfoPageIn pageIn, PageRequest pageRequest) throws Exception{
QLegalOrderInfo qLegalOrderInfo=QLegalOrderInfo.legalOrderInfo;
QLawfirmOrder qLawfirmOrder=QLawfirmOrder.lawfirmOrder;
JPAQuery<Tuple> jpaQuery = new JPAQuery(entityManager);
JPAQuery<LegallfOrderInfoPageOut> leftQuery = jpaQuery.select(Projections.bean(LegallfOrderInfoPageOut.class,qLegalOrderInfo.appCode,
qLegalOrderInfo.proppserName, qLegalOrderInfo.proppserIdno,qLegalOrderInfo.batchNo, qLegalOrderInfo.paymentChEn,
qLegalOrderInfo.paymentChZn, qLawfirmOrder.lawfirmOrderStatus,qLawfirmOrder.createTime, qLawfirmOrder.placeCaseStatus,
qLawfirmOrder.businessNo, qLawfirmOrder.id.as("lawfirmId")))
.from(qLegalOrderInfo)
.leftJoin(qLawfirmOrder).on(qLegalOrderInfo.businessNo.eq(qLawfirmOrder.businessNo));
// 拼接查询条件
QueryDslContext qdc= QueryDslContextUtil.getQueryDslContext(pageIn);
qdc.where(qLegalOrderInfo.lfStatus.eq(1));
// 律所相关案件
if(StringUtils.isNotBlank(pageIn.getLawfirmNo())) {
qdc.where(qLegalOrderInfo.businessNo.in(
JPAExpressions.select(
qLawfirmOrder.businessNo
).from(qLawfirmOrder).where(
qLawfirmOrder.lawfirmNo.eq(pageIn.getLawfirmNo()).and(qLawfirmOrder.status.eq(1)))
));
}
//同步时间
if(pageIn.getLfTimeStart()!=null){
qdc.where(qLegalOrderInfo.lfTime.goe(pageIn.getLfTimeStart()));
}
if(pageIn.getLfTimeEnd() !=null){
qdc.where(qLegalOrderInfo.lfTime.loe(pageIn.getLfTimeEnd()));
}
// 回退案件
if(pageIn.getLawfirmOrderStatus() != null){
qdc.where(qLawfirmOrder.lawfirmOrderStatus.eq(pageIn.getLawfirmOrderStatus()));
}
Predicate[] predicatesArr = qdc.getPredicates().stream().toArray(Predicate[]::new);
// 执行sql
long totalCount = leftQuery.where(predicatesArr).fetchCount();
int pageSize = pageRequest.getPageSize();
MyPage<LegallfOrderInfoPageOut> myPage;
if (totalCount > 0) {
int pageNumber = pageRequest.getPageNumber();
List<LegallfOrderInfoPageOut> list = leftQuery.offset(pageNumber * pageSize).limit(pageSize).fetch();
int totalPage = (int) ((totalCount+ pageSize-1) / pageSize);
myPage = new MyPage(list, pageSize, (int) totalCount, totalPage);
} else {
myPage = new MyPage(new ArrayList(), pageSize, 0, 0);
}
return myPage;
}
2 MyPage:
package com.lz.cloud.utils;
import java.io.Serializable;
import java.util.List;
import com.alibaba.fastjson.annotation.JSONField;
import lombok.Data;
import lombok.EqualsAndHashCode;
@Data
@EqualsAndHashCode(callSuper = false)
public class MyPage<T> implements Serializable {
/**
* 列表数据
*/
@JSONField(name = "records")
private List<T> content;
// 页码大小
private int size;
// 总条数
@JSONField(name = "total")
private int totalElements;
// 总页数
@JSONField(name = "pages")
private int totalPages;
public MyPage(){}
public MyPage(List content, int size, int totalElements, int totalPages) {
this.content = content;
this.size = size;
this.totalElements = totalElements;
this.totalPages = totalPages;
}
}
3 QueryDslContext .java
package com.springplugin.start.util;
import com.querydsl.core.types.EntityPath;
import com.querydsl.core.types.Expression;
import com.querydsl.core.types.OrderSpecifier;
import com.querydsl.core.types.Predicate;
import java.util.ArrayList;
import java.util.List;
public class QueryDslContext {
private Class<?> clazz;
private EntityPath entityPath;
private List<Expression> expressions = new ArrayList();
private List<Predicate> predicates = new ArrayList();
private List<OrderSpecifier> orderSpecifiers = new ArrayList();
private List<JoinDslContext> joins = new ArrayList();
private GroupByDslContext groupBy = new GroupByDslContext();
public QueryDslContext() {
}
public QueryDslContext addJoin(JoinDslContext joinDslContext) {
if (!this.joins.contains(joinDslContext)) {
this.joins.add(joinDslContext);
}
return this;
}
public QueryDslContext addFiled(Expression expression) {
if (!this.expressions.contains(expression)) {
this.expressions.add(expression);
}
return this;
}
public QueryDslContext where(Predicate predicate) {
if (!this.predicates.contains(predicate)) {
this.predicates.add(predicate);
}
return this;
}
public QueryDslContext groubBy(Expression expression) {
this.groupBy.add(expression);
return this;
}
public QueryDslContext orderBy(OrderSpecifier orderSpecifier) {
this.orderSpecifiers.add(orderSpecifier);
return this;
}
public Expression[] expressionToArray() {
return (Expression[])this.expressions.toArray(new Expression[this.expressions.size()]);
}
public Predicate[] predicatesToArray() {
return (Predicate[])this.predicates.toArray(new Predicate[this.predicates.size()]);
}
public OrderSpecifier[] orderSpecifiersToArray() {
return (OrderSpecifier[])this.orderSpecifiers.toArray(new OrderSpecifier[this.orderSpecifiers.size()]);
}
public Class<?> getClazz() {
return this.clazz;
}
public EntityPath entityPath() {
return this.entityPath;
}
public List<Expression> getExpressions() {
return this.expressions;
}
public List<Predicate> getPredicates() {
return this.predicates;
}
public List<OrderSpecifier> getOrderSpecifiers() {
return this.orderSpecifiers;
}
public List<JoinDslContext> getJoins() {
return this.joins;
}
public GroupByDslContext getGroupBy() {
return this.groupBy;
}
public QueryDslContext setClazz(final Class<?> clazz) {
this.clazz = clazz;
return this;
}
public QueryDslContext entityPath(final EntityPath entityPath) {
this.entityPath = entityPath;
return this;
}
public QueryDslContext setExpressions(final List<Expression> expressions) {
this.expressions = expressions;
return this;
}
public QueryDslContext setPredicates(final List<Predicate> predicates) {
this.predicates = predicates;
return this;
}
public QueryDslContext setOrderSpecifiers(final List<OrderSpecifier> orderSpecifiers) {
this.orderSpecifiers = orderSpecifiers;
return this;
}
public QueryDslContext setJoins(final List<JoinDslContext> joins) {
this.joins = joins;
return this;
}
public QueryDslContext setGroupBy(final GroupByDslContext groupBy) {
this.groupBy = groupBy;
return this;
}
public boolean equals(final Object o) {
if (o == this) {
return true;
} else if (!(o instanceof QueryDslContext)) {
return false;
} else {
QueryDslContext other = (QueryDslContext)o;
if (!other.canEqual(this)) {
return false;
} else {
label95: {
Object this$clazz = this.getClazz();
Object other$clazz = other.getClazz();
if (this$clazz == null) {
if (other$clazz == null) {
break label95;
}
} else if (this$clazz.equals(other$clazz)) {
break label95;
}
return false;
}
Object this$entityPath = this.entityPath();
Object other$entityPath = other.entityPath();
if (this$entityPath == null) {
if (other$entityPath != null) {
return false;
}
} else if (!this$entityPath.equals(other$entityPath)) {
return false;
}
Object this$expressions = this.getExpressions();
Object other$expressions = other.getExpressions();
if (this$expressions == null) {
if (other$expressions != null) {
return false;
}
} else if (!this$expressions.equals(other$expressions)) {
return false;
}
label74: {
Object this$predicates = this.getPredicates();
Object other$predicates = other.getPredicates();
if (this$predicates == null) {
if (other$predicates == null) {
break label74;
}
} else if (this$predicates.equals(other$predicates)) {
break label74;
}
return false;
}
label67: {
Object this$orderSpecifiers = this.getOrderSpecifiers();
Object other$orderSpecifiers = other.getOrderSpecifiers();
if (this$orderSpecifiers == null) {
if (other$orderSpecifiers == null) {
break label67;
}
} else if (this$orderSpecifiers.equals(other$orderSpecifiers)) {
break label67;
}
return false;
}
Object this$joins = this.getJoins();
Object other$joins = other.getJoins();
if (this$joins == null) {
if (other$joins != null) {
return false;
}
} else if (!this$joins.equals(other$joins)) {
return false;
}
Object this$groupBy = this.getGroupBy();
Object other$groupBy = other.getGroupBy();
if (this$groupBy == null) {
if (other$groupBy != null) {
return false;
}
} else if (!this$groupBy.equals(other$groupBy)) {
return false;
}
return true;
}
}
}
protected boolean canEqual(final Object other) {
return other instanceof QueryDslContext;
}
public int hashCode() {
int PRIME = true;
int result = 1;
Object $clazz = this.getClazz();
int result = result * 59 + ($clazz == null ? 43 : $clazz.hashCode());
Object $entityPath = this.entityPath();
result = result * 59 + ($entityPath == null ? 43 : $entityPath.hashCode());
Object $expressions = this.getExpressions();
result = result * 59 + ($expressions == null ? 43 : $expressions.hashCode());
Object $predicates = this.getPredicates();
result = result * 59 + ($predicates == null ? 43 : $predicates.hashCode());
Object $orderSpecifiers = this.getOrderSpecifiers();
result = result * 59 + ($orderSpecifiers == null ? 43 : $orderSpecifiers.hashCode());
Object $joins = this.getJoins();
result = result * 59 + ($joins == null ? 43 : $joins.hashCode());
Object $groupBy = this.getGroupBy();
result = result * 59 + ($groupBy == null ? 43 : $groupBy.hashCode());
return result;
}
public String toString() {
return "QueryDslContext(clazz=" + this.getClazz() + ", entityPath=" + this.entityPath() + ", expressions=" + this.getExpressions() + ", predicates=" + this.getPredicates() + ", orderSpecifiers=" + this.getOrderSpecifiers() + ", joins=" + this.getJoins() + ", groupBy=" + this.getGroupBy() + ")";
}
}