feat(user): add user entity and repository with JPA integration

This commit is contained in:
2025-11-29 09:38:08 +08:00
parent 3eb651e039
commit 894a1c5d07
10 changed files with 178 additions and 8 deletions

View File

@@ -1,5 +1,5 @@
plugins {
java
`java-library`
}
dependencies {
@@ -8,6 +8,8 @@ dependencies {
implementation("com.google.guava:guava:33.5.0-jre")
// commons-lang3
implementation("org.apache.commons:commons-lang3:3.20.0")
// jpa
api("org.springframework.boot:spring-boot-starter-data-jpa")
// test
testImplementation("org.springframework.boot:spring-boot-starter-test")
// jackson
@@ -17,4 +19,6 @@ dependencies {
implementation("org.springframework.boot:spring-boot-starter-aop")
// web
implementation("org.springframework.boot:spring-boot-starter-web")
// postgresql
runtimeOnly("org.postgresql:postgresql")
}

View File

@@ -0,0 +1,82 @@
package com.hanserwei.common.domain.dataobject;
import jakarta.persistence.*; // 使用 Jakarta Persistence API (JPA 3.0+)
import lombok.Builder;
import lombok.Getter;
import lombok.Setter;
import org.hibernate.annotations.CreationTimestamp;
import org.hibernate.annotations.UpdateTimestamp;
import java.io.Serial;
import java.io.Serializable;
import java.time.Instant; // 推荐用于 TIMESTAMP WITH TIME ZONE
/**
* 用户表t_user 对应实体)
*/
@Entity
@Setter
@Getter
@Builder
@Table(name = "t_user") // 对应数据库中的表名
public class User implements Serializable {
@Serial
private static final long serialVersionUID = 1L;
/**
* ID (BIG SERIAL)
*/
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id")
private Long id;
/**
* 用户名 (VARCHAR(60) NOT NULL UNIQUE)
*/
@Column(name = "username", length = 60, nullable = false, unique = true)
private String username;
/**
* 密码 (VARCHAR(60) NOT NULL)
*/
@Column(name = "password", length = 60, nullable = false)
private String password;
/**
* 创建时间 (TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT NOW())
* 使用 Hibernate 的 @CreationTimestamp 确保创建时自动赋值
*/
@CreationTimestamp
@Column(name = "create_time", nullable = false, updatable = false)
private Instant createTime;
/**
* 最后一次更新时间 (TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT NOW())
* 使用 Hibernate 的 @UpdateTimestamp 确保更新时自动赋值
* 注意:虽然数据库有触发器,但使用此注解可保持 ORM 层的同步性
*/
@UpdateTimestamp
@Column(name = "update_time", nullable = false)
private Instant updateTime;
/**
* 逻辑删除FALSE未删除 TRUE已删除 (BOOLEAN NOT NULL DEFAULT FALSE)
*/
@Builder.Default
@Column(name = "is_deleted", nullable = false)
private Boolean isDeleted = false; // 对应数据库默认值
public User() {
}
public User(Long id, String username, String password, Instant createTime, Instant updateTime, Boolean isDeleted) {
this.id = id;
this.username = username;
this.password = password;
this.createTime = createTime;
this.updateTime = updateTime;
this.isDeleted = isDeleted;
}
}

View File

@@ -0,0 +1,7 @@
package com.hanserwei.common.domain.repository;
import com.hanserwei.common.domain.dataobject.User;
import org.springframework.data.jpa.repository.JpaRepository;
public interface UserRepository extends JpaRepository<User, Long> {
}