標籤:
1. Annotation 的使用主要分為 2 步:
1.1 加入相應的 jar 包:
hibernate-annotations.jar // Annotation 核心包
ejb3-persistence.jar // 符合 jpa 標準的 Annotation 的實現
hibernate-commons-annotations.jar // 進行發射的時候使用
1.2 在 model 中使用 @ 註解的形式對類和屬性進行註解
2. 建立 hibernate_annotation ,工程結構目錄如:
3. 加入相應的 jar 包:
2.1 加入 hibernate 相應的 jar 包 。
2.2 加入 mysql 驅動的 jar 包 。
2.3 加入支援 annotations 的 jar 包 。
2.4 如:
4. 代碼:
4.1 Student.java
package com.hibernate.model;import javax.persistence.Entity;import javax.persistence.Id;@Entitypublic class Student {private int id;private String name;private int age;public Student(int id, String name, int age) {super();this.id = id;this.name = name;this.age = age;}@Idpublic 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 int getAge() {return age;}public void setAge(int age) {this.age = age;}}
4.2 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> <!-- Database connection settings --> <property name="connection.driver_class">com.mysql.jdbc.Driver</property> <property name="connection.url">jdbc:mysql://localhost/hibernate</property> <property name="connection.username">root</property> <property name="connection.password">root</property> <!-- JDBC connection pool (use the built-in) hibernate串連池--> <!-- <property name="connection.pool_size">1</property> --> <!-- SQL dialect --> <property name="dialect">org.hibernate.dialect.MySQLDialect</property> <!-- Enable Hibernate‘s automatic session context management --> <!-- <property name="current_session_context_class">thread</property> --> <!-- Disable the second-level cache --> <property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property> <!-- Echo all executed SQL to stdout --> <property name="show_sql">true</property> <!-- Drop and re-create the database schema on startup --> <property name="hbm2ddl.auto">update</property> <mapping class="com.hibernate.model.Student"/> </session-factory></hibernate-configuration>
4.3 StudentTest.java
package com.hibernate;import org.hibernate.Session;import org.hibernate.SessionFactory;import org.hibernate.cfg.AnnotationConfiguration;import org.hibernate.cfg.Configuration;import com.hibernate.model.Student;public class StudentTest {public static void main(String[] args) {Student s = new Student(1,"s1",18);//cfd.configure(),configure()不寫參數預設尋找src目錄下的hibernate.cfg.xmlConfiguration cfd = new AnnotationConfiguration(); //尋找設定檔//buildSessionFactory()產生一個SessionFactory工廠SessionFactory sf = cfd.configure().buildSessionFactory();Session session = sf.openSession();session.beginTransaction();session.save(s);session.getTransaction().commit();session.close();sf.close();}}
5. 部分詳解:
5.1 在 model Student.java 的類名上添加 @Entity 並引入 import javax.persistence.Entity; 使 Annotation 能夠識別這是一個實體類。
5.2 在 model Student.java 的 getId() 上添加 @Id 並引入 import javax.persistence.Id; 使 Annotation 能夠識別並表示這是一個主鍵。
5.3 在 hibernate.cfg.xml 中配置 <mapping class="com.hibernate.model.Student"/> 。
5.5 使用 Annotation 操作資料庫步驟:
5.5.1 new 一個 AnnotationConfiguration() 對象,並調用 configure() 尋找設定檔 hibernate.cfg.xml 。
5.5.2 擷取設定檔後通過 buildSessionFactory() 來建立 SessionFactory 。
5.5.3 通過 SessionFactory 的 openSession() 建立一個 Session 。
5.5.4 通過 Session 的 beginTransaction() 開啟一個事物 。
5.5.5 調用 Session 的 save() 、update() 、delete() 等方法執行資料庫操作 。
5.5.6 通過 Session 的 getTransaction() 擷取當前正在操作的事物,再通過把這個事物的 commit() 將資料更新至資料庫 。
5.5.7 最後關掉 Session 、 SessionFactory 。
6. 源碼下載
end。
hibernate學習筆記03-- hibernate + mysql + Annotation