標籤:hibernate 資料庫連接池
一、Hibernate出現背景
介紹Hibernate之前,先瞭解一下為什麼Hibernate會出現。一個技術的出現都是因為當前技術滿足不了需求。在Hibernate出現之前,對資料庫進行操作都是使用JDBC。但是JDBC有哪些優缺點呢?下面我們來詳細分析。
1.JDBC優缺點分析:
優點:速度快;
SQL 陳述式自己寫,把控性好。
缺點:代碼冗餘,頻繁的trycatch操作
查詢代碼特別繁瑣
不同的資料庫SQL語句有差異,代碼的可移植性差
沒有資料緩衝
不是物件導向
就是因為JDBC的這些缺點,導致Hibernate誕生。
2.Hibernate優缺點分析
優點:簡單
有資料緩衝:一級緩衝、二級緩衝、查詢快取
SQL語句由Hibernate內部產生,可移植性好
缺點:sql語句不是自己寫,不可控
資料庫特別大時,不識貨使用hibernate
二、持久層概述及ORM介紹1、持久層概述
分層結構是軟體設計中的一種重要設計思想。持久層是在軟體的三層體繫結構的基礎上發展起來的,它以解決對象和關係這兩大領域之間存在的不匹配問題為目標,為對象-關聯式資料庫之間提供了一個成功的映射解決方案。
2.ORM(Object/RelationMapping)對象-關係映射
ORM說白了就是讓關係型資料庫和對象發生關聯,解決資料庫中的表格到java中的Class對象之間的轉換問題。
通過配置映射問題***.hbm.xml來完成轉化。
對應檔的作用主要是完成以下幾個方面的轉化:
1.類與表的對應關係
2.類中的屬性名稱和表中的欄位名稱的對應關係
3.類中的屬性類型和表中的欄位類型的對應關係
4.把一對多和多對多的關係轉換成物件導向的關係
三、基於hibernate的入門應用1、myeclipse中建立java項目,命名為HibernateDemo2、拷貝相關的jar包到WEB-INF目錄下的lib檔案夾中。
筆者用的hibernate版本為hibernate3,所需要的jar包名稱為:
antlr-2.7.6.jar
backport-util-concurrent.jar
c3p0-0.9.1.jar 資料庫連接池包
commons-collections-3.1.jar 集合協助工具包
commons-logging-1.1.1.jar 日誌包
dom4j-1.6.1.jar 解析XML包
ehcache-1.5.0.jar 緩衝架構套件
hibernate3.jar hibernate核心包
javassist-3.9.0.GA.jar 代理模式包,解決懶載入問題
jta-1.1.jar
log4j.jar
mysql-connector-java-5.1.10-bin.jar 資料庫連接包
slf4j-api-1.5.8.jar 協助工具
slf4j-log4j12.jar 協助工具
並通過build path 把剛才拷貝的jar包添加到項目中去,。
3.編寫POJO映射類
在src目錄下建立com.hibernateTest.domain包,並在該包中建立Person類,Person類即為POJO類(Plain OldJava Objects,持久化類)。這裡對POJO類做一下介紹,POJO類很想javaBean,類中包含於資料庫表中相對應的各個屬性,這些屬性通過getter和setter方法來訪問,對外部因此了內部的實現細節。
Person類代碼為:
package com.hibernateTest.domain;import java.io.Serializable;/** * @author zhuwei * *///讓對象是實現Serializable,對象序列化的作用是讓對象可以在網路上傳輸//有java虛擬機器實現public class Person implements Serializable{private Long pid;public Long getPid(){return pid;}public void setPid(Long pid){this.pid = pid;}public String getPname(){return pname;}public void setPname(String pname){this.pname = pname;}private String pname;}4.對應檔編寫
在com.hibernateTest.domain中建立Person.hbm.xml對應檔。右鍵Person.hbm.xml,選擇開啟檔案為MyeclipseHibernate Mapping Editor,複製下面的代碼到檔案中,該代碼可以認為是模板代碼,。
<?xmlversion="1.0"encoding="utf-8"?>
<!DOCTYPEhibernate-mapping PUBLIC"-//Hibernate/HibernateMapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
</hibernate-mapping>
添加Person類和資料庫中相應表之間的映射代碼:
<!-- 用來描述一個持久化類
name 類的全名
table 表名,可以不寫,預設值和類名一樣
catalog 資料庫名稱,一般不寫
-->
<classname="com.hibernateTest.domain.Person"table="Person">
<!-- 標示屬性,和資料庫主鍵對應 ,將Person類的pid映射為表中的主鍵pid
column 列名稱
length 資料庫的欄位長度,預設資料庫最高的長度
type 類型
-->
<idname="pid"column="pid"length="5"type="java.lang.Long">
<!-- 主鍵產生器
告訴hibernate容器用什麼樣的方式產生主鍵
-->
<generatorclass="increment"></generator>
</id>
<!-- 映射person類的pname屬性 -->
<propertyname="pname"column="pname"length="20"type="java.lang.String"></property>
</class>
完成後,這個Person.hbm.xml的代碼為:
<?xml version="1.0" encoding="utf-8"?><!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN""http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"><hibernate-mapping> <!-- 用來描述一個持久化類 name 類的全名 table 表名,可以不寫,預設值和類名一樣 catalog 資料庫名稱,一般不寫 --> <class name="com.hibernateTest.domain.Person" table="Person"> <!-- 標示屬性,和資料庫主鍵對應 ,將Person類的pid映射為表中的主鍵pid column 列名稱 length 資料庫的欄位長度,預設資料庫最高的長度 type 類型 --> <id name="pid" column="pid" length="5" type="java.lang.Long"> <!-- 主鍵產生器 告訴hibernate容器用什麼樣的方式產生主鍵 --> <generator class="increment"></generator> </id> <!-- 映射person類的pname屬性 --> <property name="pname" column="pname" length="20" type="java.lang.String"></property> </class></hibernate-mapping>
5.編寫hibernate.cfg.xml設定檔
在src目錄下建立hibernate設定檔:好hibernate.cfg.xml,並選擇用Myeclipse hibernate config editor開啟。
這一部分的設定檔也是之間拷貝現成代碼,修改其中的登入名稱、密碼和資料庫名部分即可。
代碼為:
<?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只能串連一個資料庫--><session-factory><!-- 資料庫的使用者名稱--><property name="connection.username">root</property><!-- 密碼--><property name="connection.password">340621</property><!-- url--><property name="connection.url">jdbc:mysql://localhost:3306/hibernateTest</property><!-- 作用:根據持久化類和對應檔產生表validate 只驗證不產生create-drop 當hibernate啟動時產生表,hibernate結束時刪除表create 只要啟動hibernate時產生表update 在啟動hibernate容器時檢查持久化類和對應檔是不是對應,不對應則建立--><property name="hbm2ddl.auto">update</property><!-- 顯示hibernate內部產生的sql語句--><property name="show_sql">true</property><mapping resource="com/hibernateTest/domain/Person.hbm.xml" /></session-factory></hibernate-configuration>
其中語句<mappingresource="com/hibernateTest/domain/Person.hbm.xml"/>
要特別注意,在語句描述的是載入對應檔,設定方式為選擇用Myeclipse hibernate config editor開啟,刪除原有映射,重新載入即可。如所示。
6.編寫測試檔案
在工程根目錄下建立名為test的檔案,並在檔案中建立con.hibernateTest.test包,在保重建立CreateTable的測試類別,類代碼為:
package com.hibernateTest.test;import org.hibernate.cfg.Configuration;import org.junit.Test;public class CreateTable{@Testpublic void testCreateTable(){//載入設定檔Configuration configuration = new Configuration();configuration.configure();configuration.buildSessionFactory();}}
7.往表中插入記錄測試
在con.hibernateTest.test包中建立PersonTest類,代碼為:
package com.hibernateTest.test;import org.hibernate.Session;import org.hibernate.SessionFactory;import org.hibernate.Transaction;import org.hibernate.cfg.Configuration;import org.junit.Test;import com.hibernateTest.domain.Person;public class PersonTest{@Testpublic void testSavePerson(){Configuration configuration = new Configuration();configuration.configure();SessionFactory sessionfactory = configuration.buildSessionFactory();Session session = sessionfactory.openSession();Transaction transaction = session.beginTransaction();Person persion= new Person();persion.setPname("xiaobai");session.save(persion);transaction.commit();session.close();}}
運行該測試類別,可以探索資料庫表中多了一條記錄。
8.對第7步進行最佳化
看第7步的代碼hibernate配置部分是重複的,而且每一次對資料庫進行操作都要寫這部分代碼,可以考慮將這部分代碼放在一個專門的類中,操作資料庫的類繼承該類即可。
在src目錄下建立com.hibernateTest.util包,在該包中建立Hibernate類,讓hibernate載入部分在該類中完成,代碼為:
package com.hibernateTest.utils;import org.hibernate.SessionFactory;import org.hibernate.cfg.Configuration;public class HibernateUtils{public static SessionFactory sessionfactory;static{Configuration configuration = new Configuration();configuration.configure();sessionfactory = configuration.buildSessionFactory();}}
PersonTest類的代碼可以改為:
package com.hibernateTest.test;import org.hibernate.Session;import org.hibernate.SessionFactory;import org.hibernate.Transaction;import org.hibernate.cfg.Configuration;import org.junit.Test;import com.hibernateTest.domain.Person;import com.hibernateTest.utils.HibernateUtils;public class PersonTest extends HibernateUtils{@Testpublic void testSavePerson(){Session session = sessionfactory.openSession();Transaction transaction = session.beginTransaction();Person persion= new Person();persion.setPname("xiaobai");session.save(persion);transaction.commit();session.close();}}
Hibernate介紹及入門級應用開發