Hibernate學習(一)使用SchemaExport產生資料表

來源:互聯網
上載者:User

Hibernate是一個開放原始碼的對象關係映射架構,它對JDBC進行了非常輕量級的對象封裝,使得Java程式員可以隨心所欲的使用對象編程思維來操縱資料庫。

今天就來示範一下Hibernate最初級的操作,使用SchemaExport建立資料表。

1.首先建立POJO類

package com.bjpowernode.hibernate;        import java.util.Date;        /**  * 使用者  * @author Longxuan  *  */public class User {                private String  id;                private String name;                private String password;                private Date createTime;                private Date expireTime;            public String getId() {          return id;      }            public void setId(String id) {          this.id = id;      }            public String getName() {          return name;      }            public void setName(String name) {          this.name = name;      }            public String getPassword() {          return password;      }   //URL:http://www.bianceng.cn/Programming/Java/201410/45827.htm    public void setPassword(String password) {          this.password = password;      }            public Date getCreateTime() {          return createTime;      }            public void setCreateTime(Date createTime) {          this.createTime = createTime;      }            public Date getExpireTime() {          return expireTime;      }            public void setExpireTime(Date expireTime) {          this.expireTime = expireTime;      }            }

2、根據POJO類裡面裡面相關的欄位,在包中建立User.hbm.xml對應檔

<?xml version="1.0"?>  <!DOCTYPE hibernate-mapping PUBLIC       "-//Hibernate/Hibernate Mapping DTD 3.0//EN"      "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">  <hibernate-mapping>      <class name="com.bjpowernode.hibernate.User" >          <!--hibernate為我們產生主鍵id-->        <id name="id">              <generator class="uuid"  />          </id>                        <!--預設把類的變數映射為相同名字的表列,當然我們使用column屬性修改表欄位-->        <property name="name" column="name"></property>          <property name="password"></property>          <property name="createTime"></property>          <property name="expireTime"></property>      </class>  </hibernate-mapping>

3、在src中建立hibernate.cfg.xml

<!DOCTYPE hibernate-configuration PUBLIC      "-//Hibernate/Hibernate Configuration DTD 3.0//EN"      "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">        <hibernate-configuration>      <session-factory name="foo">          <!-- 資料庫的串連也可以直接使用hibernate.properties檔案 -->        <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>          <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/hibernate_test</property>          <property name="hibernate.connection.username">root</property>          <property name="hibernate.connection.password">root</property>                        <property name="hibernate.dialect" >org.hibernate.dialect.MySQLDialect</property><!-- 指定sql方言 -->        <property name="hibernate.show_sql">true</property><!-- 設定是否顯示產生sql語句 -->        <property name="hibernate.format_sql">true</property><!-- 設定是否格式化sql語句-->                      <mapping resource="com/bjpowernode/hibernate/User.hbm.xml"   />      </session-factory>  </hibernate-configuration>

聯繫我們

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