1. 程式人生 > >java專案中使用Hibernate的小例子

java專案中使用Hibernate的小例子

準備工作

  1. 下載Hibernate,地址http://hibernate.org/orm,即可下載最新版本,由於筆者用的書是以4.3.5來寫的,為了統一我也使用這個版本的吧,下載地址
  2. 下載好後的壓縮包解壓後找到lib/required子目錄下的所有jar包新增到應用的類載入路徑中,我這裡以eclipse為例,示例地址

做一個小例子

首先建立好hibernate.cfg.xml檔案,eclipse有自帶的工具去建立的,右擊我們的java專案,選擇【New】,找到Hibernate Configuration File,點選【next】 之後選擇好儲存的路徑,一般我們是放在src資料夾下的,在點【next】
輸入好資料庫連結資訊,點選【Finish】,就可以自動生成一個xml檔案,我們還需要在這個xml檔案中新增一些引數 配置好的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>
    	<!-- 指定連線資料庫的驅動類 -->
        <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
        <!-- 指定連線的資料庫的url,我這裡是連線到本地的test資料庫上 -->
        <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/test</property>
        <!-- 連線資料庫的使用者名稱 -->
        <property name="hibernate.connection.username">root</property>
        <!-- 連線資料庫的密碼(我這裡是為空的) -->
        <property name="hibernate.connection.password"></property>
        <!-- 指定連線池中的最大連線數 -->
        <property name="hibernate.c3p0.max_size">20</property>
        <!-- 指定連線池中最小連線數 -->
        <property name="hibernate.c3p0.min_size">1</property>
        <!-- 指定連線池中連線的超時時長 -->
        <property name="hibernate.c3p0.timeout">5000</property>
        <!-- 指定連線池裡最大快取多少個Statement物件 -->
        <property name="hibernate.c3p0.max_statements">100</property>
        <!-- 指定資料庫方言(就是說你這個資料庫是什麼型別的) -->
        <property name="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</property>
        <!-- 設定:根據需要自動建立(更新)資料庫表 -->
        <property name="hbm2ddl.auto">update</property>
        <!-- 顯示Hibernate持久化操作所生成的SQL -->
        <property name="show_sql">true</property>
        <!-- 將SQL指令碼進行格式化後再輸出 -->
        <property name="hibernate.format_sql">true</property>
        <!-- 羅列所有持久化類的類名 ,在這裡就是我們的entity-->
        <mapping class="entity.Person"/>
    </session-factory>
</hibernate-configuration>

之後我們建立一個類用於測試,內容如下
package test;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import org.hibernate.service.ServiceRegistry;
import org.hibernate.service.ServiceRegistryBuilder;

import entity.Person;

public class Test {
	public static void main(String[] args) {
		//不帶引數的configure方法將預設載入hibernate.cfg.xml檔案,如果傳入abc.xml作為引數
		//則不載入hibernate.cfg.xml,而改成abc.xml
		Configuration configuration = new Configuration().configure();

		ServiceRegistry serviceRegistry = new ServiceRegistryBuilder()
			.applySettings(configuration.getProperties()).buildServiceRegistry();
		
		//以Configuration例項建立SessionFactory例項
		SessionFactory sessionFactory = configuration.buildSessionFactory(serviceRegistry);
		
		//建立Session
		Session session = sessionFactory.openSession();
		
		//開始事務
		Transaction transaction = session.beginTransaction();
		
		//建立訊息物件,並設定屬性
		Person person = new Person();
		person.setName("admin");
		person.setWeight(100.5);
		
		//儲存訊息
		session.save(person);
		//提交事物
		transaction.commit();
		session.close();
		sessionFactory.close();
	}
}

如果在控制檯中沒有什麼問題,並且資料庫中加入了admin這條資料的話,說明hibernate已經可以使用了