【问题标题】:Rest api via spring通过spring休息api
【发布时间】:2018-01-01 08:19:56
【问题描述】:

我必须使用条件 select 查询其中字段 isdeleted=0, location=1 从表中的 MySQL 获取所有数据。我如何在存储库和访问管理器中实现这一点。

public interface FoodCourtRepository  extends JpaRepository<FoodCourtEntity, Long> {
    List<FoodcaseEntity> findByIsdeleted(Boolean isDeleted);
}

在访问管理器中

public List<FoodcaseDO> getAllFoodCourt() {
    List<FoodcaseEntity> foodCaseList = foodcourtRepository.findByIsdeleted(false);
}

【问题讨论】:

    标签: java mysql rest spring-mvc


    【解决方案1】:

    您还需要为位置添加另一个条件,例如:

    public List<FoodcaseEntity> findByIsdeletedAndLocation(boolean deleted, int location);
    

    并以false1 作为参数调用它,例如:

    List<FoodcaseEntity> foodCaseList = foodcourtRepository.findByIsdeletedAndLocation(false, 1);
    

    这应该会给你所需的结果。

    更新

    如果你想获取多个位置的数据,那么你需要编写一个支持IN的方法,例如:

    public List<FoodcaseEntity> findByIsdeletedAndLocationIn(boolean deleted, List<Integer> location);
    

    然后这样称呼它:

    List<FoodcaseEntity> foodCaseList = foodcourtRepository.findByIsdeletedAndLocation(false, Arrays.asList(2,3,4,5));
    

    【讨论】:

    • 在存储库中发现错误----(无效的派生查询!找不到布尔类型的属性和交付位置!遍历的路径:FoodCourtEntity.isdeleted。)
    • 先生,此方法显示 json 数据,但是当我在 url 中更改位置 =2 时,显示相同的数据,如位置 = 1 localhost:8080/court/court/list?location=1 现在如果我更改位置 = 2 相同的数据来自位置 = 1 localhost:8080/court/court/list?location=2,3,4,5
    • @bkumar 更新了答案。基本上,您需要编写另一个支持IN查询的方法来获取多个位置的数据。
    【解决方案2】:

    在存储库类中添加,

    List<FoodcaseEntity> foodCaseList = foodcourtRepository.findByIsdeletedAndLocation(boolean isSelected,int location);
    

    然后你可以发送选择和位置值。它将根据条件返回值列表。

    【讨论】:

      【解决方案3】:

      JPA 提供@Query 来编写自定义查询。您可以定义另一个名为 FoodCourtRepositoryCustom 的接口并编写自定义查询,如下所示:

      public interface FoodCourtRepositoryCustom {
       @Query("SELECT fe FROM FoodcaseEntity fe WHERE fe.isdeleted=?1 AND fe.location=?2 ")
       List<FoodcaseEntity> findByIsdeletedAndLocation(Boolean isDeleted, Integer location);
      }
      

      然后在您的存储库接口中扩展此接口,如下所示:

      public interface FoodCourtRepository  extends JpaRepository<FoodCourtEntity, Long>, FoodCourtRepositoryCustom{
      List<FoodcaseEntity> findByIsdeleted(Boolean isDeleted);
      }
      

      现在方法在您的访问管理器中可用。

      public List<FoodcaseDO> getAllFoodCourt() {
      List<FoodcaseEntity> foodCaseList = foodcourtRepository.findByIsdeleted(false);
      List<FoodcaseEntity> foodCaseList = foodcourtRepository.findByIsdeletedAndLocation(false, 1);
      }
      

      【讨论】:

      • 先生,此方法显示 json 数据,但是当我在 url 中更改 location =2 时,显示相同的数据,如 location=1 localhost:8080/court/court/list?location=1 现在如果我更改location=2 相同的数据来自 location =1 localhost:8080/court/court/list?location=2,3,4,5
      • 您能否在 FoocaseEntity 表的评论中发布示例数据,以便我检查并帮助您。
      • 字符串名称;字符串地址;字符串描述; Boolean isdeleted;int location;现在如果 isdeleted =0,并且位置 =1 列表必须在 json 中显示数据但是当我签入 url -----localhost:8080/court/court/list?location=1.it 显示 json 数据但是当我更改时location=2 它显示相同的数据
      • @Column(name = "name", length = 45, nullable = false)private Stringname;@Column(name = "address", length = 45, nullable = false) private String address;@ Column(name = "description", length = 100, nullable = true)private String description;@Column(name = "IS_DELETED", length = 3, nullable = false)private Boolean isdeleted;@Column(name = "location", length = 15, nullable = false) 私有 int 位置;
      • 我认为您没有更改作为位置参数传递的硬编码值。
      猜你喜欢
      • 2021-10-28
      • 2019-07-10
      • 1970-01-01
      • 1970-01-01
      • 2017-07-07
      • 2015-12-28
      • 2011-01-15
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多