我的网络应用程序中有一个过滤器,允许按车辆类型、品牌、燃料、州和城市进行搜索,但所有这些过滤器都是可选的。
我怎样才能使用存储库来做到这一点。
控制器类
@RequestMapping(value = "/vehicle/search", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
public Iterable<Veiculo> findBySearch(@RequestParam Long vehicletype, @RequestParam Long brand,
@RequestParam Long model, @RequestParam Long year,
@RequestParam Long state, @RequestParam Long city) {
return veiculoService.findBySearch(vehicletype, brand, model, year, state, city);
}
服务等级
public Iterable<Vehicle> findBySearch(Long vehicletype, Long brand, Long model, Long year, Long state, Long city) {
if(vehicletype != null){
//TODO: filter by vehicletype
}
if(brand != null){
//TODO: filter by brand
}
if(model != null){
//TODO: filter by model
}
//OTHER FILTERS
return //TODO: Return my repository with personal query based on filter
}
我还没有实现任何东西,因为我不明白如何做这个过滤器。
车辆类别
@Entity
@Table(name = "tb_veiculo")
public class Veiculo {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id", nullable = false)
private Long id;
@ManyToMany(cascade = CascadeType.ALL)
@JoinTable(name = "veiculo_opcionais",
joinColumns = @JoinColumn(name = "veiculo_id", referencedColumnName = "id"),
inverseJoinColumns = @JoinColumn(name = "opcional_id", referencedColumnName = "id"))
private List<Opcional> opcionais;
@JsonIgnore
@OneToMany(mappedBy = "veiculo", fetch = FetchType.LAZY, cascade = CascadeType.ALL)
private List<VeiculoImagem> veiculoImagens;
@ManyToOne(fetch = FetchType.EAGER, cascade = CascadeType.ALL)
@JoinColumn(name = "cambio_id", foreignKey = @ForeignKey(name = "fk_cambio"))
private Cambio cambio;
@ManyToOne(fetch = FetchType.EAGER, cascade = CascadeType.ALL)
@JoinColumn(name = "combustivel_id", foreignKey = @ForeignKey(name = "fk_combustivel"))
private Combustivel combustivel;
@ManyToOne(fetch = FetchType.EAGER, cascade = CascadeType.ALL)
@JoinColumn(name = "cor_id", foreignKey = @ForeignKey(name = "fk_cor"))
private Cor cor;
@ManyToOne(fetch = FetchType.EAGER, cascade = CascadeType.ALL)
@JoinColumn(name = "modelo_id", foreignKey = @ForeignKey(name = "fk_modelo"))
private Modelo modelo;
@ManyToOne(fetch = FetchType.EAGER, cascade = CascadeType.ALL)
@JoinColumn(name = "usuario_id", foreignKey = @ForeignKey(name = "fk_usuario"))
private Usuario usuario;
@Column(name = "anoFabricacao", nullable = false)
private int anoFabricacao;
@Column(name = "anoModelo", nullable = false)
private int anoModelo;
@Column(name = "quilometragem", nullable = false)
private int quilometragem;
@Column(name = "porta", nullable = false)
private int porta;
@Column(name = "valor", nullable = false)
private double valor;
//GETTERS AND SETTERS
车辆类型和品牌来自另一张表...我是葡萄牙语,我已将代码翻译成英语...
当它发生时,我需要做什么?