深入淺出--Hibernate

來源:互聯網
上載者:User

標籤:style   blog   class   c   code   java   

   話說從大二開始接觸SSH三大架構,但當時能力實在是有限,沒有繼續研究下去。現在趁著對Java的癡迷勁,立馬舊事重提,重新學習SSH。咱們先從SSH中的Hibernate說起。

   也許你會問,為什麼要發明Hibernate呢?其實什麼新生事物的出現都是有原因的,如果沒有Hibernate的ORM思想,java程式員必須精通資料庫語言,只有這樣才能完成項目開發。Hibernate創始人Gavin King突破了這種約束,讓對象和實體之間建立映射,我們通過操作java中的對象就能完成對資料庫的操作。聽起來很神秘吧,讓我用一個小Demo來向大家詳細講解。

   要搭建Hibernate工作的環境,我們要引入相關的jar包,由於我們是初學者,所以建議把jar包都引入,jar包:http://pan.baidu.com/s/1pJi9XOn。是Hibernate執行個體的檔案目錄。

   

    我們要做的是建立實體User.java,使其和資料表T_User建立映射,通過操作User.java完成對資料表T_User的操作。User.java的源碼如下:

package com.entity;    import java.util.Date;    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;      }       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;      }  }
   User.hbm.xml檔案是對應檔,通過它建立User.java和T_User表的映射關係,代碼如下:
<?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.entity.User" table="T_User">          <id name="id">              <generator class="uuid"/>          </id>          <property name="name"/>          <property name="password"/>          <property name="createTime"/>          <property name="expireTime"/>      </class>  </hibernate-mapping>  
      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>   <!-- 串連SQL Server的方言-->   <property name="dialect">org.hibernate.dialect.SQLServerDialect</property>   <!-- 串連SQL Server的資料庫名-->       <property name="connection.url">jdbc:sqlserver://127.0.0.1:1433;database=test</property>       <!-- 串連SQL Server的使用者名稱-->       <property name="connection.username">sa</property>       <!-- 串連SQL Server的密碼-->       <property name="connection.password">123456</property>       <!-- 串連SQL Server的驅動程式-->       <property name="connection.driver_class">com.microsoft.sqlserver.jdbc.SQLServerDriver</property>       <!-- 配置對應檔的路徑-->       <mapping resource="com/entity/User.hbm.xml" /></session-factory></hibernate-configuration> 
    Client.java控制User.java,使其向T_User表寫入資料,代碼如下:
package test;import java.util.Date;import org.hibernate.Session;import org.hibernate.SessionFactory;import org.hibernate.cfg.Configuration;import com.entity.User;public class Client {public static void main(String[] args) {// 讀取hibernate.cfg.xml檔案Configuration cfg = new Configuration().configure();// 建立SessionFactorySessionFactory factory = cfg.buildSessionFactory();// 取得sessionSession session = null;try {session = factory.openSession();// 開啟事務session.beginTransaction();User user = new User();user.setName("NAME1");user.setPassword("PWD1");user.setCreateTime(new Date());user.setExpireTime(new Date());// 儲存User對象session.save(user);// 提交事務session.getTransaction().commit();} catch (Exception e) {e.printStackTrace();// 復原事務session.getTransaction().rollback();} finally {if (session != null) {if (session.isOpen()) {// 關閉sessionsession.close();}}}}}
   T_User表的資料結構如下:

    

    查看T_User表,觀察操作是否成功,如下:

   

   源碼如下:http://pan.baidu.com/s/1nt4sIVZ

   經過兩天的刻苦專研,終於完成了Hibernate的第一個Demo,收穫頗多。不過這隻是個開始,Hibernate的神奇之處還多的很,我會繼續《深入淺出--Hibernate》的部落格,和大家一同進步。

聯繫我們

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