如何使用Spring Boot为传记后端创建实体类和控制器?

时间:2019-10-02 23:32:16

标签: java spring spring-boot persistence

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

我的实体类似乎是使用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 个答案:

答案 0 :(得分:1)

在这里您可以实现一个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...
}

根据您的需求,更好的做法是通过Controller中的@Service使用存储库。

希望有帮助。