Bootstrap

spring boot 多对一表连接

spring boot 多对一表连接

Model层
Person.java

package com.sdbairui.demo.Entity;

import com.fasterxml.jackson.annotation.JsonFormat;
import org.springframework.format.annotation.DateTimeFormat;

import javax.persistence.*;
import java.util.Date;
@Table
@Entity(name="person")
public class Person {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private int id;
    private String name;
    private int sex;
    private int telephone;
    private String card;
    @DateTimeFormat(pattern = "yyyy-MM-dd")
    private Date birthday;
    private int sid;
//    这个是多对一
    @ManyToOne
//    name="sid": 这个是(学生)Person表中的sid,cid是(班级)classes表的
    @JoinColumn(name = "sid",referencedColumnName = "cid",insertable = false,updatable = false)
    private Classes classes;

    public int getSid() {
        return sid;
    }

    public void setSid(int sid) {
        this.sid = sid;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getSex() {
        return sex;
    }

    public void setSex(int sex) {
        this.sex = sex;
    }

    public int getTelephone() {
        return telephone;
    }

    public void setTelephone(int telephone) {
        this.telephone = telephone;
    }

    public String getCard() {
        return card;
    }

    public void setCard(String card) {
        this.card = card;
    }
    @JsonFormat(pattern = "yyyy-MM-dd", timezone = "GMT+8")
    public Date getBirthday() {
        return birthday;
    }

    public void setBirthday(Date birthday) {
        this.birthday = birthday;
    }



    public Classes getClasses() {
        return classes;
    }

    public void setClasses(Classes classes) {
        this.classes = classes;
    }
}

Classes.java

package com.sdbairui.demo.Entity;

import javax.persistence.*;

@Table
@Entity(name="classes")
public class Classes {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private int cid;
    private int cname;
//    @OneToOne
//    @JoinColumn(name="gid",referencedColumnName = "gid",insertable = false,updatable = false)
//    private Grade grade;
//
//    public Grade getGrade() {
//        return grade;
//    }

//    public void setGrade(Grade grade) {
//        this.grade = grade;
//    }
    public int getCid() {
        return cid;
    }

    public void setCid(int cid) {
        this.cid = cid;
    }

    public int getCname() {
        return cname;
    }

    public void setCname(int cname) {
        this.cname = cname;
    }
}

Service层(逻辑层)

下面代码来自:PersonService.java

public List<Person> findAll(){
    return iPerson.findAll();
}

Controller(控制层)

PersonController.java

@RequestMapping("/Index")
    @ResponseBody
    public List<Person> toindex(Model model){
        List<Person> list=personService.findAll();
        for(int i=0;i<list.size();i++){
            System.out.println(list.get(i).getClasses().getCname()+"----->"+list.get(i).getName());
        }
        model.addAttribute("person",list);
        return list;
    }

view层(视图层)

下面代码来自:index.html

 <td th:text="${person.classes.cname}"></td>

person对象与classes对象连接之后,取得classes对象的属性cname

展现浏览器效果

[{“id”:1,“name”:“张三”,“sex”:1,“telephone”:1568079353,“card”:“13254654897987”,“birthday”:“2020-06-02”,“sid”:1,“classes”:{“cid”:1,“cname”:1901}},{“id”:3,“name”:“李四”,“sex”:1,“telephone”:2,“card”:“37132084821548842”,“birthday”:“2020-07-02”,“sid”:1,“classes”:{“cid”:1,“cname”:1901}},{“id”:5,“name”:“王五”,“sex”:1,“telephone”:123456789,“card”:“1234567897”,“birthday”:“2020-05-05”,“sid”:1,“classes”:{“cid”:1,“cname”:1901}},{“id”:8,“name”:“张三15680793539”,“sex”:1,“telephone”:15680,“card”:“1234567897”,“birthday”:“2020-05-28”,“sid”:1,“classes”:{“cid”:1,“cname”:1901}},{“id”:9,“name”:“蒋门神”,“sex”:4,“telephone”:123456789,“card”:“1234567897”,“birthday”:“2013-05-30”,“sid”:4,“classes”:{“cid”:4,“cname”:1904}}]

;