如何为传记页面(在网站上)的后端部分创建一个实体类。我不确定如何处理这样的事情,因为没有需要从服务器发送的特定内容。我附上了一些用于实体类的代码。
我的实体类似乎是使用 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 + "]";
}
}
最佳答案
您可以执行以下操作来实现一个处理对 Biography 实体的请求的 Spring Controller 。
- 您的传记实体似乎不错
- 要使用它,您可以利用
org.springframework.data.repository.CrudRepository;
即:
public interface BiographyRepository extends CrudRepository <Biography, Long> {
}
- Spring 非常灵活,您可以按照自己喜欢的方式组织代码。以下是如何组织 Controller 代码的示例:
@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
来使用存储库。
希望有帮助。
关于java - 如何使用Spring Boot为传记后端制作实体类和 Controller ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58210573/