今天我們進(jìn)行SSH配置的最后一步Hibernate的集成。

四,集成Hibernate

    4.1  集成Hibernate相關(guān)的:
        a) 這里我們采用的proxool連接池。
                


據(jù)說(shuō)在Hibernate提供的三種連接池中,效率最好的一個(gè)。這里我且聽信網(wǎng)絡(luò)大眾的話,等以后我有技術(shù)有能力了,一定會(huì)親自測(cè)試一下。


        b)   Hibernate映射采用Hibernate Annonations 技術(shù)。
                

還是在網(wǎng)上,翻了一堆資料后,發(fā)現(xiàn)annonations是最為優(yōu)雅,也是最為省力,效率最高的


    4.2    先創(chuàng)建一個(gè)數(shù)據(jù)庫(kù)。這里我創(chuàng)建一個(gè)叫 miziData 的數(shù)據(jù)庫(kù),并新建了一個(gè)叫User的表,這用來(lái)最后我們測(cè)試用。
          
/*==============================================================*/
/* DBMS name:      MySQL5                    */
/* Created on:     2009-04-27 22:50*/
/*==============================================================*/
SET FOREIGN_KEY_CHECKS=0;

DROP DATABASE IF EXISTS `miziData`;

CREATE DATABASE `miziData`
    
CHARACTER SET 'utf8'
    COLLATE 
'utf8_general_ci';
    
USE `miziData`;

/*==============================================================*/
/* Table: USER                                                  */
/*==============================================================*/
create table USER (
    ID                            
varchar(32)        not null,
    USERNAME        
varchar(32)        null,
    SEX            
int            null,
    AGE            
int            null,
    PASSWORD         
varchar(32)        null,
    CREATEDATE        
datetime        null,
    
constraint PK_USER primary key (ID)
)
type 
= InnoDB;


    4.3 配置proxool連接池信息,在src下面新建一個(gè)proxool.xml文件,并修改內(nèi)容如下:

 

<?xml version="1.0" encoding="utf-8"?>  
<!-- the proxool configuration can be embedded within your own application's.  
Anything outside the "proxool" tag is ignored. 
-->  
<something-else-entirely>  
    
<proxool> 
        
<!-- ### 連接池別名########### --> 
        
<alias>DbPool</alias>
        
        
<!-- ### proxool只能管理自己產(chǎn)生的連接########### --> 
        
<driver-url>jdbc:mysql://localhost:3306/miziData?characterEncoding=UTF-8</driver-url> 
        
        
<!-- ###JDBC驅(qū)動(dòng)程式########### -->    
        
<driver-class>com.mysql.jdbc.Driver</driver-class> 
        
        
<!-- ### 數(shù)據(jù)庫(kù)信息########### -->   
        
<driver-properties>   
            
<property name="user" value="root"/>   
            
<property name="password" value="root"/>   
        
</driver-properties>    
        
        
<!-- ### proxool自動(dòng)偵察各個(gè)連接狀態(tài)的時(shí)間間隔(毫秒),偵察到空閑的連接就馬上回收,超時(shí)的銷毀 ########### --> 
        
<house-keeping-sleep-time>28000</house-keeping-sleep-time>    
        
        
<!-- ### 空閑連接最少保持?jǐn)?shù)########### -->  
        
<prototype-count>10</prototype-count>  
        
        
<!-- ### 最大/最小連接數(shù)########### -->  
        
<maximum-connection-count>50</maximum-connection-count>   
        
<minimum-connection-count>10</minimum-connection-count>   
        
        
<!-- ### 最大/最小連接數(shù)########### -->  
        
<maximum-active-time>3600000</maximum-active-time>
        
        
<!-- ### 如果發(fā)現(xiàn)了空閑的數(shù)據(jù)庫(kù)連接 
                 house keeper  將會(huì)用這個(gè)語(yǔ)句來(lái)測(cè)試.這個(gè)語(yǔ)句最好非常快的被執(zhí)行.
                  如果沒有定義,測(cè)試過程將會(huì)被忽略########### 
--> 
        
<house-keeping-test-sql>select CURRENT_DATE</house-keeping-test-sql> 
        
        
<!-- ### 如果為true,在每個(gè)連接被測(cè)試前都會(huì)服務(wù)這個(gè)連接,如果一個(gè)連接失敗,那么將被丟棄,
                  另一個(gè)連接將會(huì)被處理,如果所有連接都失敗,一個(gè)新的連接將會(huì)被建立。否則將會(huì)拋出一個(gè)SQLException異常########### 
--> 
        
<test-before-use>true</test-before-use>
        
        
<!-- ### 如果為true,那么每個(gè)被執(zhí)行的SQL語(yǔ)句將會(huì)在執(zhí)行期被log記錄(DEBUG LEVEL).
                  你也可以注冊(cè)一個(gè)ConnectionListener (參看ProxoolFacade)得到這些信息########### 
-->
        
<trace>true</trace>
        
        
<!-- ### 日志統(tǒng)計(jì)跟蹤類型  ########### -->
        
<statistics-log-level>DEBUG</statistics-log-level> 
    
</proxool>  
</something-else-entirely>


     4.4  在Src下新建hibernate.xml,并修改內(nèi)容如下:

<?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>
    
        
<!-- ### 定義Hibernate的連接加載類  ########### -->
        
<property name="hibernate.connection.provider_class">org.hibernate.connection.ProxoolConnectionProvider</property>
        
        
<!-- ### 連接池別名,注意要與proxool的別名一致   ########### -->
        
<property name="hibernate.proxool.pool_alias">DbPool</property>
        
        
<!-- ### 向Hibernate聲明連接池的配置文件位置,通常與proxool在同一位置,如果不同請(qǐng)注意路徑   ########### -->
        
<property name="hibernate.proxool.xml">proxool.xml</property>
        
        
        
<!-- ### 聲明SQL語(yǔ)句的方言  ##########-->
        
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
        
        
        
<!-- ### 解決hibernate和jdbc不能同時(shí)使用proxool的問題 ########## -->
        
<!-- ### <property name="hibernate.proxool.existing_pool">true</property> ###########-->
        
<!-- ### Echo all executed SQL to stdout ########## -->
        
        
<!-- ### 定義是否顯示Hibernate生成的SQL語(yǔ)言,一般在調(diào)試階段設(shè)為true,完成后再改成false,這樣有利于調(diào)試  ##########-->
        
<property name="show_sql">true</property>
        
        
    
</session-factory>
    
</hibernate-configuration>

    4.5 修改application.xml 內(nèi)容如下:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi
="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:p
="http://www.springframework.org/schema/p"
       xmlns:aop
="http://www.springframework.org/schema/aop"
       xmlns:tx
="http://www.springframework.org/schema/tx"
       xsi:schemaLocation
="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
       http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
       http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd"
>
        
        
<!-- ### AnnotationSessionFactoryBean是從LocalSessionFactoryBean類繼承過來(lái)的 ########## -->
        
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
             
<property name="configLocation" value="classpath:hibernate.xml" />
        
</bean>
        
        
<!-- 
        * 這里bean的ID對(duì)應(yīng)的是 struts.xml action的class
        ****************
-->
        
<bean id="Hello" class="test.action.Hello" scope="prototype"></bean>

</beans>

    4.6 web.xml 加入 proxoolAdmn 監(jiān)聽,內(nèi)容如下,
<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_ID" version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
    
    
<!-- ###################################### -->
    
<!-- ##########  Struts2  ################## -->
    
<!-- ###################################### -->
    
    
<!-- 
     * Struts2的主要的Filter,負(fù)責(zé)四個(gè)方面的功能:
     * (1)執(zhí)行Actions
     * (2)清除ActionContext
     * (3)維護(hù)靜態(tài)內(nèi)容
     * (4)清除request生命周期內(nèi)的XWork的interceptors
     * 另注:該過濾器應(yīng)該過濾所有的請(qǐng)求URL。一般被設(shè)置為/*
     ************ 
-->
    
<filter>  
        
<filter-name>struts2</filter-name>  
        
<filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>  
    
</filter>
    
    
<!-- ### Proxool連接池監(jiān)聽 ########### --> 
    
<!-- @Version ver1.0 | 20090428  -->
    
<servlet>
        
<servlet-name>ProxoolAdmin</servlet-name>
        
<servlet-class>
            org.logicalcobwebs.proxool.admin.servlet.AdminServlet
        
</servlet-class>
    
</servlet>
    
    
    
<!-- ###################################### -->
    
<!-- ##########  Spring2  ################## -->
    
<!-- ###################################### -->
    
    
<!-- 
     * [ <context-param></context-param ] =>用來(lái)設(shè)定web站臺(tái)的環(huán)境參數(shù)
     * [ <param-name></param-name> ]  (子元素)=> 用來(lái)指定參數(shù)的名稱
     * [ <param-value></param-value> ] (子元素)=> 用來(lái)設(shè)定參數(shù)值
     * ************
     * 從類路徑下加載spring的配置文件, 多個(gè)配置文件可以用逗號(hào)和空格區(qū)分
     * classpath:  關(guān)鍵字特指類路徑下加載
     ******************** 
-->
    
    
<context-param>
        
<param-name>contextConfigLocation</param-name>
        
<param-value>classpath:applicationContext*.xml</param-value>
    
</context-param>
    
    
<!-- 
     * [<listener></listener>]=>用來(lái)設(shè)定監(jiān)聽接口
     * [<listener-class></listener-class>](子元素)=>定義Listener的類名稱
     * *******
     * 負(fù)責(zé)啟動(dòng)spring的監(jiān)聽器
     * 它將引用處的上下文參數(shù)獲得spring配置文件地址
     * 指定Spring提供的ContextLoaderListener Web 容器監(jiān)聽器,
     * 該監(jiān)聽器在web容器啟動(dòng)時(shí)自動(dòng)運(yùn)行并且根據(jù)ContextLoaderListener參數(shù)
     * 獲取Spring配置文件,并啟動(dòng)Spring容器。
     ************** 
-->
        
    
<listener>
        
<listener-class>
            org.springframework.web.context.ContextLoaderListener
        
</listener-class>
    
</listener>
    
    
    
    
    
<filter-mapping>  
        
<filter-name>struts2</filter-name>  
        
<url-pattern>/*</url-pattern>  
    
</filter-mapping>  
    
    
<servlet-mapping>
        
<servlet-name>ProxoolAdmin</servlet-name>
        
<url-pattern>/proxoolPool</url-pattern>
    
</servlet-mapping>
    
    
    
<display-name>miziStudy</display-name>
    
<welcome-file-list>
        
<welcome-file>index.html</welcome-file>
        
<welcome-file>index.htm</welcome-file>
        
<welcome-file>index.jsp</welcome-file>
        
<welcome-file>default.html</welcome-file>
        
<welcome-file>default.htm</welcome-file>
        
<welcome-file>default.jsp</welcome-file>
    
</welcome-file-list>
</web-app>



    4.7 導(dǎo)入相關(guān)包,這次關(guān)聯(lián)到的包很多,其中有包括hibernate,annotations,proxool等



我試過了 這些包 缺一不可。。。

    4.8 測(cè)試proxool 數(shù)據(jù)連接池是否工作。啟動(dòng)tomcat..成功后在瀏覽器中輸入http://localhost/miziStudy/proxoolPool
如果你得出的頁(yè)面和我一致,那么數(shù)據(jù)庫(kù)連接成功。

可以這么說(shuō)吧,SSH(Spring2.5+Struts2+Hibernate3.4)的最簡(jiǎn)配置到這里已經(jīng)結(jié)束了,但是這是永遠(yuǎn)不夠了,還缺了好多東西沒有,比如web.xxml的filter,spring 的AOP,切片,hibernate的事務(wù)等等許多還沒有在這里顯示出來(lái),但是起碼我們配置成功了,接下來(lái)我會(huì)不斷用新的實(shí)例來(lái)豐富這個(gè)簡(jiǎn)單的配置,大家拭目以待吧!!!其實(shí)偶自己是最期待的,因?yàn)槊總€(gè)實(shí)例出來(lái),就代表著我又進(jìn)一步。。。。加油!!