【发布时间】:2021-05-21 04:59:50
【问题描述】:
我想使用以下原生查询在 Postman 中显示来自 2 个不相关对象的结果:
@Query(value = "select * from product p where p.id like concat('%', :productId, '%') ", nativeQuery = true)
List<Product> findProductById(String productId);
@Query(value = "select * from customer c where c.id like concat('%',:id,'%')", nativeQuery = true)
List<Customer> findCustomerById(String id);
我想用条件添加他们的结果:
- 如果productId为空,我只想显示
List<Customer>的结果,反之亦然 - 如果两者都为空/不为空,则应显示查询、产品和客户的结果,如下所示:
@Override
public List<Object> findById(String customerId, String productId) {
List<Object> obj = new ArrayList<>();
if(customerId.isEmpty()){
obj.addAll(productRepository.findProductById(productId));
} else if(productId.isEmpty()){
obj.addAll(customerRepository.findCustomerById(customerId));
} else {
obj.addAll(productRepository.findProductById(productId));
obj.addAll(customerRepository.findCustomerById(customerId));
}
return obj;
}
这是我的控制器:
@GetMapping("/custom")
public List<Object> findById(@RequestParam(name = "customer") String customer, @RequestParam(name = "product") String product){
return service.findById(customer, product);
}
但是,在 Postman 中,当两者都为空时,它们仅显示来自 List<Product> 的结果。有什么办法可以同时显示吗?
【问题讨论】:
标签: java spring-boot controller postman nativequery