多条件分页+排序
1、Controller
package com.mf.controller.bas;
import com.mf.entity.bas.BasYeGroup;
import com.mf.service.bas_service.BasYeService;
import com.mf.util.ResponseResult;
import com.mf.util.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
@RestController
@RequestMapping("/bas/ye")
public class BasYeController {
@Autowired
private BasYeService basYeService;
/**
* 查询叶片
* @param basYeGroup
* @param page
* @param rows
* @return
*/
@RequestMapping("/all")
public ResponseResult<List<BasYeGroup>> YeAll(
BasYeGroup basYeGroup,
@RequestParam("page") Integer page,
@RequestParam("rows") Integer rows
){
//传参
String yeGroupId=basYeGroup.getYeGroupId();
Page<BasYeGroup> basYeGroups=basYeService.getYeAll(page,rows,yeGroupId);
//返回数据
List<BasYeGroup> list =basYeGroups.getContent();
//总条数
Long count=basYeService.getCount(basYeGroup);
return new ResponseResult<>(1,count,list);
}
}
2、Service
package com.mf.service.bas_service;
import com.mf.entity.bas.BasProduct;
import com.mf.entity.bas.BasYeGroup;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import java.util.List;
public interface BasYeService {
/**
* 叶片查询
*/
Page<BasYeGroup> getYeAll(Integer page,Integer rows,String yeGroupId);
/**
* 叶片分页总数
* @param basProduct
* @return
*/
Long getCount(BasYeGroup basProduct);
}
3、实现类
package com.mf.service.bas_impl;
import com.mf.entity.bas.BasYeGroup;
import com.mf.repository.bas.BasYeRepository;
import com.mf.service.bas_service.BasYeService;
import com.mf.util.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.data.domain.Sort.Order;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import javax.persistence.criteria.*;
import java.util.ArrayList;
import java.util.List;
@Service
public class BasYeServiceImpl implements BasYeService {
@Autowired
BasYeRepository basYeRepository;
/**
* 叶片分页
* @param
* @param
* @param
* @return
*/
@Override
public Page<BasYeGroup> getYeAll(Integer page, Integer rows, String yeGroupId) {
//1、多排序
List<Order> list=new ArrayList<>();
list.add( new Order(Direction.ASC, "yeGroupId"));
list.add( new Order(Direction.ASC, "theOrder"));
Sort sort = new Sort(list);
//2、传参
//Pageable 封装了分页的参数,当前页,每页显示的条数,注意:它的当前页是从0开始的
Pageable pageable = new PageRequest(page-1, rows,sort);
Specification<BasYeGroup> spec = (root, criteriaQuery, criteriaBuilder) -> {
List<Predicate> predicates = new ArrayList<Predicate>();
//3、模糊查询
if (StringUtil.isNotEmpty(yeGroupId)) {
predicates.add(criteriaBuilder.like(root.get("yeGroupId"), "%" + yeGroupId + "%"));
}
//4、将多条件连接在一起
return criteriaQuery.where(predicates.toArray(new Predicate[predicates.size()])).getRestriction();
};
//5、返回数据
return basYeRepository.findAll(spec,pageable);
}
/**
* 叶片分页总数
* @param basYeGroup
* @return
*/
@Override
public Long getCount(BasYeGroup basYeGroup) {
Long count = basYeRepository.count(new Specification<BasYeGroup>() {
@Override
public Predicate toPredicate(Root<BasYeGroup> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
Predicate predicate = cb.conjunction();
if(basYeGroup != null) {
if (StringUtil.isNotEmpty(basYeGroup.getYeGroupId())) {
predicate.getExpressions().add(cb.like(root.get("yeGroupId"), "%" + basYeGroup.getYeGroupId()+ "%"));
}
}
return predicate;
}
});
return count;
}
}
4、dao层
package com.mf.repository.bas;
import com.mf.entity.bas.BasYeGroup;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
public interface BasYeRepository extends JpaRepository<BasYeGroup,String>, JpaSpecificationExecutor<BasYeGroup> {
}
5、实体类
package com.mf.entity.bas;
import lombok.Data;
import org.springframework.data.domain.Sort;
import javax.persistence.*;
import java.io.Serializable;
/**
* 叶片
*/
@Data
@Entity//实体
@Table(name = "bas_ye_group")
@NamedQuery(name = "BasYeGroup.findAll",query = "select b from BasYeGroup b")
public class BasYeGroup implements Serializable {
@Id
@Column(name = "ye_group_guid")
public String yeGroupGuid;
@Column(name = "ye_group_id")
public String yeGroupId;
@Column(name = "product_id")
public String productId;
@Column(name = "the_order")
public Integer theOrder;
@Column(name = "quantity")
public Integer quantity;
public BasYeGroup(){}
@Override
public String toString() {
return "BasYeGroup{" +
"yeGroupGuid='" + yeGroupGuid + '\'' +
", yeGroupId='" + yeGroupId + '\'' +
", productId='" + productId + '\'' +
", theOrder=" + theOrder +
", quantity=" + quantity +
'}';
}
}
番外-Jpa多条件查询
©著作权归作者所有,转载或内容合作请联系作者
- 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
- 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
- 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
推荐阅读更多精彩内容
- 有的业务场景需要我们可选多个条件进行查询,这时要是写死查询条件就会很不方便了,我们可以使用Specificatio...
- 实现 PagingAndSortingRepository,QueryDslPredicateExecutor,J...
- 不需要自己拼写组装sql 引用pom 3.建实体类,maven 编译代后生产QOrderEntity,QOrder...
- 参考文章: 解决 JPA 多表动态查询 JPA EntityManager createNativeQuery 多...
- 在项目中,一般都会遇到参数不确定传的情况,比如搜索,关键字可能是空,在mybtis中可以用<if>标签去判断,Jp...