先看下jo_user這張表。
jeebbs採用hibernate作為持久層架構,我們設計一個持久對象PO來映射這張表。
PO=POJO+映射設定檔 檔案目錄如下圖所示。
其中UnifiedUser繼承的是BaseUnifiedUser這個抽象類別,所以UnifiedUser這個類就是POJO。下面看下對應檔,指定了pojo裡面屬性和jo_user中欄位的一一映射關係。
<?xml version="1.0"?><!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"><hibernate-mapping package="com.jeecms.core.entity"><class name="UnifiedUser" table="jo_user"><meta attribute="sync-DAO">false</meta><id name="id" type="java.lang.Integer" column="user_id"><generator class="identity"/></id><property name="username" column="username" type="string" not-null="true" length="100"/><property name="email" column="email" type="string" not-null="false" length="100"/><property name="password" column="password" type="string" not-null="true" length="32"/><property name="registerTime" column="register_time" type="timestamp" not-null="true" length="19"/><property name="registerIp" column="register_ip" type="string" not-null="true" length="50"/><property name="lastLoginTime" column="last_login_time" type="timestamp" not-null="false" length="19"/><property name="lastLoginIp" column="last_login_ip" type="string" not-null="false" length="50"/><property name="loginCount" column="login_count" type="integer" not-null="true" length="10"/><property name="resetKey" column="reset_key" type="string" not-null="false" length="32"/><property name="resetPwd" column="reset_pwd" type="string" not-null="false" length="10"/><property name="activation" type="java.lang.Boolean" not-null="true"/><property name="activationCode" column="activation_code" type="string" not-null="false" length="32"/></class></hibernate-mapping>
到此,jo_user這張表已經映射到UnifiedUser這個類裡面了。後續編程的時候只需訪問這個類的即可,不需要直接存取資料庫中的表了。資料庫中其他的表也都是用以上這種方法來映射到對應的類中。
Spring提倡面向介面編程,耦合也只是介面的耦合,所以DAO的部分也是一個介面,一個實作類別。
未完待續~