- 不需要自己拼写组装sql
- 引用pom
<dependency>
<groupId>com.querydsl</groupId>
<artifactId>querydsl-jpa</artifactId>
</dependency>
<dependency>
<groupId>com.querydsl</groupId>
<artifactId>querydsl-apt</artifactId>
</dependency>
3.建实体类,maven 编译代后生产QOrderEntity,QOrderEntity is a Querydsl query type for OrderEntity
@Data
@DynamicInsert
@DynamicUpdate
@Entity
@Table(name = "t_order")
public class OrderEntity {
/**
* 主键
*/
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id")
private Long id;
/**
* 平台订单号
*/
@Column(name = "order_id")
private String orderId;
/**
* 用户id
*/
@Column(name = "user_id")
private String userId;
}
4.具体demo示例
//加sql条件
List<BooleanExpression> predicates = new ArrayList<>();
if (StringUtils.isNotBlank(userId)) {
predicates.add(QOrderEntity.orderEntity.userId.eq(userId));
}
if (StringUtils.isNotBlank(orderId)) {
predicates.add(QOrderEntity.orderEntity.orderId.eq(orderId));
}
//加sql排序
List<Sort.Order> sorts = new ArrayList<>();
sorts.add(new Sort.Order(Sort.Direction.DESC, "createTime"));
Sort sort = new Sort(sorts);
//加sql分页
Pageable pageable = new PageRequest(pageNum - 1, pageSize, sort);
Page<OrderEntity> tbOtaDeviceResults = orderDao.findAll(ExpressionUtils.allOf(predicates.toArray(new Predicate[]{})), pageable);
网友评论