滄海一幻覺(jué)
2023-09-20 14:52:26
我正在開(kāi)發(fā)一個(gè) Spring 應(yīng)用程序,它記錄用戶(hù)請(qǐng)求響應(yīng)(將其發(fā)送到數(shù)據(jù)庫(kù)并檢索它)。獲取記錄代碼是這樣的(控制器):/** * 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 的類(lèi):(刪除了一些變量及其 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查詢(xún)示例(我試圖將查詢(xún)放入CrudRepository中的java代碼中,InternSearchAnalytics.user_request_response是表的名稱(chēng)):select u_httpstatus, u_queryparam from InternSearchAnalytics.user_request_response但是,當(dāng)我運(yùn)行 java 代碼并點(diǎn)擊生成的端點(diǎn)時(shí),我得到了所有字段。有人可以幫我嗎?我已經(jīng)被這個(gè)問(wèn)題困擾了兩天了。
1 回答

烙印99
TA貢獻(xiàn)1829條經(jīng)驗(yàn) 獲得超13個(gè)贊
您可以使用類(lèi)中的新構(gòu)造函數(shù)從查詢(xún)中獲取選定的字段,UserRequestResponse例如
public UserRequestResponse(String u_httpstatus, String u_queryparam) {
this.u_httpstatus = u_httpstatus;
this.u_queryparam = u_queryparam;
}
對(duì)于查詢(xún),您應(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";
一點(diǎn)是您需要使用具有完全限定名稱(chēng)的構(gòu)造函數(shù)。希望這可以幫助。
添加回答
舉報(bào)
0/150
提交
取消