我正在開發(fā)一個 Spring 應(yīng)用程序,它記錄用戶請求響應(yīng)(將其發(fā)送到數(shù)據(jù)庫并檢索它)。獲取記錄代碼是這樣的(控制器):/** * this method fetches all userrequest response records from user_request_response table * @return */ @CrossOrigin(origins = "*") @GetMapping(path="/all") public @ResponseBody Iterable<UserRequestResponse> getAllRequestResponseRecords() { return userRequestResponseRepository.findAll(); }Dao code:import org.springframework.data.jpa.repository.Query;import org.springframework.data.repository.CrudRepository;import com.abc.datacollection.entity.UserProjection;import com.abc.datacollection.entity.UserRequestResponse;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) from UserRequestResponse user"; @Query(value = FIND_QUERY) List<UserProjection> getAllRequestResponseRecords();}具有 getter 和 setter 的類:(刪除了一些變量及其 getter 和 setter)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 int getSys_id() { return sys_id; }public String getU_httpstatus() { return u_httpstatus; }MySQL查詢示例(我試圖將查詢放入CrudRepository中的java代碼中,InternSearchAnalytics.user_request_response是表的名稱):select u_httpstatus, u_queryparam from InternSearchAnalytics.user_request_response但是,當我運行 java 代碼并點擊生成的端點時,我得到了所有字段。有人可以幫我嗎?我已經(jīng)被這個問題困擾了兩天了。
1 回答

烙印99
TA貢獻1829條經(jīng)驗 獲得超13個贊
您可以使用類中的新構(gòu)造函數(shù)從查詢中獲取選定的字段,UserRequestResponse例如
public UserRequestResponse(String u_httpstatus, String u_queryparam) {
this.u_httpstatus = u_httpstatus;
this.u_queryparam = u_queryparam;
}
對于查詢,您應(yīng)該使用構(gòu)造函數(shù)更改它
public static final String FIND_QUERY =
"select new com.your.package.name.UserRequestResponse(user.u_httpstatus ,user.u_queryparam) from UserRequestResponse user";
一點是您需要使用具有完全限定名稱的構(gòu)造函數(shù)。希望這可以幫助。
添加回答
舉報
0/150
提交
取消