首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 編程語言 >  >> Java 程式設計 >> Java 相關知識

EasyDBO Java 註解映射配置原理與實戰指南

     編輯:Java 相關知識

註解映射核心註解解析

EasyDBO 測試版引入註解配置 ORM 映射,核心為 @Table 與 @TableField 兩個運行期保留註解。

@Table 註解定義

@Retention(RetentionPolicy.RUNTIME)
public @interface Table {
    String tableName();                    // 資料表名稱
    String keyField() default "cid";       // 主鍵欄位,預設 "cid"
    String keyGenerator() default "com.easyjf.dbo.RandomIdGenerator"; // 主鍵生成器
}

@TableField 註解定義

@Retention(RetentionPolicy.RUNTIME)
public @interface TableField {
    String name();                         // 資料庫欄位名
    boolean lazy() default false;          // 延遲載入
    boolean unique() default false;        // 唯一性約束
}

實戰配置範例

基本映射(使用預設值)

@Table(tableName = "message")
public class Message implements Serializable {
    @TableField(name = "cid") private String cid;
    @TableField(name = "title") private String title1;
    @TableField(name = "content") private String content1;
    @TableField(name = "inputUser") private String inputUser;
    @TableField(name = "inputTime") private Date inputTime;
    @TableField(name = "publish") private Boolean publish1;
    @TableField(name = "status") private Integer status1;
    // getter/setter...
}

預設主鍵為 cid,生成器為 RandomIdGenerator。

自訂主鍵與生成器

@Table(
    tableName = "message",
    keyField = "id",
    keyGenerator = "com.easyjf.dbo.NullIdGenerator"
)
public class Message implements Serializable {
    @TableField(name = "id") private String id;
    // 其他欄位...
}

進階關聯映射

除基本欄位,尚支援關聯關係註解:

  • @OneToOne:一對一關聯
  • @ManyToOne:多對一關聯
  • @ManyToMany:多對多關聯

詳細用法請參考官方開發指南:EasyDBO Development Guide

版本演進提醒

註解配置功能於正式版仍將持續調整擴充,建議持續關注最新版本釋出資訊與遷移指南。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved