如何使用 Spring 启动为传记后端制作实体 class 和控制器?

How to make entity class and controller for biography backend using Spring Boot?

如何为传记页面(在网站上)的后端部分制作实体 class。我不确定如何处理这样的事情,因为不需要从服务器发送特定的东西。我附上了一些用于我的实体 class 的代码。

我的实体 class 看起来是使用 Spring Boot 在网站上为传记页面创建后端的正确方法吗?

实体Class

    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 + "]";
        }

    }

您可以执行以下操作来实现 Spring 控制器来处理对 Biography 实体的请求。

  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 使用存储库。

希望对您有所帮助。