Week 1-8: Spring Boot 学习计划完整项目
Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
This commit is contained in:
107
week3/src/main/java/com/learn/entity/Student.java
Normal file
107
week3/src/main/java/com/learn/entity/Student.java
Normal file
@@ -0,0 +1,107 @@
|
||||
package com.learn.entity;
|
||||
|
||||
import com.baomidou.mybatisplus.annotation.IdType;
|
||||
import com.baomidou.mybatisplus.annotation.TableField;
|
||||
import com.baomidou.mybatisplus.annotation.TableId;
|
||||
import com.baomidou.mybatisplus.annotation.TableName;
|
||||
import jakarta.persistence.*;
|
||||
import jakarta.validation.constraints.*;
|
||||
import java.time.LocalDateTime;
|
||||
|
||||
/**
|
||||
* 学生实体 —— 同时兼容 JPA 和 MyBatis-Plus
|
||||
*
|
||||
* 对比观察:
|
||||
* @Entity (JPA) ↔ @TableName (MP) —— 声明这是一个表映射类
|
||||
* @Table (JPA) ↔ @TableName (MP) —— 指定对应的表名
|
||||
* @Id (JPA) ↔ @TableId (MP) —— 主键
|
||||
* @GeneratedValue (JPA) ↔ @TableId(type=...) (MP) —— 主键生成策略
|
||||
* @Column (JPA) ↔ @TableField (MP) —— 字段映射
|
||||
*
|
||||
* 注意:JPA 和 MP 的注解可以共存于同一个实体类上,互不冲突。
|
||||
* 因为它们分属不同的框架,各自读取各自的注解。
|
||||
*/
|
||||
@Entity
|
||||
@Table(name = "student")
|
||||
@TableName("student") // MP 注解:指定表名
|
||||
public class Student {
|
||||
|
||||
@Id
|
||||
@GeneratedValue(strategy = GenerationType.IDENTITY) // JPA:数据库自增
|
||||
@TableId(type = IdType.AUTO) // MP:数据库自增
|
||||
private Long id;
|
||||
|
||||
@NotBlank(message = "姓名不能为空")
|
||||
@Size(min = 1, max = 20, message = "姓名长度 1-20")
|
||||
@Column(name = "name", length = 20, nullable = false)
|
||||
@TableField("name") // MP 注解(当字段名=列名时可省略,此处显式写出便于学习)
|
||||
private String name;
|
||||
|
||||
@Min(value = 1, message = "年龄必须大于 0")
|
||||
@Max(value = 150, message = "年龄必须小于 150")
|
||||
@Column(name = "age", nullable = false)
|
||||
@TableField("age")
|
||||
private int age;
|
||||
|
||||
@NotBlank(message = "邮箱不能为空")
|
||||
@Email(message = "邮箱格式不正确")
|
||||
@Column(name = "email", length = 50, nullable = false)
|
||||
@TableField("email")
|
||||
private String email;
|
||||
|
||||
@Min(value = 0, message = "成绩不能为负数")
|
||||
@Max(value = 100, message = "成绩不能超过 100")
|
||||
@Column(name = "score", nullable = false)
|
||||
@TableField("score")
|
||||
private int score;
|
||||
|
||||
// ---- 审计字段(由数据库自动管理)----
|
||||
@Column(name = "create_time", insertable = false, updatable = false)
|
||||
@TableField("create_time")
|
||||
private LocalDateTime createTime;
|
||||
|
||||
@Column(name = "update_time", insertable = false, updatable = false)
|
||||
@TableField("update_time")
|
||||
private LocalDateTime updateTime;
|
||||
|
||||
// ==================== 构造方法 ====================
|
||||
|
||||
public Student() {}
|
||||
|
||||
public Student(Long id, String name, int age, String email, int score) {
|
||||
this.id = id;
|
||||
this.name = name;
|
||||
this.age = age;
|
||||
this.email = email;
|
||||
this.score = score;
|
||||
}
|
||||
|
||||
// ==================== Getter / Setter ====================
|
||||
|
||||
public Long getId() { return id; }
|
||||
public void setId(Long id) { this.id = id; }
|
||||
|
||||
public String getName() { return name; }
|
||||
public void setName(String name) { this.name = name; }
|
||||
|
||||
public int getAge() { return age; }
|
||||
public void setAge(int age) { this.age = age; }
|
||||
|
||||
public String getEmail() { return email; }
|
||||
public void setEmail(String email) { this.email = email; }
|
||||
|
||||
public int getScore() { return score; }
|
||||
public void setScore(int score) { this.score = score; }
|
||||
|
||||
public LocalDateTime getCreateTime() { return createTime; }
|
||||
public void setCreateTime(LocalDateTime createTime) { this.createTime = createTime; }
|
||||
|
||||
public LocalDateTime getUpdateTime() { return updateTime; }
|
||||
public void setUpdateTime(LocalDateTime updateTime) { this.updateTime = updateTime; }
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return String.format("Student{id=%d, name='%s', age=%d, email='%s', score=%d}",
|
||||
id, name, age, email, score);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user