亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

我們如何在 CrudRepository 中實現自定義查找方法以僅從數據庫中獲取特定列?

我們如何在 CrudRepository 中實現自定義查找方法以僅從數據庫中獲取特定列?

胡子哥哥 2023-09-20 15:17:56
我必須在我的存儲庫中執行 SQL 查詢:public interface UserRequestResponseRepository extends JpaRepository<UserRequestResponse, Integer> {    //public static final String FIND_QUERY = "select user.u_httpstatus ,user.u_queryparam from UserRequestResponse user";    public static final String FIND_QUERY =             "select new com.abc.datacollection.entity.UserRequestResponse(user.u_httpstatus ,user.u_queryparam) from UserRequestResponse user";    @Query(value = FIND_QUERY)    public List<UserProjection> getAllRequestResponseRecords();}其中 UserProjection 是我定義的投影:public interface UserProjection {    String getU_httpstatus();    String getU_queryparam();}userRequestResponse 類具有比 u_httpstatus 和 u_queryparam 更多的字段,但我只想在響應中使用這 2 個字段。public @ResponseBody List<UserRequestResponse> getAllRequestResponseRecords() {    return userRequestResponseRepository.findAll() ;}如何修改上面的代碼 (findAll()) 以從我的自定義查詢中獲取結果,而不是從默認的 CrudRepository findAll() (返回所有字段)中獲取結果。
查看完整描述

1 回答

?
萬千封印

TA貢獻1891條經驗 獲得超3個贊

首先,您不需要添加 a@Query來使投影起作用。只需將UserProjection存儲庫中的方法的返回類型作為方法的返回類型就足夠了。

其次,您可以在存儲庫中將以下方法作為基于投影的 findAll 方法;

public?List<UserProjection>?findAllProjectedBy();


查看完整回答
反對 回復 2023-09-20
  • 1 回答
  • 0 關注
  • 113 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號