// 根据条件筛选课程订单 @SuppressWarnings("unchecked") @RequestMapping(value = "/{coachID}", method = RequestMethod.GET) public PageImpl<OrderLine> findOrderLinesByCoach( @PathVariable Integer coachID, @RequestParam(value = "order_status", required = false) String orderStatus, @RequestParam(value = "start_date") String startDate, @RequestParam(value = "end_date") String endDate, Pageable pageable) { CoachEntity coachEntity = coachService.findOne(coachID); Validate.notNull(coachEntity, "coach id is invalid"); DateTime startDateTime = (null == startDate ? null : DateTime.parse(startDate)); DateTime endDateTime = (null == endDate ? null : DateTime.parse(endDate)); List<OrderLineEntity> orderLines = orderLineService.findByOrderLineStatus(coachID, orderStatus, startDateTime, endDateTime); Integer start = (pageable.getPageNumber() * pageable.getPageSize()); Integer end; if ((pageable.getPageNumber() + 1) * pageable.getPageSize() > orderLines.size()) { end = orderLines.size(); } else { end = ((pageable.getPageNumber() + 1) * pageable.getPageSize()); } List<OrderLineEntity> show = orderLines.subList(start, end); return ModelConvertUtils.getPageByOrderLineEntity(show, pageable, orderLines.size()); }
private PageParams convert(final Pageable pageable) { final PageParams result = new PageParams(); result.setPageSize(pageable.getPageSize()); result.setDataCount(pageable.getPageNumber()); result.setPageNo(pageable.getPageNumber()); return result; }
/** @see DATAMONGO-445 */ @Test public void shouldTakeSkipAndLimitSettingsFromGivenPageable() { Pageable pageable = new PageRequest(3, 5); NearQuery query = NearQuery.near(new Point(1, 1)).with(pageable); assertThat(query.getSkip(), is(pageable.getPageNumber() * pageable.getPageSize())); assertThat( (Integer) query.toDBObject().get("num"), is((pageable.getPageNumber() + 1) * pageable.getPageSize())); }
/** @see DATAMONGO-445 */ @Test public void shouldTakeSkipAndLimitSettingsFromPageableEvenIfItWasSpecifiedOnQuery() { int limit = 10; int skip = 5; Pageable pageable = new PageRequest(3, 5); NearQuery query = NearQuery.near(new Point(1, 1)) .query(Query.query(Criteria.where("foo").is("bar")).limit(limit).skip(skip)) .with(pageable); assertThat(query.getSkip(), is(pageable.getPageNumber() * pageable.getPageSize())); assertThat( (Integer) query.toDBObject().get("num"), is((pageable.getPageNumber() + 1) * pageable.getPageSize())); }
@Test public void testGetUsersWithAuthority() throws SQLException { String expectedQuery = "select \"public\".\"users\".\"username\", \"public\".\"users\".\"locked\", \"public\".\"authorities\".\"authority\", count(\"public\".\"users\".\"username\") over () as \"count\" from \"public\".\"users\" join \"public\".\"authorities\" on \"public\".\"users\".\"username\" = \"public\".\"authorities\".\"username\" where \"public\".\"authorities\".\"authority\" = 'ROLE_ADMIN' order by \"public\".\"users\".\"username\" asc limit 5 offset 0"; Pageable pageable = Mockito.mock(Pageable.class); ResultSet resultSet = Mockito.mock(ResultSet.class); Mockito.when(pageable.getPageNumber()).thenReturn(0); Mockito.when(pageable.getPageSize()).thenReturn(5); Mockito.when(resultSet.getInt(PagingConstants.COUNT)).thenReturn(1); Mockito.when(resultSet.getString("username")).thenReturn("admin"); RESTPage<UserDTO> page = userDao.getUsersWithAuthority("ROLE_ADMIN", pageable); ArgumentCaptor<PagingRowCallbackHandler> pagingRowCallbackHandlerCaptor = ArgumentCaptor.forClass(PagingRowCallbackHandler.class); Mockito.verify(jdbcTemplate) .query(Matchers.eq(expectedQuery), pagingRowCallbackHandlerCaptor.capture()); PagingRowCallbackHandler pagingRowCallbackHandler = pagingRowCallbackHandlerCaptor.getValue(); pagingRowCallbackHandler.processRow(resultSet); Mockito.verify(resultSet).getInt(PagingConstants.COUNT); Mockito.verify(resultSet).getString("username"); Assert.assertEquals(1, page.getContentSize()); Assert.assertEquals("admin", page.getContent().get(0).getUsername()); }
private Query createQueryParam(String _jpql, List<SqlQueryItem> _paramList, Pageable _pageable) { Query query = manager.createQuery(_jpql); if (_pageable != null) { int maxResult = _pageable.getPageSize(); query.setFirstResult(_pageable.getPageNumber() * _pageable.getPageSize()); query.setMaxResults(maxResult); } if (_paramList != null) { for (SqlQueryItem param : _paramList) { Object value = param.getValue(); if (param.getAction() == SqlActionType.LIKE) { String val = param.getValue().toString().trim(); if (!val.startsWith("%")) { val = "%" + val; } if (!val.endsWith("%")) { val = val + "%"; } value = val; } query.setParameter(param.getPropertyName().replace(".", ""), value); } } return query; }
@Override public <T> Page<T> getPage( Class<T> clazz, Query query, Pageable pageable, HibernateParameter... parameters) { long totalItemCount = count(query, parameters); List<T> items = find( clazz, query, pageable.getPageNumber() * pageable.getPageSize(), pageable.getPageSize(), parameters); return new PageImpl<T>(items, pageable, totalItemCount); }
@Override public <T> Page<T> getPage(Class<T> clazz, Criteria criteria, Pageable pageable) { Criteria countCriteria = HibernateTool.copyCriteria(criteria); long totalItemCount = count(countCriteria); List<T> items = find( clazz, criteria, pageable.getPageNumber() * pageable.getPageSize(), pageable.getPageSize(), CriteriaTool.toOrders(pageable.getSort()).toArray(new Order[0])); return new PageImpl<T>(items, pageable, totalItemCount); }
@Test public void testGetUsersWithAuthority_page2() { String expectedQuery = "select \"public\".\"users\".\"username\", \"public\".\"users\".\"locked\", \"public\".\"authorities\".\"authority\", count(\"public\".\"users\".\"username\") over () as \"count\" from \"public\".\"users\" join \"public\".\"authorities\" on \"public\".\"users\".\"username\" = \"public\".\"authorities\".\"username\" where \"public\".\"authorities\".\"authority\" = 'ROLE_ADMIN' order by \"public\".\"users\".\"username\" asc limit 5 offset 5"; Pageable pageable = Mockito.mock(Pageable.class); Mockito.when(pageable.getPageNumber()).thenReturn(1); Mockito.when(pageable.getPageSize()).thenReturn(5); userDao.getUsersWithAuthority("ROLE_ADMIN", pageable); Mockito.verify(jdbcTemplate) .query(Matchers.eq(expectedQuery), Matchers.any(PagingRowCallbackHandler.class)); }
@Override public List<T> queryNativeSqlListEntity(String _nativeSql, Object[] _params, Pageable _pageable) { Query query = getManager().createNativeQuery(_nativeSql, this.getCls()); if (_pageable != null) { int maxResult = _pageable.getPageSize(); query.setFirstResult(_pageable.getPageNumber() * _pageable.getPageSize()); query.setMaxResults(maxResult); } if (_params != null) { for (int i = 0; i < _params.length; i++) { query.setParameter(i + 1, _params[i]); } } return query.getResultList(); }
@RequestMapping @PageableDefaults(value = 20, sort = "id=desc") public String list(@CurrentUser User user, Pageable pageable, Model model) { Searchable searchable = Searchable.newSearchable(); searchable.addSearchFilter("userId", SearchOperator.eq, user.getId()); Page<NotificationData> page = notificationDataService.findAll(pageable); model.addAttribute("page", page); if (pageable.getPageNumber() == 0) { notificationDataService.markReadAll(user.getId()); } return viewName("list"); }
@Override public Page<Catalog> findAll(Pageable arg0) { int pSize = arg0.getPageSize(); int pNumb = arg0.getPageNumber(); int pFirst = pNumb * pSize; int pLast = pFirst + pSize; int total = datas.size(); List<Catalog> content = new ArrayList<>(); for (int i = 0; i < total; i++) { if (i >= pFirst && i < pLast) { Catalog data = datas.get(i); content.add(data); } } return new PageImpl<>(content, arg0, total); }
@RequestMapping(value = "/cities/search", method = RequestMethod.GET) public PagedCities search(@RequestParam("name") String name, Pageable pageable) { return repository.findByNameContains(name, pageable.getPageNumber(), pageable.getPageSize()); }
@RequestMapping(value = "/cities/", method = RequestMethod.GET) public PagedCities list(Model uiModel, Pageable pageable) { PagedCities cities = repository.findAll(pageable.getPageNumber(), pageable.getPageSize()); uiModel.addAttribute("pagedCities", cities); return cities; }