JDK註解替代Hibernate的Entity映射

來源:互聯網
上載者:User

標籤:nat   代碼   integer   demo   his   替換   模組   string   欄位   

1.在entity(實體類)模組中使用註解

 

1_1.註解的位置出現在 [類定義的前面] 和 [屬性的get方法前面]

Java代碼:

package app.entity;//模型//注意看,匯入的是jdk的拓展包import javax.persistence.*;@Entity //告訴Spring這個類是實體類、模型類@Table(name="t_demo") //聲明這個模型對於其哪張資料表public class Demo {        private Integer cid; //編號欄位,對應是資料庫資料表中的主鍵    private String name; //姓名欄位,對應資料庫資料表中的name欄位    @Id    @GeneratedValue(strategy = GenerationType.AUTO)    public Integer getCid() {        return cid;    }    public void setCid(Integer cid) {        this.cid = cid;    }        @Column(name="name")    public String getName() {        return name;    }    public void setName(String name) {        this.name = name;    }    }

 

 

1_2.註解的位置也可以出現在 [屬性前面] 和 [類定義的前面]

Java代碼:

package app.entity;//模型import javax.persistence.*;@Entity //告訴Spring這個類是實體類、模型類@Table(name="t_demo") //聲明這個模型對於其哪張資料表public class Demo {        @Id    @GeneratedValue(strategy = GenerationType.AUTO)    private Integer cid; //編號欄位,對應是資料庫資料表中的主鍵        @Column(name="name")    private String name; //姓名欄位,對應資料庫資料表中的name欄位    public Integer getCid() {        return cid;    }    public void setCid(Integer cid) {        this.cid = cid;    }        public String getName() {        return name;    }    public void setName(String name) {        this.name = name;    }    }

 

 

 2.把Spring的applicationContext.xml設定檔中的<property name="mappingResources">換掉

 Xml代碼:

    <bean>            <!-- <property name="mappingResources">            <list>                <value>app/entity/Bc.hbm.xml</value>            </list>        </property> -->        <!--替換成下面的配置-->        <property name="packagesToScan">            <list>                <!-- value標籤中的值是實體類所在的包 -->                <value>app.entity</value>            </list>        </property>    </bean>

 

 

 

實現了不用Hibernate的Entity對應檔,使用Jdk提供的註解來完成

----------------------------------------------------------------------------

(over)

 

JDK註解替代Hibernate的Entity映射

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.