StudentDTO
package com.example.demo1025.dto;
import java.io.Serializable;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.OneToOne;
import javax.persistence.Table;@Entity
@Table(name = "student")
public class StudentDTO implements Serializable {private static final long serialVersionUID = 1L;
@Id
@GeneratedValue
@Column(name = "student_id")
private int id;private String studentName;
@OneToOne(cascade = CascadeType.ALL)
// 设置cascade,否则会报错object references an unsaved transient instance - save the
// transient instance before flushing :
// com.example.demo1025.dto.StudentDTO.score ->
// com.example.demo1025.dto.ScoreDTO
private ScoreDTO score;public int getId() {
return id;
}public void setId(int id) {
this.id = id;
}public String getStudentName() {
return studentName;
}public void setStudentName(String studentName) {
this.studentName = studentName;
}public ScoreDTO getScoreDTO() {
return score;
}public void setScoreDTO(ScoreDTO scoreDTO) {
this.score = scoreDTO;
}}
ScoreDTO
package com.example.demo1025.dto;
import java.io.Serializable;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.OneToOne;
import javax.persistence.Table;@Entity
@Table(name = "score")
public class ScoreDTO implements Serializable {/**
*
*/
private static final long serialVersionUID = 1L;@Id
@GeneratedValue
private int id;private String eng_score;
private String math_score;// mappedBy填写的名称是在StudentDTO类中的名字,而不是表的名称,需要在非主体的那个类中标注出它在主体中的名字
@OneToOne(mappedBy = "score")
private StudentDTO studentDTO;public int getId() {
return id;
}public void setId(int id) {
this.id = id;
}public String getEng_score() {
return eng_score;
}public void setEng_score(String eng_score) {
this.eng_score = eng_score;
}public String getMath_score() {
return math_score;
}public void setMath_score(String math_score) {
this.math_score = math_score;
}}
StudentScoreRepository
package com.example.demo1025.dao;
import org.springframework.data.jpa.repository.JpaRepository;
import com.example.demo1025.dto.StudentDTO;
public interface StudentScoreRepository extends JpaRepository<StudentDTO, Integer> {
}
StudentScoreService
package com.example.demo1025.service;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;import com.example.demo1025.dao.StudentScoreRepository;
import com.example.demo1025.dto.ScoreDTO;
import com.example.demo1025.dto.StudentDTO;@Service
public class StudentScoreService {
@Autowired
private StudentScoreRepository studentScoreRepository;public void addStudentScore() {
ScoreDTO scoreDTO = new ScoreDTO();
scoreDTO.setEng_score("100");
scoreDTO.setMath_score("84");
StudentDTO studentDTO = new StudentDTO();
studentDTO.setStudentName("小明");
studentDTO.setScoreDTO(scoreDTO);
studentScoreRepository.save(studentDTO);
}public List<StudentDTO> getAllStudentScore() {
return studentScoreRepository.findAll();
}
}
StuentScoreController
package com.example.demo1025.controller;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;import com.example.demo1025.dto.StudentDTO;
import com.example.demo1025.service.StudentScoreService;@Controller
public class StuentScoreController {
@Autowired
private StudentScoreService studentScoreService;@RequestMapping("addStudentScore")
@ResponseBody
public String addStudentScore() {
studentScoreService.addStudentScore();
return "添加成功";
}@RequestMapping("getAllStudentScore")
@ResponseBody
public List<StudentDTO> getAllStudentScore() {
return studentScoreService.getAllStudentScore();
}}