我用dto、entity创建了一个spring引导,也从dto生成了entity,反之亦然,但是当我试图获取数据时,数据没有存储在postgres sql中,它显示为null,但是首先创建的是controller类
import com.school.Entity.StudentDetails;
import com.school.Service.StudentService;
import com.school.dto.StudentDetailDto;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;
@RestController
@RequestMapping("/api")
@CrossOrigin("*")
public class SchoolController {
@Autowired
StudentService studentService;
@GetMapping("/test")
public String test(){
return "test";
}
@PostMapping("/uploadStudentData")
public StudentDetailDto createStudentDetails(@RequestBody StudentDetailDto dto){
return studentService.createStudentDetails(dto);
}
@GetMapping("/getAll")
public List<StudentDetails> getAllStudentData(){
return studentService.getAllStudentData();
}
}
现在学生们开始上课了
import java.util.List;
public class StudentDetailDto {
private Long id;
private String branch;
private List<StudentDto> student;
//getter and setter
}
现在学生开始上课了
public class StudentDto {
private Long id;
private String firstName;
private String lastName;
private List<MarksListDto> marksList;
// getter and setter
}
现在是marks list类
public class MarksListDto {
private Long id;
private String subjectName;
private Integer marks;
// getter and setter
}
现在是schooldetails实体类
@Entity
public class StudentDetails {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String branch;
@OneToMany(targetEntity=Students.class, mappedBy="id", fetch=FetchType.EAGER,cascade=CascadeType.ALL)
private List<Students> student;
//getter and setter
}
现在学生实体类
@Entity
public class Students {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String firstName;
private String lastName;
@OneToMany(targetEntity=MarksList.class, mappedBy="id", fetch=FetchType.EAGER,cascade=CascadeType.ALL)
private List<MarksList> marksList;
//getter and setter
}
现在标记列表实体
@Entity
public class MarksList {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String subjectName;
private int marks;
//getter and setter
}
存储库
public interface StudentRepository extends JpaRepository<StudentDetails, Long> {
}
现在最重要的是,我认为有什么问题是服务器
import com.school.Entity.StudentDetails;
import com.school.Repository.StudentRepository;
import com.school.dto.StudentDetailDto;
import org.modelmapper.ModelMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import java.util.List;
@Service
public class StudentService {
@Autowired
StudentRepository studentRepository;
@Autowired
ModelMapper modelMapper;
public StudentDetailDto createStudentDetails(StudentDetailDto dto) {
validateDto(dto);
StudentDetails studentDetails = generateEntityFromDto(dto);
studentDetails = studentRepository.save(studentDetails);
return generateDtoFromEntity(studentDetails);
}
private StudentDetails generateEntityFromDto(StudentDetailDto dto) {
StudentDetails studentDetails = modelMapper.map(dto, StudentDetails.class);
return studentDetails;
}
private StudentDetailDto generateDtoFromEntity(StudentDetails studentDetails){
StudentDetailDto dto = modelMapper.map(studentDetails,StudentDetailDto.class);
return dto;
}
private void validateDto(StudentDetailDto dto) {
}
// public List<StudentDetailDto> getAllStudentData() {
// List<StudentDetailDto> dto = new ArrayList<>();
// studentRepository.findAll().forEach(dto::add);
// return dto;
// }
// public String createStudentDetails(StudentDetails studentDetails){
// studentRepository.save(studentDetails);
// return "Student details created";
// }
//
public List<StudentDetails> getAllStudentData() {
return new ArrayList<>(studentRepository.findAll());
}
}
所以,当我在 Postman 中发送数据时,它向我显示它正在存储空值,我现在不知道,有人能帮我解决这个问题,能帮我把代码改进到最好的版本吗
2条答案
按热度按时间gdrx4gfi1#
现在它运行良好,我添加了以下更改:
在spring配置中定义modelmapper bean:
附属国
除了上面的修改,其余的都很好,但请进一步优化你的代码。
使用 Postman 测试
qlzsbp2j2#
所以在google搜索了很多遍之后,我发现可能是某个构造器或其他东西出了问题,所以我需要为dto类定义@data注解,它就成功了
我在我的其他dto类中做了类似的更改,它起了作用,并且您必须将导入添加到gradle的依赖项中