hibernate學習筆記02-- eclipse 下 hibernate+mysql 的實現。

hibernate 環境的配置步驟:java

  1. 加入 hibernate 所需的 jar 包,並將這些 jar 添加到 project 中,如圖:mysql


  2. hibernate.cfg.xml 的創建。hibernate 的 hibernate.cfg.xml 配置文件默認在 project/src 目錄下,如圖: sql

  3. hibernate.cfg.xml 中的內容以下(固然,這是從 hibernate 的參考文檔中拷貝過來的,還未修改):
    數據庫

<?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">org.hsqldb.jdbcDriver</property>
        <property name="connection.url">jdbc:hsqldb:hsql://localhost</property>
        <property name="connection.username">sa</property>
        <property name="connection.password"></property>

        <!-- JDBC connection pool (use the built-in) -->
        <property name="connection.pool_size">1</property>

        <!-- SQL dialect -->
        <property name="dialect">org.hibernate.dialect.HSQLDialect</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 resource="org/hibernate/tutorial/domain/Event.hbm.xml"/>

    </session-factory>

</hibernate-configuration>

至此,hibernate 環境配置完成。session

hibernate 與 mysql 集成:
app

  1. 添加支持 mysql 驅動的 jar 包,以下圖:dom

    

    2. 修改 hibernate.cfg.xml ide

<!-- Database connection settings -->
        <property name="connection.driver_class">org.hsqldb.jdbcDriver</property>
        <property name="connection.url">jdbc:hsqldb:hsql://localhost</property>
        <property name="connection.username">sa</property>
        <property name="connection.password"></property>
        
        
        <!-- SQL dialect -->
        <property name="dialect">org.hibernate.dialect.HSQLDialect</property>

    修改成 mysql 的鏈接方式:
測試

<!-- 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>
        
        
        <!-- SQL dialect -->
        <property name="dialect">org.hibernate.dialect.MySQLDialect</property>

至此,hibernate + mysql 環境搭建完成(固然,要在 mysql 數據庫下創建一個名爲 hibernate 的數據庫)。
ui

下面咱們測試一下:

  1. 創建如下包和類 ,並修改 hibernate.cfg.xml

  2. 貼下代碼:

    2.1 Student.java

package com.hibernate.model;

public class Student {

	private int id;
	private String name;
	private int age;

	public int getAge() {
		return age;
	}

	public int getId() {
		return id;
	}

	public String getName() {
		return name;
	}

	public void setAge(int age) {
		this.age = age;
	}

	public void setId(int id) {
		this.id = id;
	}

	public void setName(String name) {
		this.name = name;
	}

}

    2.2 Student.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="com.hibernate.model">
	<class name="Student">
		<id name="id"/>
		<property name="name"/>
		<property name="age"/>
	</class>
</hibernate-mapping>

    2.3 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 resource="com/hibernate/model/Student.hbm.xml"/>

    </session-factory>

</hibernate-configuration>

    2.4 StudentTest

package com.hibernate;


import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;

import com.hibernate.model.Student;

public class StudentTest {

	public static void main(String[] args) {
		Student s = new Student();
		s.setId(1);
		s.setName("s1");
		s.setAge(18);
		
		//cfd.configure(),configure()不寫參數默認查找src目錄下的hibernate.cfg.xml
		Configuration cfd = new Configuration();
		//buildSessionFactory()產生一個SessionFactory工廠
		SessionFactory sf = cfd.configure().buildSessionFactory();
		Session session = sf.openSession();
		session.beginTransaction();
		session.save(s);
		session.getTransaction().commit();
		session.close();
		sf.close();
	}
}

運行下 StudentTest 類中的 main 方法,則在名爲 hibernate 數據庫中新建立一個名爲 student 的表並添加數據,以下圖:

---------------------------------------------------------------------------------------------------------------------

注:

hibernate 使用操做數據庫的步驟:

  1. 經過 Configuration 類的 configure() 查找配置文件 hibernate.cfg.xml  。

  2. 獲取配置文件後經過 buildSessionFactory() 來建立 SessionFactory 。

  3. 經過 SessionFactory  的 openSession() 建立一個 Session 。

  4. 經過 Session 的 beginTransaction() 開啓一個事物 。

  5. 調用 Session 的 save() 、update() 、delete() 等方法執行數據庫操做 。

  6. 經過 Session 的 getTransaction() 獲取當前正在操做的事物,再經過把這個事物的 commit() 將數據更新至數據庫 。

  7. 最後關掉 Session 、 SessionFactory  。

                Configuration cfd = new Configuration(); 
		SessionFactory sf = cfd.configure().buildSessionFactory();
		Session session = sf.openSession();
		session.beginTransaction();
		//session.save(s); 調用session的增刪改查方法
		session.getTransaction().commit();
		session.close();
		sf.close();

源碼下載

end。

相關文章
相關標籤/搜索