1. 程式人生 > >Hibernate環境搭建與測試

Hibernate環境搭建與測試

hibernate是一個優秀的輕量級ORM框架,封裝了JDBC操作,可以讓程式設計人員以面向物件的方式操作資料庫

一. 先到hibernate官網下載最新hibernate包,我下載的是2017最新版本hibernate-release-5.2.10.Final.zip

http://hibernate.org/orm/


二.解壓檔案,把required檔案下所有jar包匯入工程

hibernate-release-5.2.10.Final\lib\required



三. 匯入其他包:必須包含日誌包、MySQL或Oracle驅動包


3. 建立資料庫表單,和javaBean實體,這裡可以自己隨便建立一個

這裡建立一個Customer物件 和 mysql表單,屬性相互對應


4.建立對映關係

a. 在實體包下面建立對應的xml檔案,名字互相對應,最好和類使用一樣的名字,字尾名.hbm.xlm

b.建立物件對映xml約束,也就是在xml前面加一行標籤敘述,這行字在hibernate匯入的包裡,具體路徑如下:

·

貼上過來後看到這個提示就說明約束建立成功

c 配置對映關係

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC 
    "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
    "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
	<class name="com.ssh.domain.Customer" table="cst_customer">
		<!--主鍵 -->
		<id name="cust_id" column="cust_id">
			<!--主鍵生成策略  -->
			<generator class="native"></generator>
		</id>
		<!-- 屬性 name代表類屬性,column代表資料庫列名-->
		<property name="cust_name" column="cust_name"></property>
		<property name="cust_user_id" column="cust_user_id"></property>
		<property name="cust_create_id" column="cust_create_id"></property>
		<property name="cust_source" column="cust_source"></property>
		<property name="cust_industry" column="cust_industry"></property>
		<property name="cust_level" column="cust_level"></property>
		<property name="cust_linkman" column="cust_linkman"></property>
		<property name="cust_phone" column="cust_phone"></property>
		<property name="cust_mobile" column="cust_mobile"></property>
	
	</class>
</hibernate-mapping>

四. 編寫hibernate核心配置檔案

約束方式和上面一樣,但是命名必須是hibernate.cfg.xml,且必須放在src根目錄下

\hibernate-release-5.2.10.Final\project\etc目錄下有個檔案hibernate.properties

在這個檔案裡面搜尋你要配置資料庫連線資訊,copy到xml中進行配置,比如mysql就搜尋mysql,會看到以下行,然後進行配置:

## MySQL

#hibernate.dialect org.hibernate.dialect.MySQLDialect
#hibernate.dialect org.hibernate.dialect.MySQLInnoDBDialect
#hibernate.dialect org.hibernate.dialect.MySQLMyISAMDialect
#hibernate.connection.driver_class com.mysql.jdbc.Driver
#hibernate.connection.url jdbc:mysql:///test
#hibernate.connection.username gavin
#hibernate.connection.password

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
	"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
	"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
	<!-- 一個數據對應一個session-factory -->
	<session-factory>
		<!-- 必須配置的5個引數:資料庫4個引數和資料庫方言 -->
		<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
		<property name="hibernate.connection.url">jdbc:mysql:///ssh</property>
		<property name="hibernate.connection.username">root</property>
		<property name="hibernate.connection.password">root</property>
		<!-- 資料庫方言,不同資料庫不同配置 -->
		<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
		<!-- 可選配置 -->
		<!-- 在控制檯顯示SQL語句 -->
		<property name="hibernate.show_sql">true</property>
		<!-- 格式化SQL語句 -->
		<property name="hibernate.format_sql">true</property>
		<!-- 生成資料庫表結構 ,create:每次刪除原表重新建新表,測試使用-->
		<!-- 生成資料庫表結構 ,update:如果沒有表,則建立表,如果有表則正常更新; 可以自動增加欄位也就是表列-->
		<!--validate校驗對映關係,與配置不一致則報錯  -->
		<property name="hibernate.hbm2ddl.auto">update</property>
		<!-- 配置對映檔案,引入配置檔案路徑 -->
		<mapping resource="com/ssh/domain/Customer.hbm.xml"/>
	</session-factory>
</hibernate-configuration>
四 編寫測試類,確認是否搭建成功


package com.ssh.demo;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import org.junit.Test;

import com.ssh.domain.Customer;

public class Demo1 {
/*
 * 第一個程式:測試hibernate
 */
	@Test
	public void test1(){
		 //1載入配置檔案
		Configuration config = new Configuration();
		//預設載入src下hibernate.cfg.xml檔案
		config.configure();
		//2建立SessionFactory物件
		SessionFactory factory = config.buildSessionFactory();
		//3建立session物件
		Session session = factory.openSession();
		//4開啟事務
		Transaction trans = session.beginTransaction();
		//5執行程式碼
		Customer cus = new Customer();
		cus.setCust_name("raylu");
		cus.setCust_mobile("23423424");
		//儲存資料
		session.save(cus);
		//6提交事務//回滾事務
		trans.commit();
		//7釋放資源
		factory.close();
		session.close();
	}
}

看看資料庫有資料了說明執行成功!