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

Java EE 面向對象設計之道:領域模型驅動的分層架構

     編輯:Java 相關知識

許多開發者抱怨 Java EE 配置繁瑣,實則源於缺乏 OO 思維。表現層與持久層的本質是映射配置,而非業務邏輯。唯有以領域模型為核心,才能構建清晰、可維護的系統。

分層職責與映射本質

層核心任務映射方向
表現層離散表單欄位 ↔ 領域模型Struts ActionForm/標籤庫封裝
業務層純領域模型運算(無技術細節)核心價值所在
持久層領域模型 ↔ 關係表Hibernate/EJB CMP O-R Mapping

表現層與持久層如同「打包與拆封」:將離散數據封裝為對象傳遞給業務層,業務層處理完畢再拆解為關係數據持久化。業務層始終面向對象,而非面向字段。

映射配置的簡要之道

原則:緊扣需求,領域模型不妥協。映射配置是「紅娘」,雙方是領域模型與技術實體(表單/資料表)。若為配置方便而修改模型,必導致偏離需求。

  • 持久層:以領域模型為主導設計映射,資料表結構由 Hibernate 自動生成(hbm2ddl.auto=update)
  • 表現層:若模型無法支撐界面需求,必須回頭重構模型,而非在 Action 中硬編碼彌補

領域建模實戰:聚合與組合

訂單聚合根

Order (聚合根)
├── Customer (聚合關聯,多對一)
└── List<OrderLine> (組合關聯,一對多,強生命週期綁定)
    └── Product (關聯)
    └── quantity

組合意味著部分不能獨立於整體存在(OrderLine 無 Order 無意義),映射時用 cascade="all-delete-orphan"。

Hibernate 聚合映射關鍵細節

父子關係主鍵策略

子對象主鍵必須由 Hibernate 自動生成(如 uuid.hex),絕不可用 assigned。否則 session.update(parent) 無法正確判斷子對象是新增還是修改,導致 SQL 錯誤或資料不一致。

<class name="UserProperty" table="userprops">
  <id name="propId" type="string">
    <generator class="uuid.hex"/> <!-- 關鍵 -->
  </id>
  <many-to-one name="user" column="userId" not-null="true"/> <!-- 雙向關聯提升效能 -->
</class>

<class name="User" table="testuser">
  <bag name="userProps" inverse="true" cascade="all-delete-orphan"> <!-- inverse="true" 由關係維護端控制 -->
    <key column="userId"/>
    <one-to-many class="UserProperty"/>
  </bag>
</class>

Hibernate 2 vs 3 差異

  • H2 需在 <id> 加 unsaved-value="null",H3 不需要
  • H2 lazy 預設 false,H3 預設 true → 需 Hibernate.initialize() 或 Open Session in View(違背分層,易洩漏連線)
  • 實踐建議:讀取聚合時用 JDBC 手寫 SQL 組裝完整對象圖,寫入用 Hibernate 統一模板,兼顧效能與封裝

Struts 表現層聚合綁定

使用索引屬性綁定集合:

<html:form action="/userSaveAction.do">
  <html:select property="dept.deptId"> <!-- 多對一 -->
    <html:optionsCollection name="deptListForm" property="list" value="deptId" label="name"/>
  </html:select>
  <html:text property="userId"/>
  <html:text property="username"/>
  <table>
    <tr><td><html:hidden property="userProp[0].propId"/></td>
        <td><html:text property="userProp[0].name"/></td>
        <td><html:text property="userProp[0].value"/></td></tr>
    <tr><td><html:hidden property="userProp[1].propId"/></td> ... </tr>
  </table>
</html:form>

Form 中提供索引存取器:

public UserProperty getUserProp(int index) {
    return (UserProperty) ((List) userProps).get(index);
}

總結

真正的 Java EE 面向對象系統 = 領域建模(DDD)+ 框架配置映射。以 OO 思維提煉領域模型,再以 OO 思維配置表現/持久層映射,才是 Java 專案快速、高質量交付的正道。

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