本文主要是介绍SpringDataJpa-动态查询并且分页,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
1.商品实体
/*** @Description 商品实体* @Author yuxiang* @CreatedDate 2019/10/30 12:04*/
@Entity
@Table(name = "item")
@Data
public class ItemEntity extends BaseEntity implements Serializable {/*** 商品名称*/private String itemName;/*** 价格*/private Double price;/*** 商品分类*/private String classNumber;/*** 商品牌子*/private String brand;/*** 是否在售卖 1表示上架中 0表示已经下架*/private int isSaled;public ItemEntity(){}public ItemEntity(String itemName, Double price, String classNumber, String brand, int isSaled) {this.itemName = itemName;this.price = price;this.classNumber = classNumber;this.brand = brand;this.isSaled = isSaled;}
}
2.respository
一定要实现接口JpaSpecificationExecutor
public interface ItemRepository extends JpaRepository<ItemEntity,Long> , JpaSpecificationExecutor<ItemEntity> {
}
3.service 及serviceImpl
service
/*** @Description 商品服务* @Author yuxiang* @CreatedDate 2019/10/30 17:31*/
public interface ItemService {/*** 根据表单查询商品* @param searchForm* @param pageNumber* @param pageSize* @return*/Page<ItemEntity> searchItemsByForm(ItemSearchForm searchForm, Integer pageNumber, Integer pageSize);
}
serviceImpl
@Service
public class ItemServiceImpl implements ItemService {@Autowiredprivate ItemRepository itemRepository;@Overridepublic Page<ItemEntity> searchItemsByForm(ItemSearchForm searchForm,Integer pageNumber,Integer pageSize){//Specification查询构造器Specification<ItemEntity> specification = new Specification<ItemEntity>() {@Overridepublic Predicate toPredicate(Root<ItemEntity> root, CriteriaQuery<?> criteriaQuery, CriteriaBuilder criteriaBuilder) {List<Predicate> predicates = new ArrayList<>();//添加搜素条件if (StringUtils.isNotEmpty(searchForm.getClassName())&&StringUtils.isNotBlank(searchForm.getClassName())){//分类用equalpredicates.add(criteriaBuilder.equal(root.get("className"),searchForm.getClassName()));}if (StringUtils.isNotEmpty(searchForm.getBrand())&&StringUtils.isNotBlank(searchForm.getBrand())){predicates.add(criteriaBuilder.like(root.get("brand"),"%"+searchForm.getBrand()+"%"));}if (StringUtils.isNotEmpty(searchForm.getItemNumber())&&StringUtils.isNotBlank(searchForm.getItemNumber())){predicates.add(criteriaBuilder.like(root.get("id"),"%"+searchForm.getItemNumber()+"%"));}if (StringUtils.isNotEmpty(searchForm.getItemName())&&StringUtils.isNotBlank(searchForm.getItemName())){predicates.add(criteriaBuilder.like(root.get("itemName"),"%"+searchForm.getItemName()+"%"));}//把搜索条件用and连接起来return criteriaBuilder.and(predicates.toArray(new Predicate[predicates.size()]));}};Pageable pageable = PageRequest.of(pageNumber,pageSize, Sort.Direction.DESC,"updateTime");//new PageRequest(page,size,Sort)已过时,这是官方推荐的方法return itemRepository.findAll(specification,pageable);}
}
在Contoller中使用
@PostMapping("/searchItem")public List<ItemEntity> searchItem(@RequestBody ItemSearchForm searchForm,@RequestParam(value = "pageNumber",defaultValue = "1")Integer pageNumber,@RequestParam(value = "pageSize",defaultValue = "10")Integer pageSize){ApiResponse response;Page<ItemEntity> page = itemService.searchItemsByForm(searchForm, pageNumber, pageSize);//返回商品列表return page.getContent();}
这篇关于SpringDataJpa-动态查询并且分页的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!