Java GET方法中筛选对象所依据的可选属性
目前我有一个实体运动鞋定义为Java GET方法中筛选对象所依据的可选属性,java,spring,spring-boot,jpa,Java,Spring,Spring Boot,Jpa,目前我有一个实体运动鞋定义为 @Entity(name = "sneaker") public class Sneaker extends Product { @Column private BigDecimal size; @Enumerated(EnumType.STRING) @Column private BrandType brand; ... // getters, setters, constructors omitt
@Entity(name = "sneaker")
public class Sneaker extends Product {
@Column
private BigDecimal size;
@Enumerated(EnumType.STRING)
@Column
private BrandType brand;
...
// getters, setters, constructors omitted
}
以及一个@RestControllerget方法,该方法接受可选参数以根据数据库对象的属性筛选它们
@GetMapping
public ResponseEntity<List<Sneaker>> getSneakers(
@RequestParam Optional<List<BrandType>> brands,
@RequestParam Optional<List<BigDecimal>> sizes
) {
List<Sneaker> sneakers;
if (brands.isPresent() && sizes.isEmpty()) {
sneakers = sneakerService.getAllByBrands(Util.filterNullItems(brands.get()));
} else if (brands.isEmpty() && sizes.isPresent()) {
sneakers = sneakerService.getAllBySize(sizes.get());
} else if (brands.isPresent() && sizes.isPresent()) {
sneakers = sneakerService.getAllByBrandAndSize(brands.get(), sizes.get());
} else {
sneakers = sneakerService.getAll();
}
if (sneakers.isEmpty())
throw new RuntimeException("No Sneakers were found");
return ResponseEntity
.ok(sneakers);
}
@GetMapping
公众响应运动鞋(
@RequestParam可选品牌,
@RequestParam可选大小
) {
列出运动鞋;
if(brands.isPresent()&&size.isEmpty()){
sneakers=sneakerService.getAllByBrands(Util.filterNullItems(brands.get());
}else if(brands.isEmpty()&&size.isPresent()){
sneakers=sneakerService.getAllBySize(size.get());
}else if(brands.isPresent()&&size.isPresent()){
sneakers=sneakerService.getAllByBrandAndSize(brands.get(),size.get());
}否则{
sneakers=sneakerService.getAll();
}
if(sneakers.isEmpty())
抛出新的RuntimeException(“未找到运动鞋”);
返回响应性
.ok(运动鞋);
}
根据给定参数查询数据库的最佳实践是什么?我假设控制器中的多个if-else语句不是最好的方法,因为向一个对象添加更多属性会使我的代码呈指数级扩展,并造成混乱。我是否应该查询所有(或仅通过某些属性)对象并用流过滤它们?我建议使用
您的服务方式:
@Service
public class SneakerService {
@Autowired
SneakerRepository repository;
public List<Sneaker> getSneakers(Optional<List<BrandType>> brands, Optional<List<BigDecimal>> sizes) {
Specification<Sneaker> spec =
Specifications.where(SneakerSpecifications.brandIn(brands))
.and(SneakerSpecifications.sizeIn(sizes));
return repository.findAll(spec);
}
}
@服务
公共类SneakerService{
@自动连线
储存库;
公共列表getSneakers(可选品牌、可选尺寸){
规格规格=
规范.其中(运动鞋规范.品牌)
和(运动鞋规格尺寸);
返回repository.findAll(spec);
}
}
如果不使用Spring数据Jpa
,则相同的方法可用于标准API
或Querydsl
更多信息我建议使用
您的服务方式:
@Service
public class SneakerService {
@Autowired
SneakerRepository repository;
public List<Sneaker> getSneakers(Optional<List<BrandType>> brands, Optional<List<BigDecimal>> sizes) {
Specification<Sneaker> spec =
Specifications.where(SneakerSpecifications.brandIn(brands))
.and(SneakerSpecifications.sizeIn(sizes));
return repository.findAll(spec);
}
}
@服务
公共类SneakerService{
@自动连线
储存库;
公共列表getSneakers(可选品牌、可选尺寸){
规格规格=
规范.其中(运动鞋规范.品牌)
和(运动鞋规格尺寸);
返回repository.findAll(spec);
}
}
如果不使用Spring数据Jpa
,则相同的方法可用于标准API
或Querydsl
更多信息
@Service
public class SneakerService {
@Autowired
SneakerRepository repository;
public List<Sneaker> getSneakers(Optional<List<BrandType>> brands, Optional<List<BigDecimal>> sizes) {
Specification<Sneaker> spec =
Specifications.where(SneakerSpecifications.brandIn(brands))
.and(SneakerSpecifications.sizeIn(sizes));
return repository.findAll(spec);
}
}