2025-12-15 13:14:18 +08:00
|
|
|
|
package com.vibevault.model;
|
|
|
|
|
|
|
|
|
|
|
|
import jakarta.persistence.*;
|
|
|
|
|
|
|
2025-12-22 04:10:30 +08:00
|
|
|
|
|
2025-12-15 13:14:18 +08:00
|
|
|
|
/**
|
|
|
|
|
|
* 用户实体类
|
2025-12-22 04:10:30 +08:00
|
|
|
|
* 需要实现:
|
2025-12-15 13:14:18 +08:00
|
|
|
|
* - 将此类映射为数据库表 "users"
|
|
|
|
|
|
* - id 作为自增主键
|
|
|
|
|
|
* - username 必须唯一且不能为空
|
|
|
|
|
|
* - password 不能为空
|
|
|
|
|
|
* - [Challenge] 支持用户角色(如 ROLE_USER, ROLE_ADMIN)
|
|
|
|
|
|
*/
|
2025-12-22 04:10:30 +08:00
|
|
|
|
@Entity // 实现:将此类映射为数据库表
|
|
|
|
|
|
@Table(name = "users") // 实现:指定表名为"users"
|
2025-12-15 13:14:18 +08:00
|
|
|
|
public class User {
|
|
|
|
|
|
|
2025-12-22 04:10:30 +08:00
|
|
|
|
@Id // 实现:标记id为主键
|
|
|
|
|
|
@GeneratedValue(strategy = GenerationType.IDENTITY) // 实现:id作为自增主键
|
2025-12-15 13:14:18 +08:00
|
|
|
|
private Long id;
|
|
|
|
|
|
|
2025-12-22 04:10:30 +08:00
|
|
|
|
@Column(nullable = false, unique = true) // 实现:username必须唯一且不能为空
|
2025-12-15 13:14:18 +08:00
|
|
|
|
private String username;
|
|
|
|
|
|
|
2025-12-22 04:10:30 +08:00
|
|
|
|
@Column(nullable = false) // 实现:password不能为空
|
2025-12-15 13:14:18 +08:00
|
|
|
|
private String password;
|
|
|
|
|
|
|
2025-12-22 04:10:30 +08:00
|
|
|
|
// [Challenge] 支持用户角色(如 ROLE_USER, ROLE_ADMIN)
|
|
|
|
|
|
@Column(nullable = false) // 角色字段非空
|
|
|
|
|
|
private String role = "ROLE_USER"; // 默认角色为ROLE_USER
|
2025-12-15 13:14:18 +08:00
|
|
|
|
|
2025-12-22 04:10:30 +08:00
|
|
|
|
// 无参构造(JPA必须)
|
|
|
|
|
|
public User() {
|
2025-12-15 13:14:18 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2025-12-22 04:10:30 +08:00
|
|
|
|
// 2参数构造(用户名+密码)
|
2025-12-15 13:14:18 +08:00
|
|
|
|
public User(String username, String password) {
|
|
|
|
|
|
this.username = username;
|
|
|
|
|
|
this.password = password;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-12-22 04:10:30 +08:00
|
|
|
|
// 3参数构造(适配测试代码:用户名+密码+角色)
|
2025-12-15 13:14:18 +08:00
|
|
|
|
public User(String username, String password, String role) {
|
|
|
|
|
|
this.username = username;
|
|
|
|
|
|
this.password = password;
|
|
|
|
|
|
this.role = role;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-12-22 04:10:30 +08:00
|
|
|
|
// 必须的getter/setter方法(JPA操作实体需要)
|
2025-12-15 13:14:18 +08:00
|
|
|
|
public Long getId() {
|
|
|
|
|
|
return id;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-12-22 04:10:30 +08:00
|
|
|
|
public void setId(Long id) {
|
|
|
|
|
|
this.id = id;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-12-15 13:14:18 +08:00
|
|
|
|
public String getUsername() {
|
|
|
|
|
|
return username;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-12-22 04:10:30 +08:00
|
|
|
|
public void setUsername(String username) {
|
|
|
|
|
|
this.username = username;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-12-15 13:14:18 +08:00
|
|
|
|
public String getPassword() {
|
|
|
|
|
|
return password;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-12-22 04:10:30 +08:00
|
|
|
|
public void setPassword(String password) {
|
|
|
|
|
|
this.password = password;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-12-15 13:14:18 +08:00
|
|
|
|
public String getRole() {
|
|
|
|
|
|
return role;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public void setRole(String role) {
|
|
|
|
|
|
this.role = role;
|
|
|
|
|
|
}
|
2025-12-22 04:10:30 +08:00
|
|
|
|
}
|