Hibernate-chapter two,hibernate-chapter

來源:互聯網
上載者:User

Hibernate-chapter two,hibernate-chapter

又見面啦!!!經過上一章的學習,我們大概對Hibernate架構有所瞭解。接下來進入我們的第二階段——實戰!

======準備資料庫======

這裡使用MySQL作為示範。

如:

資料庫的操作暫時就到這裡。

Ps:沒有資料庫基礎的朋友們,請先學習資料庫基礎再來學習哦!

======準備Hibernate的jar包======

 

至於你下載什麼版本自己決定,這裡先用4.2.4作為示範。

 

*其中有資料庫驅動包<mysql-connector-........>,你使用什麼資料庫就下載什麼資料庫驅動包,這裡是MySQL。

======建立項目======

 

======接下來就是編碼階段======

 

步驟寫得清清楚楚,我不做解釋了,接著動手敲吧。

*******************************************************

附圖:這是代碼結構圖

 

1——建立Hibernate設定檔

 1 <?xml version="1.0" encoding="UTF-8"?> 2 <!DOCTYPE hibernate-configuration PUBLIC 3         "-//Hibernate/Hibernate Configuration DTD 3.0//EN" 4         "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"> 5 <hibernate-configuration> 6     <session-factory> 7      8         <!-- 配置串連資料庫的基本資料 --> 9         <property name="connection.username">root</property>10         <property name="connection.password">123456</property>11         <property name="connection.driver_class">com.mysql.jdbc.Driver</property>12         <property name="connection.url">jdbc:mysql://localhost:3306/hibernateTest</property>13         14         <!-- 配置 hibernate 的基本資料 -->15         16         <!-- hibernate 所使用的資料庫方言     -->    17         <property name="dialect">org.hibernate.dialect.MySQLInnoDBDialect</property>18         19         <!-- 執行操作時是否在控制台列印 SQL -->20         <property name="show_sql">true</property>21     22         <!-- 是否對 SQL 進行格式化 -->23         <property name="format_sql">true</property>24     25         <!-- 指定自動產生資料表的策略 -->26         <property name="hibernate.hbm2ddl.auto">update</property>27         28         29         <!-- 指定關聯的 .hbm.xml 檔案 -->30         <mapping resource="com/jbit/entity/User/User.hbm.xml"/>31         32     </session-factory>33 34 </hibernate-configuration>

2——建立持久化類

 1 package com.jbit.entity.User; 2 public class User { 3  4     private int id; 5     private String name; 6     private int age; 7      8     public int getId() { 9         return id;10     }11     public void setId(int id) {12         this.id = id;13     }14     public String getName() {15         return name;16     }17     public void setName(String name) {18         this.name = name;19     }20     public int getAge() {21         return age;22     }23     public void setAge(int age) {24         this.age = age;25     }26     public User() {27     28     }29     public User(String name, int age) {30         super();31         this.name = name;32         this.age = age;33     }34     @Override35     public String toString() {36         return "User [id=" + id + ", name=" + name + ", age=" + age + "]";37     }38     39     40     41     42 }

3——建立關係對應檔

 1 <?xml version="1.0"?> 2 <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" 3 "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> 4  5 <hibernate-mapping> 6  7     <class name="com.jbit.entity.User.User" table="USER"> 8          9         <id name="id" type="java.lang.Integer" >10             <column name="ID" />11             <!-- 指定主鍵的產生方式, native: 使用資料庫本地方式 -->12             <generator class="native" />13         </id>14     15         <property name="name" type="java.lang.String">16             <column name="NAME"/>17         </property>18         19         <property name="age" type="java.lang.Integer">20             <column name="AGE" />21         </property>22     </class>23     24 </hibernate-mapping>

4——建立通過Hibernate APL訪問資料庫代碼

 1 package com.jbit.entity.User; 2  3 import org.hibernate.Session; 4 import org.hibernate.SessionFactory; 5 import org.hibernate.Transaction; 6 import org.hibernate.cfg.Configuration; 7 import org.hibernate.service.ServiceRegistry; 8 import org.hibernate.service.ServiceRegistryBuilder; 9 import org.junit.Test;10 11 public class HibernateTest {12     @Test13     public void test() {14         15         System.out.println("test...啟動。。。。。。");16         17         //1. 建立一個 SessionFactory 對象18         19         SessionFactory sessionFactory = null;20         21         //1). 建立 Configuration 對象: 對應 hibernate 的基本配置資訊和 對象關係映射資訊22         Configuration configuration = new Configuration().configure();23         24         //4.0 之前這樣建立25 //        sessionFactory = configuration.buildSessionFactory();26         27         //2). 建立一個 ServiceRegistry 對象: hibernate 4.x 新添加的對象28         //hibernate 的任何配置和服務都需要在該對象中註冊後才能有效.29         ServiceRegistry serviceRegistry = 30                 new ServiceRegistryBuilder().applySettings(configuration.getProperties())31                                             .buildServiceRegistry();32         33         //3).34         sessionFactory = configuration.buildSessionFactory(serviceRegistry);35         36         //2. 建立一個 Session 對象37         Session session = sessionFactory.openSession();38         39         //3. 開啟事務40         Transaction transaction = session.beginTransaction();41         42         //4. 執行儲存操作43         User users = new User("Java12345",10);44         45         session.save(users);46         47         48         //5. 提交事務 49         transaction.commit();50         51         //6. 關閉 Session52         session.close();53         54         //7. 關閉 SessionFactory 對象55         sessionFactory.close();56     }57 }

嗯-------------上面的敲完,理解後,接下來就是執行,看資料庫是否自動為資料庫添加表和列!

看我的成功了!你呢???

 

 --------------------------------------------------------------------------------------------------------------------------------------來自尼瑪星球的光軍/QQ:383996901

聯繫我們

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