在MySQL中搜寻

问题描述

我正在尝试在Spring Boot的存储库中创建一个函数搜索检查条件的元素:

@Query(value = "select *from `trash_can_response` WHERE temperature=:'21' OR distance=:'24' OR humidity=:'82' ",nativeQuery = true)
    public TrashCanResponse notif(); 

这是我的数据示例:

{
    "id": 42,"humidity": 82,"distance": 23,"temperature": 21
  },{
    "id": 43,{
    "id": 44,{
    "id": 45,"distance": 24,{
    "id": 46,{
    "id": 47,{
    "id": 50,{
    "id": 51,{
    "id": 54,

我想使用函数notif()返回获得temperature=21 OR humidity =21 OR distance =24的人;

我知道我的查询代码有误,您能为我更正吗?


使用JPA的用户

@Entity
@Table(name = "TrashCanResponse")
@EntityListeners(AuditingEntityListener.class)
@JsonIgnoreProperties(value = { "createdAt","updatedAt" },allowGetters = true)

@EnableJpaAuditing
public class TrashCanResponse {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    @NotBlank
    private Integer humidity;
    
    @Column(unique=true)
    @NotBlank
    private Integer distance;

    @NotBlank
    private Integer temperature;

   

    @Column(updatable = false)
    @CreationTimestamp
    private LocalDateTime createdAt;
    @UpdateTimestamp
    private LocalDateTime updatedAt;

    

    public TrashCanResponse() {
    }

    public Integer getTemperature() {
        return temperature;
    }

    public void setTemperature(Integer temperature) {
        this.temperature = temperature;
    }

    public TrashCanResponse(Integer temperature,Integer humidity,Integer distance) {
             this.setTemperature(temperature);
            this.distance=distance;
            this.humidity=humidity;
            
    }

        // Getters and Setters

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }



    public Integer getHumidity() {
        return humidity;
    }

    public void setHumidity(Integer humidity) {
        this.humidity = humidity;
    }

    public Integer getdistance() {
        return distance;
    }

    public void setdistance(Integer distance) {
        this.distance = distance;
    }

  

 
    
}

一切都很好,只是我想找一个检查我情况的特定行,因为我在查询中很糟糕..

解决方法

您的方法应具有某种返回类型。
您可以如下使用@Query,而不使用nativeQuery

@Query(value = "select TCR from TrashCanResponse TCR WHERE TCR .temperature=21 OR TCR .distance=24 OR TCR .humidity=82 ")
public List<TrashCanResponse> TrashCanResponse notif();