【SSH三大架構】Hibernate基礎第一篇:編寫第一個Hibernate程式

來源:互聯網
上載者:User

標籤:blog   http   io   ar   os   java   for   檔案   資料   

接下來這幾章節學習的是Hibernate,Hibernate的主要作用就是用來和資料庫進行串連,簡化了JDBC的操作。


首先,我們建立項目,然後把Hibernate的jar包和sqlserver的驅動匯入進去。

接下來,我們需要寫一個實體類:User

package cn.itcast.hibernate.domain;import java.util.Date;public class User { private int id; //private String name; private String name; private Date  birthday; public int getId() {return id;}public void setId(int id) {this.id = id;}public String getName() {return name;}public void setName(String name) {this.name = name;}public Date getBirthday() {return birthday;}public void setBirthday(Date birthday) {this.birthday = birthday;}}
提供了三個屬性:id、name、birthday


然後,我們在User所在的包下,配置User實體類的對應檔: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 package="cn.itcast.hibernate.domain"><class name="User" table="uuser"><id name="id"><generator class="native"/> </id><property name="name" /><property name="birthday" /></class></hibernate-mapping>
可以看到,我們配置了一個<class>,name屬性是這個類的雷鳴,table是要映射的資料庫的表

<id><property><property>這三個標籤是定義了表中的屬性列,其中為id設定了<generator>標籤,規定了主鍵產生的策略(後邊會介紹)


接下來,我們就要寫設定檔了,在src目錄下建立:Hibernate.cfg.xml

<?xml version='1.0' encoding='utf-8'?> <!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"><hibernate-configuration><session-factory><property name="connection.driver_class">com.microsoft.sqlserver.jdbc.SQLServerDriver</property><property name="connection.url">jdbc:sqlserver://localhost:1433;DatabaseName=testhibernate</property><property name="connection.username">sa</property><property name="connection.password">123456</property><property name="dialect">org.hibernate.dialect.SQLServerDialect</property><property name="connection.password">123456</property><property name="hbm2ddl.auto">create</property><property name="show_sql">true</property><mapping resource="cn/itcast/hibernate/domain/User.hbm.xml" /></session-factory></hibernate-configuration>
在這上邊配置了資料庫驅動、資料庫地址及資料庫、使用者名稱、密碼、sqlserver資料庫方言,還有建表方式、是否顯示sql語句,最後一個<mapping>是把user.hbm.xml這個對應檔引入進來。


然後,我們寫一個測試類別實現一下:

package cn.itcast.hibernate;import java.util.Date;import org.hibernate.Session;import org.hibernate.SessionFactory;import org.hibernate.Transaction;import org.hibernate.cfg.Configuration;import cn.itcast.hibernate.domain.User;public class Base {public static void main(String[] args) {Configuration cfg = new Configuration();cfg.configure();SessionFactory sf = cfg.buildSessionFactory();Session s = sf.openSession();Transaction tx =  s.beginTransaction();User u = new User();u.setBirthday(new Date());u.setName("name1");s.save(u);tx.commit();s.close();System.out.println("end");}}
在這段代碼中,首先載入設定檔,然後開啟session,再利用session開啟事務,然後把user對象儲存在session中,提交事務,關閉session。

在Hibernate中,預設是不會插入資料的,只有開啟事務才能夠插入。


我們運行之後,就可以探索資料庫中插入了一條資料。




【SSH三大架構】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.