eclipse中搭建springboot学习(9)---JPA使用3(多张表 OneToOne)

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();
    }

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值