我正在学习使用JHipster,并且无法弄清楚如何使用创建自定义查询。
在我的项目中,我有Orders
表格,其中包含DeliveryDay
和Week
字段,并且只想显示当前某一天的订单。 DeliveryDay和Week为int
,其值为(1-7和0-2)
所以在OrdersRepository.java
我添加了这样的自定义查询:
public interface OrdersRepository extends JpaRepository<Orders,Long> {
Page<Orders> findByDeliveryDayAndWeek(int weekday, int week, pageable);
在OrdersResource.java
我添加了这个:
@RequestMapping(value = "/today",
method = RequestMethod.GET,
produces = MediaType.APPLICATION_JSON_VALUE)
@Timed
public ResponseEntity<List<Orders>> getOrdersForToday(Pageable pageable)
throws URISyntaxException {
log.debug("REST request to get a page of Orderss");
Page<Orders> page = ordersRepository.findByDeliveryDayAndWeek(1, 0, pageable);
HttpHeaders headers = PaginationUtil.generatePaginationHttpHeaders(page, "/api/today");
return new ResponseEntity<>(page.getContent(), headers, HttpStatus.OK);
}
我还添加了today.html
(已复制orders.html
)和today.js
'use strict';
angular.module('fruitcrmApp')
.config(function ($stateProvider) {
$stateProvider
.state('today', {
parent: 'entity',
url: '/today',
data: {
authorities: ['ROLE_USER'],
pageTitle: 'fruitcrmApp.orders.home.title'
},
views: {
'content@': {
templateUrl: 'scripts/app/custom/today.html',
controller: 'OrdersController'
}
},
resolve: {
translatePartialLoader: ['$translate', '$translatePartialLoader', function ($translate, $translatePartialLoader) {
$translatePartialLoader.addPart('orders');
$translatePartialLoader.addPart('global');
return $translate.refresh();
}]
}
})
});
并在today.js
index.html
我的orders.controller.js
看起来像这样(由JHipster生成)
'use strict';
angular.module('fruitcrmApp')
.controller('OrdersController', function ($scope, $state, Orders, OrdersSearch, ParseLinks) {
$scope.orderss = [];
$scope.predicate = 'id';
$scope.reverse = true;
$scope.page = 1;
$scope.loadAll = function() {
Orders.query({page: $scope.page - 1, size: 20, sort: [$scope.predicate + ',' + ($scope.reverse ? 'asc' : 'desc'), 'id']}, function(result, headers) {
$scope.links = ParseLinks.parse(headers('link'));
$scope.totalItems = headers('X-Total-Count');
$scope.orderss = result;
});
};
$scope.loadPage = function(page) {
$scope.page = page;
$scope.loadAll();
};
$scope.loadAll();
$scope.search = function () {
OrdersSearch.query({query: $scope.searchQuery}, function(result) {
$scope.orderss = result;
}, function(response) {
if(response.status === 404) {
$scope.loadAll();
}
});
};
$scope.refresh = function () {
$scope.loadAll();
$scope.clear();
};
$scope.clear = function () {
$scope.orders = {
details: null,
orderDate: null,
firstDelivery: null,
isActive: false,
id: null
};
};
});
现在我可以访问http://localhost:3000/#/today
,但它会显示Orders
我所做错的所有数据?如何使用我自己的OrdersRepository.java
方法?
我试图搜索示例,但没有发现任何相关内容。我错过了哪些必要的步骤?如果答案很长,那么链接到一些教程将会很好。
答案 0 :(得分:7)
您需要为today
API端点创建新的角度服务。像这样的东西叫做orders-today.service.js
:
'use strict';
angular.module('fruitcrmApp')
.factory('OrdersToday', function ($resource) {
return $resource('api/orders/today', {}, {
'query': { method: 'GET', isArray: true}
});
});
然后在您的orders.controller.js
文件中,您需要注入新的OrdersToday服务:
.controller('OrdersController', function ($scope, $state, Orders, OrdersSearch, OrdersToday, ParseLinks) {
如果您想获取今天订单的列表,则需要使用OrdersToday.query
,就像您在粘贴的示例中使用Orders.query
一样。
您可能希望创建一个引用OrdersTodayController
的{{1}},并在OrdersToday
而不是today.js
中使用它。