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

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

如何使用Spring Boot為傳記后端制作實體類和控制器?

如何使用Spring Boot為傳記后端制作實體類和控制器?

慕森王 2023-09-20 16:21:11
如何為傳記頁面(在網站上)的后端部分創建一個實體類。我不確定如何處理這樣的事情,因為沒有需要從服務器發送的特定內容。我附上了一些用于實體類的代碼。我的實體類似乎是使用 Spring Boot 在網站上為傳記頁面創建后端的正確方法嗎?實體類    import javax.persistence.Column;    import javax.persistence.Entity;    import javax.persistence.GeneratedValue;    import javax.persistence.Id;    import javax.persistence.Table;    @Entity    @Table(name="BIOGRAPHY")    public class Biography {        @Id        @GeneratedValue        private Long sectionId;        @Column(name = "section_title")        private String titleSection;        @Column(name = "section_text")        private String textSection;        public Long getSectionId() {            return sectionId;        }        public String getTitleSection() {            return titleSection;        }        public String getTextSection() {            return textSection;        }        @Override        public String toString() {            return "EmployeeEntity [sectionId=" + sectionId + ", titleSection=" + titleSection +                    ", textSection=" + textSection + "]";        }    }
查看完整描述

1 回答

?
不負相思意

TA貢獻1777條經驗 獲得超10個贊

您可以執行以下操作來實現一個處理對 Biography 實體的請求的 Spring 控制器。

  1. 您的傳記實體似乎不錯

  2. 要使用它,您可以利用org.springframework.data.repository.CrudRepository;
    即:

public interface BiographyRepository  extends CrudRepository <Biography, Long> {

}
  1. Spring 非常靈活,您可以按照自己喜歡的方式組織代碼。以下是如何組織控制器代碼的示例:

@RestController

@RequestMapping 

public class BiographyController {


  @Autowired

  private BiographyRepository biographyRepository;


  @RequestMapping(value = "/biography, method = RequestMethod.POST)

  public @ResponseBody

  Response create (HttpServletRequest request) {

    //read biography object from the request

    biographyRepository.save(biography);

  }


  //other methods...

}

根據您的需要,更好的做法可能是通過@Service控制器中的存儲庫進行操作。


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

添加回答

舉報

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

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

公眾號

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