我正在制作一个项目(基础商店),目前我坚持订购(例如按价格,名称等)。我想知道在任何情况下都可以使用简单的方法对商品使用order by进行1个灵活的查询。因为我的代码返回了无序的对象列表。
因此,在此项目中,我在用户在组合框元素中选择排序类型后将请求发送到服务器。在我的控制器选择排序方式之后,它将调用查询,然后返回我们的对象。
请帮忙,您是否有机会告诉我,在Controller或Service Implementation中,哪种switch(order_type)更好的方式?
P.S。如果使用[http://localhost:9999/commodities/category-name/CPU/c.name%20asc控制器将请求发送到服务器,则只能使用switch原因,因为控制器仅看到不带.name asc的c,这就是为什么我使用switch的原因。
代码:
控制器:
@RestController
@RequestMapping("commodities")
public class CommodityController {
@GetMapping("category-name/{name}/{order_type}")
public ResponseEntity<List<CommodityDTO>> getCommodityByCategoryNameWithOrder(@PathVariable("name") String categoryName, @PathVariable("order_type") String order_type){
List<CommodityDTO> byCategoryWithOrder;
switch(order_type) {
case "name_asc":
byCategoryWithOrder = commodityService.getAllByCategoryNameWithOrder(categoryName, "c.name asc");
break;
case "name_desc":
byCategoryWithOrder = commodityService.getAllByCategoryNameWithOrder(categoryName, "c.name desc");
break;
case "price_asc":
byCategoryWithOrder = commodityService.getAllByCategoryNameWithOrder(categoryName, "c.price asc");
break;
case "price_desc":
byCategoryWithOrder = commodityService.getAllByCategoryNameWithOrder(categoryName, "c.price desc");
break;
default:
return null;
}
return new ResponseEntity<List<CommodityDTO>>(byCategoryWithOrder,HttpStatus.OK);
}
服务实施:
@Service
@Transactional
public class CommodityServiceImpl implements CommodityService{
@Autowired
private CommodityRepository commodityRepository;
@Autowired
private ObjectMapperUtils objectMapperUtils;
@Autowired
private CloudinaryService cloudinaryService;
@Override
public List<CommodityDTO> getAllByCategoryNameWithOrder(String categoryName, String order_type){
System.out.println("\n\n\n\nOrder type:\n"+order_type+"\n\n\n\n");
return objectMapperUtils.mapAll(commodityRepository.findAllByCategoryNameWithOrder(categoryName, order_type), CommodityDTO.class);
}
}
存储库:
public interface CommodityRepository extends JpaRepository<Commodity, Integer>{
@Query("Select c FROM Commodity c "
+ "Join Category ct on c.category.id = ct.id "
+ "where ct.name = ?1 "
+ "order by ?2")
List<Commodity> findAllByCategoryNameWithOrder(String categoryName, String order_type);
}
答案 0 :(得分:2)
您应该看看如何使用QueryDSL扩展来处理Spring数据。结合Spring Data Web扩展,可以让您通过属性的任意组合来过滤实体,并应用排序和分页,而无需编写任何代码。
将以下内容添加到您的配置中,然后您的代码将如下所示:
@EnableSpringDataWebSupport
https://docs.spring.io/spring-data/jpa/docs/current/reference/html/#core.extensions.querydsl
https://docs.spring.io/spring-data/jpa/docs/current/reference/html/#core.web.type-safe
在此处查看如何设置项目以支持QueryDsl:
https://www.baeldung.com/querydsl-with-jpa-tutorial
控制器
@GetMapping("/searchCommodities")
public ResponseEntity<List<CommodityDTO>> findCommodities(
@QuerydslPredicate(root = Commodity.class) Predicate predicate,Pageable pageable){
return new ResponseEntity<List<CommodityDTO>>
(commodityService.getCategories(predicate, pageable), HttpStatus.OK);
}
服务
public class CommodityServiceImpl implements CommodityService{
@Override
public List<CommodityDTO> getCategories(Predicate predicate, Pageable pageable){
return objectMapperUtils.mapAll(commodityRepository.findAll(
predicate, pageable), CommodityDTO.class);
}
存储库
public interface CommodityRepository extends JpaRepository<Commodity, Integer>,
QueryDslPredicateExecutor<Commodity>{
//no query methods required
}
您现在可以拨打,例如:
/searchCommodities?name=someName&sort=someProperty
/searchCommodities?name=someName&someOtherproperty=xyz&sort=someProperty,desc&sort=someProperty,asc
/searchCommodities?name=x&name=y&name=z //name = x or y or z
答案 1 :(得分:0)
有两种选择: