一. 概述:hibernate框架是作用于dao層的,實(shí)現(xiàn)對(duì)數(shù)據(jù)的持久化保存.通過面向?qū)ο蟮姆绞讲僮鲾?shù)據(jù)庫。
二. hibernate框架的搭建
1.導(dǎo)包
lib目錄下的required文件夾下的所有jar包.
mysql驅(qū)動(dòng)包.
2.創(chuàng)建數(shù)據(jù)庫于表.
3.創(chuàng)建實(shí)體類.
4.創(chuàng)建實(shí)體映射文件(以crm練習(xí)Customer類為例)
實(shí)體類名.hbm.xml
引入約束文件
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
|
<? 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"> <!-- 根元素 package(可選):填寫包名.后面凡是需要完整類名的地方,都可以省略包名了. --> < hibernate-mapping package = "cn.itcast.domain" > <!-- class:映射類與表的關(guān)系 name屬性:實(shí)體屬性名 table屬性:對(duì)應(yīng)的表名 --> < class name = "Customer" table = "cst_customer" > <!-- id:映射主鍵屬性名(OID)與主鍵列對(duì)應(yīng)關(guān)系 name屬性: OID名稱 column屬性(可選):主鍵列名,默認(rèn)值就是name屬性值 length屬性(可選):指定屬性長度.默認(rèn)值使用數(shù)據(jù)庫對(duì)應(yīng)列長度 type屬性(可選):指定當(dāng)前列(屬性)的類型.默認(rèn)值會(huì)根據(jù)數(shù)據(jù)庫類型自動(dòng)指定類型. type="long" hibernate類型 type="java.lang.Long" java類型 <column name="cust_id" sql-type="bigint" ></column> 數(shù)據(jù)庫類型 --> < id name = "cust_id" > <!--主鍵生成策略 increment:hibernate每次保存數(shù)據(jù)是,會(huì)查詢數(shù)據(jù)庫中最大的值,在最大值的基礎(chǔ)上加1作為新的主鍵值(測(cè)試時(shí)使用) identity:主鍵自增,有數(shù)據(jù)庫負(fù)責(zé)生成主鍵值 sequence:序列,Oracle時(shí)使用 hilo:高低位算法,適用于既不支持自增也不支持序列的庫(用不著) native:identity|sequence|hilo自動(dòng)三選一 uuid:主鍵類型為字符串是使用. assigned:有我們手動(dòng)指定ID值 --> < generator class = "native" ></ generator > </ id > <!-- property:映射非主鍵屬性名與非主鍵列對(duì)應(yīng)關(guān)系 name屬性: 屬性名 column屬性(可選):非主鍵列名,默認(rèn)值就是name屬性值 length屬性(可選):指定屬性長度.默認(rèn)值使用數(shù)據(jù)庫對(duì)應(yīng)列長度 type屬性(可選):指定當(dāng)前列(屬性)的類型.默認(rèn)值會(huì)根據(jù)數(shù)據(jù)庫類型自動(dòng)指定類型. type="long" hibernate類型 type="java.lang.Long" java類型 <column name="cust_id" sql-type="bigint" ></column> 數(shù)據(jù)庫類型 --> < property name = "cust_name" column = "cust_name" ></ property > < property name = "cust_source" ></ property > < property name = "cust_industry" column = "cust_industry" ></ property > < property name = "cust_level" column = "cust_level" ></ property > < property name = "cust_phone" column = "cust_phone" ></ property > < property name = "cust_mobile" column = "cust_mobile" ></ property > </ class > </ hibernate-mapping > |
創(chuàng)建主配置文件
hibernate.cfg.xml(在src下)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
|
<? 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 > <!-- 以下都是為sessionFactory對(duì)象配置的 --> < session-factory > <!-- 必選配置 //方言 //所有數(shù)據(jù)庫的sql語句都是基于SQL99標(biāo)準(zhǔn)的 //每個(gè)數(shù)據(jù)庫遵循SQL99標(biāo)準(zhǔn)的同時(shí),也會(huì)擴(kuò)充一部分SQL語句.這些標(biāo)準(zhǔn)之外的sql語句叫做方言 mysql方言: limit 0,5 //注意:mysql方言類一共有3個(gè).一定要選最短的 #hibernate.dialect org.hibernate.dialect.MySQLDialect //數(shù)據(jù)庫驅(qū)動(dòng) #hibernate.connection.driver_class com.mysql.jdbc.Driver //數(shù)據(jù)庫連接url #hibernate.connection.url jdbc:mysql:///test //連接用戶名 #hibernate.connection.username gavin //連接密碼 #hibernate.connection.password --> < property name = "hibernate.connection.driver_class" >com.mysql.jdbc.Driver</ property > < property name = "hibernate.connection.url" >jdbc:mysql:///hibernate_54</ property > < property name = "hibernate.connection.username" >root</ property > < property name = "hibernate.connection.password" >1234</ property > < property name = "hibernate.dialect" >org.hibernate.dialect.MySQLDialect</ property > <!-- 可選配置 //是否在控制臺(tái)顯示hibernate生成的sql hibernate.show_sql true //是否對(duì)顯示到控制臺(tái)的sql語句格式化 hibernate.format_sql true //自動(dòng)建表 # create(測(cè)試時(shí)使用) : 自動(dòng)建表,每次啟動(dòng)hibernate的時(shí)候都會(huì)自動(dòng)建表. # create-drop(測(cè)試時(shí)使用) : 自動(dòng)建表,每次啟動(dòng)hibernate的時(shí)候都會(huì)自動(dòng)建表.釋放資源時(shí)會(huì)將所有表刪除. # update(常用) : 自動(dòng)建表,有表就不會(huì)再創(chuàng)建,如果已經(jīng)存在的表不完全匹配.會(huì)自動(dòng)修改表結(jié)構(gòu). # validate : 校驗(yàn)表結(jié)構(gòu).不會(huì)自動(dòng)建表.每次hibernate啟動(dòng)時(shí)都會(huì)檢查表結(jié)構(gòu)是否正確. //不正確=>拋出異常. --> < property name = "hibernate.show_sql" >true</ property > < property name = "hibernate.format_sql" >true</ property > < property name = "hibernate.hbm2ddl.auto" >update</ property > <!-- 指定數(shù)據(jù)庫隔離級(jí)別 ## specify a JDBC isolation level #hibernate.connection.isolation 4 mysql 默認(rèn)級(jí)別是4 Oracle 默認(rèn)級(jí)別是2 --> < property name = "hibernate.connection.isolation" >4</ property > <!-- 配置session與當(dāng)前線程綁定 --> < property name = "hibernate.current_session_context_class" >thread</ property > <!-- 映射引入配置 resource屬性:填寫引入映射文件的路徑. 相對(duì)于src目錄下. --> < mapping resource = "cn/itcast/domain/Customer.hbm.xml" /> </ session-factory > </ hibernate-configuration > |
以上這篇hibernate框架環(huán)境搭建具體步驟(介紹)就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持服務(wù)器之家。