Hibernate是由徐培成老師授課的,傳智播客的老師正是我要尋找的那種老師。有豐富的工作經驗和項目架構經驗。而且他們的教學經驗也很豐富!老徐講課十分精細,他編寫的熟練程度也很讓人吃驚!
Hibernate,以前老方在講解JAVAWEB的時候有給我們編寫過hibernate的簡易實現原理——一對多關系的ORM。當時感覺hibernate并不難,今天學習其實它本來就不難。Hibernate的最困難的地方就是它的那個配置文件。因為hibernate正靠那個配置文件來進行ORM的,也是靠這個配置文件來進行其他的操作,比如創建表格,限制對表或字段的操作…。
為什么要使用hibernate?老徐今天舉了一個十分好的例子:如果你的表中有100個字段,使用JDBC去插入記錄、更新記錄…這是非常恐怖的。如果插入或更新記錄出錯了呢?一個一個字段的對?想都不感想,如果以后數據庫字段要全部添加后綴名呢!~~哇哇哇,受不了了。Ok,hibernate可以很好的解決JDBC的“漏洞”。
這個配置文件我把它看成是hibernate的核心!
下面我來舉個簡單的例子,使用hibernate對一個DOMAIN對象進行自動存取。
1.定義一個Customer的domain,從學習數據庫至今使用顧客與訂單做的講解比較多,因為這種邏輯關系可以很好的闡明多表的關系,我們目前只使用一個表:
public class Customer { private int id; private String name; private int gender; private Date birthday; private String email; private String phone; private String address; //下面是getter和setter方法 … } |
2.在Customer的類路徑下添加hibernate的映射文件(元數據文件),映射文件名為Customer.hbm.xml,這樣在調用“ConfigurationObj. .addClass(Customer.class);”hibernate框架可以自動加載它。
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> <hibernate-mapping> <!-- 1.name 指定類名 2.table 指定表名,如果忽略則默認為類名 --> <class name="cn.itcast.cc.hibernate.persistence.Customer" table="customers" lazy="false"> <!-- 1.name OID的標識名 2.column 對應表的主鍵 3.type hibernate中的類型,對應JAVA類型為java.lang.Integer/int,對應SQL類型為INTEGER --> <id name="id" column="id" type="integer"> <!-- 1.class 設置id字段為自增、主鍵 --> <generator class="increment"></generator> </id> <!-- 1.name 對應類的屬性 2.column 對應表的字段,如果不指定access。此處的name只為hibernate獲取讀取字段的方法。 比如name="abcde",hibernate拼出getAbcde和setAbcde,而此時我們可以添加這兩個方法,用于實現特殊功能。 3.type hibernate中的類型 4.access hibernate直接訪問字段,而不通過getter和setter。 --> <property name="name" column="name" type="string" access="field"/> <property name="gender" column="gender" type="integer" /> <property name="birthday" column="birthday" type="date" /> <property name="phone" column="phone" type="string" /> <property name="email" column="email" type="string" /> <property name="address" column="address" type="string" /> </class> </hibernate-mapping> |
3.hibernate的屬性文件,使用時請將里面的中文行全部刪除:
hibernate.connection.driver_class=com.mysql.jdbc.Driver hibernate.connection.url=jdbc:mysql://localhost:3306/test hibernate.connection.username=root hibernate.connection.password=root #hibernate使用的本地化特性,比如MySQL和Oracle的分頁技術不同。 #hibernate會根據這項設置,自動完成分頁技術。 hibernate.dialect=org.hibernate.dialect.MySQL5Dialect #回顯SQL語句,hibernate根據元數據文件自動生成SQL語句并執行。 hibernate.show_sql=true #嚴重注意,如果指定為可以自動創建表,那么每次運行hibernate都會刪除并重新創建表。 hibernate.hbm2ddl.auto=create |
4.我們需要向工程中導入hibernate目錄下的jar文件。
1).hibernate3.jar.
2).lib/required/*.jar
3).3rd/*.jar
5.編寫測試類:
import java.util.Date; import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.Transaction; import org.hibernate.cfg.Configuration; import cn.itcast.cc.hibernate.persistence.Customer; public class private static SessionFactory sefac = null; // 當類被加載時,獲取SessionFactory。 static { //創建Configuration時,默認加載hibernate.properties文件。 //也可以調用addResource方法,將配置文件加載進來。 Configuration config = new Configuration(); // 將ORM中的O在此添加到 config.addClass(Customer.class); sefac = config.buildSessionFactory(); } public static void main(String[] args) { // 創建一個customer對象 Customer customer = new Customer(); customer.setName("changcheng"); customer.setGender(1); customer.setBirthday(new Date()); customer.setPhone("13998689955"); customer.setEmail("changcheng@google.com"); customer.setAddress(" // 調用保存 save(customer); // 調用查詢 find(1); } private static void save(Customer customer){ //獲取session相當于獲取了一個連接 Session session = sefac.openSession(); //此處必須使用事務,否則不能保存數據。 Transaction tr = session.beginTransaction(); //直接保存 session.save(customer); //提交事務 tr.commit(); //關閉會話 session.close(); } private static void find(Integer id){ //獲取session相當于獲取了一個連接 Session session = sefac.openSession(); Transaction tra = session.beginTransaction(); //直接保存 Customer customer = (Customer) session.load(Customer.class, id); tra.commit(); //關閉會話 session.close(); System.out.println(customer); } } |
上面是一個簡單的hibernate例子,今天也有學習hibernate的簡介與Java對象持久化概述。什么是持久化?就是將數據保存在外存里唄。比如硬盤、U盤…。Hibernate就是從業務邏輯層中抽取出來的持久化層!
除此之外,今天有講解一對多和多對一關系的映射。就是老方之前講過的,只不過這里使用了hibernate來實現自動存儲。多對一需要在class元素中添加一個“many-to-one”子元素,用于指定多對一的那個屬性的類型信息等。一對多需要在class元素中添加一個“set”子元素,用于指定一對多的那個set屬性的類型信息等。在此就不一一列舉了!