java—我创建了一个带有dto的spring boot,实体,但也无法存储它

lo8azlld  于 2021-06-29  发布在  Java
关注(0)|答案(2)|浏览(270)

我用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 中发送数据时,它向我显示它正在存储空值,我现在不知道,有人能帮我解决这个问题,能帮我把代码改进到最好的版本吗

gdrx4gfi

gdrx4gfi1#

现在它运行良好,我添加了以下更改:
在spring配置中定义modelmapper bean:

@Bean
public ModelMapper modelMapper() {
    return new ModelMapper();
}

附属国

<dependency>
<groupId>org.modelmapper</groupId>
<artifactId>modelmapper</artifactId>
<version>2.3.5</version>

除了上面的修改,其余的都很好,但请进一步优化你的代码。
使用 Postman 测试

qlzsbp2j

qlzsbp2j2#

所以在google搜索了很多遍之后,我发现可能是某个构造器或其他东西出了问题,所以我需要为dto类定义@data注解,它就成功了

import lombok.Data;

@Data
public class MarksListDto {
    private Long id;
    private String subjectName;
    private Integer marks;

}

我在我的其他dto类中做了类似的更改,它起了作用,并且您必须将导入添加到gradle的依赖项中

compileOnly 'org.projectlombok:lombok'
    annotationProcessor 'org.projectlombok:lombok'

相关问题