hibernate用註解的方式實現orm

來源:互聯網
上載者:User

標籤:connect   set   ring   show   ping   log   conf   xml檔案   預設   

hibernate 有兩種方式實現把一張表映射成一個對象,一種是設定檔的方式,一種是註解的方式。這裡用hibernate提供的註解的方式實現一個對象和一張表之間的對應。

思路:

首先在hibernate.cfg.xml檔案中配置如下內容:資料庫,方言,是否顯示sql,載入映射類:注意這個hibernate.cfg.xml位置在src下。

因為new Configuration().configure()。這個configure()函數開啟源碼預設的hiberante.cfg.xml就在src下。

hibernate.cfg.xml的配置的代碼如下所示:

 1 <?xml version=‘1.0‘ encoding=‘utf-8‘?> 2 <!-- 3   ~ Hibernate, Relational Persistence for Idiomatic Java 4   ~ 5   ~ License: GNU Lesser General Public License (LGPL), version 2.1 or later. 6   ~ See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>. 7   --> 8 <!DOCTYPE hibernate-configuration PUBLIC 9         "-//Hibernate/Hibernate Configuration DTD 3.0//EN"10         "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">11 12 <hibernate-configuration>13 14     <session-factory>15 16         <!-- Database connection settings -->17         <property name="connection.driver_class">oracle.jdbc.OracleDriver</property>18         <property name="connection.url">jdbc:oracle:thin:@localhost:1521:orcl</property>19         <property name="connection.username">scott</property>20         <property name="connection.password">a123456</property>21 22         <!-- JDBC connection pool (use the built-in) -->23         <property name="connection.pool_size">10</property>24 25         <!-- SQL dialect -->26         <property name="dialect">org.hibernate.dialect.Oracle10gDialect</property>27         <!-- Echo all executed SQL to stdout -->28         <property name="show_sql">true</property>29 30         <!-- Drop and re-create the database schema on startup -->31      <!--   <property name="hbm2ddl.auto">create</property>-->32 33         <!-- Names the annotated entity class -->34              <mapping class="com.qls.domain.DiaoChan"/>35     </session-factory>36 37 </hibernate-configuration>

然後再oracle資料庫中建立diaoChan這張表:

sql語句如下:

1 create table diaoChan(2  id number(2) primary key not null,3  name varchar2(30) ,4    height number(2)5   );

然後再oracle資料庫建立一個序列,用於主鍵建置原則用的。

sql語句如下:

1 create sequence sixi start with 1 increment by 1;

 寫一個hibernate的domain對象:

代碼如下:

package com.qls.domain;import javax.persistence.*;/** * Created by 秦林森 on 2017/5/20. */@Entity@Table(name = "diaoChan")public class DiaoChan {    private int id;    private String name;    private int height;    @Id   @GeneratedValue(strategy = GenerationType.SEQUENCE,generator = "people")//其中上行的generator中的內容必須和下面這行的name表示一致。   @SequenceGenerator(name = "people",sequenceName = "sixi",initialValue = 1,allocationSize = 1)    public int getId() {        return id;    }    public void setId(int id) {        this.id = id;    }    @Column(name ="name")    public String getName() {        return name;    }    public void setName(String name) {        this.name = name;    }    @Column(name = "height")    public int getHeight() {        return height;    }    public void setHeight(int height) {        this.height = height;    }}

然後寫一個Test類主要是向表中插入一條資料:

代碼如下:

 1 package com.qls.test; 2  3 import com.qls.domain.DiaoChan; 4 import org.hibernate.Session; 5 import org.hibernate.SessionFactory; 6 import org.hibernate.Transaction; 7 import org.hibernate.cfg.Configuration; 8  9 /**10  * Created by 秦林森 on 2017/5/20.11  */12 public class Test2 {13     public static void main(String[] args) {14         SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory();15         Session session = sessionFactory.openSession();16         DiaoChan diaoChan = new DiaoChan();17         diaoChan.setName("崇禎");18         diaoChan.setHeight(10);19         Transaction tx = session.beginTransaction();//開啟事務。20         session.save(diaoChan);21         tx.commit();//提交事務。22         session.close();//關閉會話。23     }24 }

 

運行Test2之後就會在diaoChan這張表中插入一條資料:

查詢結果如下所示:

1 SQL> select *from diaochan;2  3  ID NAME                           HEIGHT4 --- ------------------------------ ------5   3 貂蟬                               126   4 崇禎                               10

上面的結果是因為我運行了兩次的原因。

 

hibernate用註解的方式實現orm

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.