参考:https://blog.csdn.net/qq_40715775/article/details/83153808
依赖:
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-mongodb</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-rest</artifactId> </dependency>
domain:
@NoArgsConstructor @AllArgsConstructor @Document(collection = Constant.MILESTONE_WORK_TABLE) public class MilestoneInfo implements Serializable { private static final long serialVersionUID = 1L; @Id private String id; private String mawb;}
config:(application.propeties)
#spring.data.mongodb.uri=mongodb://userName:password@ip:port/database spring.data.mongodb.database=SERVICE-sea-test spring.data.mongodb.host=127.0.0.1 spring.data.mongodb.port=27017 #spring.data.mongodb.username=root #spring.data.mongodb.password=root
repository:
@RepositoryRestResource(path = "worktable",collectionResourceRel = "worktables") public interface WorkTableRepository extends MongoRepository<MilestoneInfo, String>{ // @Query("{\"sendtimes\":{\"$regex\":?0}, \"courierbillNo\": ?1}") List<MilestoneInfo> findByReasonAndCustomerAndSendTimesLessThan(String reason, String customer,int sendTimers); // @Query("{\"sendtimes\":{\"$regex\":?0}, \"success\": ?1}") List<MilestoneInfo> findByMawbOrCustomerOrOrderNoOrStatusOrCourierBillNoOrSendTimesLessThan(String mawb, String customer,String orderNo,String status,String courierbillNo,Integer sendTimers); List<MilestoneInfo> findByMawbAndCustomerAndOrderNoAndStatusAndCourierBillNoAndSendTimesLessThan(String mawb, String customer,String orderNo,String status,String courierbillNo,Integer sendTimers); }
web:
@GetMapping("finds/{page}/{size}") public Page<MilestoneInfo> findByPageSort(@PathVariable("page")int page,@PathVariable("size")int size) { //Sort sort = new Sort("id"); Sort sort = new Sort(Direction.ASC,"mawb"); // Pageable接口用于构造翻页查询,PageRequest是其实现类,可以通过提供的工厂方法创建PageRequest: // public static PageRequest of(int page, int size) // 也可以在PageRequest中加入排序: // public static PageRequest of(int page, int size, Sort sort) PageRequest pageable = PageRequest.of(page - 1, size,sort); Page<MilestoneInfo> pagedatas = workTableRepository.findAll(pageable); return pagedatas; }