(1)、編寫配置文件 Hibernate通過讀寫預設的XML配置文件hibernate.cfg.xml載入資料庫配置信息、代碼如下: (2)、編寫持久化類 持久化類是Hibernate操作的對象、它與資料庫中的數據表相對應 (3)、編寫映射文件 (4)、編寫Hibernate的工具類 ...
(1)、編寫配置文件
Hibernate通過讀寫預設的XML配置文件hibernate.cfg.xml載入資料庫配置信息、代碼如下:
<hibernate-configuration> <session-factory> <!-- 資料庫驅動 --> <property name="connection.driver_class">com.mysql.jdbc.Driver</property> <!-- 資料庫連接的URL --> <property name="connection.url">jdbc:mysql://localhost:3306/db_database14</property> <!-- 資料庫連接用戶名 --> <property name="connection.username">root</property> <!-- 資料庫連接密碼 --> <property name="connection.password"></property> <!-- Hibernate方言 --> <property name="dialect">org.hibernate.dialect.MySQLDialect</property> <!-- 列印SQL語句 --> <property name="show_sql">true</property> <!-- 映射文件 --> <mapping resource="com/wgh/model/Book.hbm.xml" /> <mapping resource="com/wgh/model/Category.hbm.xml" /> </session-factory> </hibernate-configuration>
(2)、編寫持久化類
持久化類是Hibernate操作的對象、它與資料庫中的數據表相對應
(雙向)
/** * 圖書類別持久化類 */ public class Category { private Integer id; //ID private String name; //類別名稱 private Set<Book> books; //Set集合(類別中的所有圖書) public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Set<Book> getBooks() { return books; } public void setBooks(Set<Book> books) { this.books = books; } }
(單向)
/** * 圖書類別持久化類 */ public class Category { private Integer id; //ID private String name; //類別名稱 public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } }
/** * 圖書持久類 */ public class Book { private Integer id; //ID private String name; //圖書名稱 private String author; private Category category; //所屬類別 public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getAuthor() { return author; } public void setAuthor(String author) { this.author = author; } public Category getCategory() { return category; } public void setCategory(Category category) { this.category = category; } }
(3)、編寫映射文件
(雙向)
<hibernate-mapping package="com.wgh.model"> <class name="Category" table="tb_Category_manytoone3"> <id name="id"> <generator class="native"/> </id> <property name="name" not-null="true" length="200" /> <!-- 一對多映射 --> <set name="books"> <key column="categoryId"/> <one-to-many class="Book"/> </set> </class> </hibernate-mapping>
(單向)
<hibernate-mapping package="com.wgh.model"> <class name="Category" table="tb_Category_manytoone0"> <id name="id"> <generator class="native"/> </id> <property name="name" not-null="true" length="200" /> </class> </hibernate-mapping>
<hibernate-mapping package="com.wgh.model"> <class name="Book" table="tb_book_manytoone0"> <!-- 主鍵 --> <id name="id"> <generator class="native"/> </id> <!-- 圖書名稱 --> <property name="name" not-null="true" length="200" /> <!-- 作者 --> <property name="author" not-null="true" length="50"/> <!-- 多對一關聯映射 --> <many-to-one name="category" class="Category"> <!-- 映射的欄位 --> <column name="categoryId"/> </many-to-one> </class> </hibernate-mapping>
(4)、編寫Hibernate的工具類
package com.wgh.hibernate; import org.hibernate.HibernateException; import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.cfg.Configuration; import org.hibernate.service.ServiceRegistryBuilder; public class HibernateUtil { private static final ThreadLocal<Session> threadLocal = new ThreadLocal<Session>(); private static SessionFactory sessionFactory = null; // SessionFactory對象 // 靜態塊 static { try { Configuration cfg = new Configuration().configure(); // 載入Hibernate配置文件 sessionFactory = cfg .buildSessionFactory(new ServiceRegistryBuilder().applySettings(cfg.getProperties()) .buildServiceRegistry()); } catch (Exception e) { System.err.println("創建會話工廠失敗"); e.printStackTrace(); } } /** * 獲取Session * * @return Session * @throws HibernateException */ public static Session getSession() throws HibernateException { Session session = (Session) threadLocal.get(); if (session == null || !session.isOpen()) { if (sessionFactory == null) { rebuildSessionFactory(); } session = (sessionFactory != null) ? sessionFactory.openSession() : null; threadLocal.set(session); } return session; } /** * 重建會話工廠 */ public static void rebuildSessionFactory() { try { Configuration cfg = new Configuration().configure(); // 載入Hibernate配置文件 sessionFactory = cfg .buildSessionFactory(new ServiceRegistryBuilder().applySettings(cfg.getProperties()) .buildServiceRegistry()); } catch (Exception e) { System.err.println("創建會話工廠失敗"); e.printStackTrace(); } } /** * 獲取SessionFactory對象 * * @return SessionFactory對象 */ public static SessionFactory getSessionFactory() { return sessionFactory; } /** * 關閉Session * * @throws HibernateException */ public static void closeSession() throws HibernateException { Session session = (Session) threadLocal.get(); threadLocal.set(null); if (session != null) { session.close(); // 關閉Session } } }