hibernate學習筆記03-- hibernate + mysql + Annotation

1. Annotation 的使用主要分爲 2 步:java

1.1 加入相應的 jar 包:mysql

    hibernate-annotations.jar // Annotation  核心包sql

    ejb3-persistence.jar  // 符合 jpa 標準的 Annotation 的實現數據庫

    hibernate-commons-annotations.jar // 進行發射的時候使用session

1.2 在 model 中使用 @ 註解的形式對類和屬性進行註解
app

2. 新建 hibernate_annotation ,工程結構目錄以下圖:ide

    

3. 加入相應的 jar 包:ui

    2.1 加入 hibernate 相應的 jar 包 。this

    2.2 加入 mysql 驅動的 jar 包 。
url

    2.3 加入支持 annotations 的 jar 包 。

    2.4 以下圖:

    

4. 代碼:

    4.1 Student.java 

package com.hibernate.model;

import javax.persistence.Entity;
import javax.persistence.Id;

@Entity
public 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;
	}

	@Id
	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 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.xml
		Configuration 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。

相關文章
相關標籤/搜索