spring-data-jpa查询语句的书写实例小计

时间:2021-09-27 09:41:44
//查询语句
List<AuctionLot> alots = auctionLotRepository.findAllByAuctionIdAndAucIdIsNotNullAndIsOffcourtIsNullOrderByOrderNum(auctionId);
1.auctionId属性与传入的值相等

2.aucId属性不能为null

3.isOffcourt属性为null

4.根据orderNum排序

以上,查询语句和以下效果一样

Specification<AuctionLot> spec = (root, query, cb) -> {
List<Predicate> predicates = new ArrayList<Predicate>(); Predicate predicate = cb.equal(root.get(AuctionLot_.auctionId), auctionId);
predicates.add(predicate);
Predicate predicate1 = cb.isNotNull(root.get(AuctionLot_.aucId));
predicates.add(predicate1);
Predicate predicate2 = cb.notEqual(root.get(AuctionLot_.isOffcourt), Integer.valueOf(1));
predicates.add(predicate2);
query.orderBy(cb.desc(root.get(AuctionLot_.orderNum)));// 根据出价时间倒序排列
if (!predicates.isEmpty()) {
return cb.and(predicates.toArray(new Predicate[0]));
} else {
return null;
}
}; List<AuctionLot> alots = auctionLotRepository.findAll(spec);