多对一
entity类
班级类
package com.ysd.entity;
import com.fasterxml.jackson.annotation.JsonIgnore;
import lombok.Data;
import javax.persistence.*;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
@Data
@Entity
@Table(name = "clazztb")
public class Clazz implements Serializable {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(columnDefinition = "int unsigned NOT NULL comment'备注:班级自动增长主键'")
private Integer clazzId;
@Column(length = 10, unique = true)
private String clazzName;
@OrderBy
@Column(columnDefinition = "int unsigned DEFAULT 0 comment'备注:班级总人数' ")
private Integer clazzNumber;
private String showClazz() {
return "Clazz[clazzId=" + clazzId + ",clazzName=" + clazzName + ",clazzNumber=" + clazzNumber + "]";
}
}
学生类
package com.ysd.entity;
import com.fasterxml.jackson.annotation.JsonIgnore;
import lombok.Data;
import javax.persistence.*;
import java.io.Serializable;
import java.sql.Timestamp;
import java.util.Date;
@Data
@Entity
@Table(name = "studenttb")
public class Studentz implements Serializable {
@Id //实体类主键
@GeneratedValue(strategy = GenerationType.IDENTITY) //自动增长
@OrderBy //数据加载顺序
@Column(columnDefinition = "int unsigned NOT NULL comment'备注:学生自动增长主键'")
private Integer studentId;
@Column(length = 20) //字符长度20
private String studentName;
@Column(columnDefinition = "char(1) comment'备注:学生性别'")
private String studentSex;
@Column(columnDefinition = "int unsigned DEFAULT 0 comment'备注:学生年龄'")
private Integer studentAge;
private Date studentBirthday;
//@CreationTimestamp@UpdateTimestamp // 插 入 , 修 改 时 自 动 维 护 时 间 戳
@Column(columnDefinition = "TIMESTAMP", nullable = false, updatable = false, insertable = false)
private Timestamp updateTime;
@Transient // 临 时 参 数 , 不 映 射 到 数 据 库 表 字 段
private String studentSpare;
// 对 应 的 班 级
@ManyToOne(targetEntity=Clazz.class,cascade=CascadeType.ALL,fetch=FetchType.EAGER)//,cascade=CascadeType.ALL,fetch=FetchType.EAGER
@JoinColumn(name="class_stu_id",referencedColumnName="clazzId")//,referencedColumnName="clazzId"
private Clazz clazz;
public String showStudentAndClazz(){
return"Student[studentId="+ studentId+",studentName="+studentName+
",studentSex="+studentSex+ ",studentAge="+studentAge+",studentBirthday="
+studentBirthday+ ",updateTime="+updateTime+",studentSpare="+studentSpare+",clazz=" +clazz+"]";
}
// public String showStudent() {
// return "Student[studentId=" + studentId + ",studentName=" + studentName + ",studentSex=" +
// studentSex + ",studentAge=" + studentAge + ",studentBirthday=" + studentBirthday +
// ",updateTime=" + updateTime + "]";
,studentSpare=" + studentSpare + "
// }
}
dao层
service层
serviceimpl层
controller层
结果