spring data jpa 分页查询
程序员文章站
2022-04-25 11:16:37
...
public Page<GoodsInfo> searchGoodsListPage(int pageIndex, int pageSize, final TblTaoyGoodsInfo goodsInfo, final Map paramMap)
{
String priceOrd = StringUtil.getStrValFromMap(paramMap, "priceOrd");
String salesOrd = StringUtil.getStrValFromMap(paramMap, "salesOrd");
String isHighProfit = StringUtil.getStrValFromMap(paramMap, "isHighProfit");
String couponOrd = StringUtil.getStrValFromMap(paramMap, "couponOrd");
final String isCoupon = StringUtil.getStrValFromMap(paramMap, "isCoupon");
final String isDayCoupon = StringUtil.getStrValFromMap(paramMap, "isDayCoupon");
final String startTime= StringUtil.getStrValFromMap(paramMap, "startTime");
final String endTime= StringUtil.getStrValFromMap(paramMap, "endTime");
//规格定义
Specification<GoodsInfo> specification = new Specification<GoodsInfo>() {
/**
* 构造断言
* @param root 实体对象引用
* @param query 规则查询对象
* @param cb 规则构建对象
* @return 断言
*/
@Override
public Predicate toPredicate(Root<GoodsInfo> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
List<Predicate> predicates = new ArrayList<>(); //所有的断言
if(1 == goodsInfo.getIs99()){ //添加断言 9.9包邮
Predicate is99 = cb.equal(root.get("is99").as(Integer.class), goodsInfo.getIs99());
predicates.add(is99);
}
if(1 == goodsInfo.getIsJuhuasuan()){ //添加断言 聚划算
Predicate isJuhuasuan = cb.equal(root.get("isJuhuasuan").as(Integer.class), goodsInfo.getIsJuhuasuan());
predicates.add(isJuhuasuan);
}
if(1 == goodsInfo.getIsTaoqg()){ //添加断言 淘抢购
Predicate isTaoqg = cb.equal(root.get("isTaoqg").as(Integer.class), goodsInfo.getIsTaoqg());
predicates.add(isTaoqg);
}
if(1 == goodsInfo.getIsHot()){ //添加断言 淘抢购
Predicate isHot = cb.equal(root.get("isHot").as(Integer.class), goodsInfo.getIsHot());
predicates.add(isHot);
}
if(ExStringUtils.isNotBlank(goodsInfo.getGoodsTitle())){ // 按标题搜索
Predicate goodsTitle = cb.like(root.get("goodsTitle").as(String.class), "%"+goodsInfo.getGoodsTitle()+"%");
predicates.add(goodsTitle);
}
if("1".equals(isCoupon)){
Predicate isCoupon = cb.isNotNull(root.get("couponId").as(String.class));
predicates.add(isCoupon);
}
// 每日神券
if("1".equals(isDayCoupon)){ // 开始时间<= 当日 00:00:00 结束时间 >= 当日23:59:59
predicates.add(cb.lessThanOrEqualTo(root.get("couponStartTime").as(String.class), startTime);
predicates.add(cb.greaterThan(root.get("couponEndTime").as(String.class), endTime));
}
return cb.and(predicates.toArray(new Predicate[0]));
}
};
pageIndex = pageIndex <= 0 ? 0 : pageIndex - 1;
Pageable pageable = null;
//多属性排序 按add先后顺序
List< Order> orders=new ArrayList< Order>();
if("1".equals(isHighProfit)){
orders.add( new Order(Sort.Direction. DESC, "aboutFee"));
}
if("1".equals(couponOrd)){
orders.add( new Order(Sort.Direction. DESC, "couponPrice"));
}
if("1".equals(priceOrd)){
orders.add( new Order(Sort.Direction. ASC, "goodsPrice"));
}
if("0".equals(priceOrd)){
orders.add( new Order(Sort.Direction. DESC, "goodsPrice"));
}
if("1".equals(salesOrd)){
orders.add( new Order(Sort.Direction. ASC, "volume"));
}
if("0".equals(salesOrd)){
orders.add( new Order(Sort.Direction. DESC, "volume"));
}
if(orders.size()>0){
pageable = new PageRequest(pageIndex, pageSize, new Sort(orders));
}else{
pageable = new PageRequest(pageIndex, pageSize);
}
Page<GoodsInfo> all = goodsRepository.findAll(specification, pageable);
return all;
}
上一篇: 解决ie6下最小高度问题
下一篇: 解决IE6下fixed失效的问题
推荐阅读
-
Spring Data Solr 查询方法
-
Spring Data Jpa+SpringMVC+Jquery.pagination.js实现分页示例
-
springboot使用spring-data-jpa操作MySQL数据库
-
Spring Data JPA 实现多表关联查询的示例代码
-
SpringBoot Data JPA 关联表查询的方法
-
Spring Data JPA+kkpager实现分页功能实例
-
EasyUi+Spring Data 实现按条件分页查询的实例代码
-
Spring Data Jpa+SpringMVC+Jquery.pagination.js实现分页示例
-
Spring Data JPA+kkpager实现分页功能实例
-
Spring Data JPA实现动态条件与范围查询实例代码