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

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

CrudRepository 中的自定義 SQL 查詢

CrudRepository 中的自定義 SQL 查詢

侃侃爾雅 2023-09-20 15:53:08
我正在嘗試在擴展 CrudRepository 的存儲庫中執行一些 SQL 查詢。我在控制器中有以下代碼:@CrossOrigin(origins = "*")    @GetMapping(path="/all")    public @ResponseBody List<UserProjection> getAllRequestResponseRecords() {        return userRequestResponseRepository.findAllProjectedBy() ;    }DAO代碼如下:public interface UserRequestResponseRepository extends CrudRepository<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();     List<UserProjection> findAllProjectedBy();}班級是:import java.sql.Date;import javax.persistence.Entity;import javax.persistence.GeneratedValue;import javax.persistence.GenerationType;import javax.persistence.Id;@Entity // This tells Hibernate to make a table out of this classpublic class UserRequestResponse {    @Id    @GeneratedValue(strategy = GenerationType.IDENTITY)    private String u_httpstatus;    private String u_error_message;    private String u_queryparam;    public UserRequestResponse(String u_httpstatus, String u_queryparam) {        this.u_httpstatus = u_httpstatus;        this.u_queryparam = u_queryparam;    }    public String getU_httpstatus() {        return u_httpstatus;    }    public void setU_httpstatus(String u_httpstatus) {        this.u_httpstatus = u_httpstatus;    }    public String getU_error_message() {        return u_error_message;    }    public void setU_error_message(String u_error_message) {        this.u_error_message = u_error_message;    }我對如何添加像這樣的查詢感到困惑:select u_type,count(u_type) from u_user_click_data group by u_type如何更改投影以及其他必要的更改是什么?
查看完整描述

1 回答

?
DIEA

TA貢獻1820條經驗 獲得超2個贊

您可以將 DAO 更改為以下內容,這應該可以工作。


public interface UserRequestResponseRepository extends CrudRepository<UserRequestResponse, Integer> {

public static final String FIND_QUERY =?

"select new com.abc.datacollection.entity.UserRequestResponse(user.u_httpstatus ,user.u_queryparam, COUNT(user.u_type)) from UserRequestResponse user GROUP BY user.u_type";

@Query(value = FIND_QUERY)

//public List<UserProjection> getAllRequestResponseRecords();

?List<UserProjection> findAllProjectedBy();

}


確保 Bean 類構造函數應該具有傳遞參數。



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

添加回答

舉報

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

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

公眾號

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