我刚开始接触Spring Boot和Hibernate。这里我试图运行一个基于可选参数查询的搜索,可以按名称、国家等进行搜索。如果我将此字段保持为空,则查询应返回全部列表。但问题在于,我的方法返回所有数据,忽略了我的搜索参数。我的模型类如下:
@Entity(name="MLFM_ORDER_OWNER")
public class ModelOrderOwner {
@Id @GenericGenerator(name = "custom_sequence", strategy =
"com.biziitech.mlfm.IdGenerator")
@GeneratedValue(generator = "custom_sequence")
@Column(name="ORDER_OWNER_ID")
private Long orderOwnerId;
@Column(name="OWNER_NAME")
private String ownerName;
@OneToOne
@JoinColumn(name="BUSINESS_TYPE_ID")
private ModelBusinessType businessTypeId;
@Column(name="SHORT_CODE")
private String shortCode;
@ManyToOne
@JoinColumn(name="OWNER_COUNTRY")
private ModelCountry ownerCountry;
// getter setter..
我的代码库接口长这样
public interface OrderOwnerRepository extends
JpaRepository<ModelOrderOwner,Long>{
@Query("select a from MLFM_ORDER_OWNER a where a.businessTypeId.typeId=coalsec(:typeId,a.businessTypeId.typeId) and a.ownerCountry.countryId=coalsec(:countryId,a.ownerCountry.countryId) and a.ownerName LIKE %:name and a.shortCode LIKE %:code")
public List <ModelOrderOwner> findOwnerDetails(@Param("typeId")Long typeId,@Param("countryId")Long countryId,@Param("name")String name,@Param("code")String code);
}
以下是我的控制器方法:
@RequestMapping(path="/owners/search")
public String getAllOwner(Model model,@RequestParam("owner_name") String name,@RequestParam("shortCode") String code,
@RequestParam("phoneNumber") String phoneNumber,@RequestParam("countryName") Long countryId,
@RequestParam("businessType") Long typeId
) {
model.addAttribute("ownerList",ownerRepository.findOwnerDetails(typeId, countryId, name, code));
return "data_list";
}
有人能在这方面帮助我吗?拜托了。