文章背景图

通过idea逆向生成实体

2025-04-23
2
-
- 分钟
  1. 打开 JPA 面板
    ViewTool WindowsPersistence(如果没有,先确保项目已启用 JPA)

  2. 生成实体

    • 右键项目名 → Generate Persistence MappingBy Database Schema

    • 选择已配置的数据库连接 → 勾选需要生成实体的表(如 EMPLOYEE

    • 配置生成选项:

      • Package‌:指定实体类存放的包(如 com.example.entity

      • Options‌:勾选 Generate JPA annotationsGenerate column name annotations

      • Customize mappings‌:可手动调整字段类型(如将 NUMBER 映射为 Long

  3. 完成生成
    生成的实体类会自动出现在指定包下,示例:

    @Entity 
    @Table(name = "EMPLOYEE") 
    public class Employee { 
    @Id 
    @Column(name = "EMP_ID", precision = 10) 
    private Long empId;
    @Column(name = "EMP_NAME", length = 50) 
    private String empName; 
    // 自动处理外键关联 
    @ManyToOne 
    @JoinColumn(name = "DEPT_ID") 
    private Department department; 
    }

评论交流

文章目录