struts2.0 hibernate3.0 spring2.0 集成開發(一)

    由於公司原因,最近開始搞起來struts2.0 spring2.0 hibernate3.0,以前項目是struts1.X jdon ibatis.      hibernate和spring以前都接觸過,不過那時還在學校,東西在就已經還給老師了。最近開始在網上找各種資料。加上以前的一點基礎,算是問題不 大。
    先來說說struts2。0。這個框架給我的第一映象是,無非是一個現實層的框架,應該沒什麼很重要。可是慢慢深入後發現,struts2.0確實比起 struts1.0要進步不少。而且更加接近web2.0了。很不錯。hibernate是我一直都很喜歡的持久層框架,嘿嘿。因爲sql一直都不好,嘿 嘿。在就是業務層了。spring可以說是我一直都很深入去研究的一個框架。這回有機會也很好。     
     好了,說了這多廢話,我們開始第一節的內容吧.    第一節嘛,首先幫大家配置配置環境啦。讓大家有個感性的認識。先說道配置環境,首先談到這3個框架所需的jar吧,爲了避免大家去各個官網去下,我準備把這3個框架所需要的 jar打包傳到csdn的,可是大小超過了10M傳不上去,大家還是去下吧....
    在來介紹一下這3個框架用的配置文件。
  • applicationContext.xml這是spring的配置的文件(用來配置bean和數據庫的配置)
  • struts.xml 和struts1的配置文件差不多,用來負責action的配置
  • struts.properties 這個文件大家應該比較陌生,這是用來配置struts在運行時的一些參數
  • web。xml這不要說吧。
    需要說明的一點就是這裏沒有hibernate的配置文件,因爲已經集成到spring的配置文件裏面去了。
  •     這裏先從web.xml開始說起吧,他就像是房子的地基。不可或缺,這個文件沒什麼好說的,大家注意下里面的註釋就可以了。
    <?xml version="1.0" encoding="UTF-8"?>
    <web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
        xmlns:xsi
    ="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation
    ="http://java.sun.com/xml/ns/javaee 
        http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
    >
        
    <display-name>ASK123</display-name>
        
    <!-- 用來加載spring的配置文件 -->
        
    <context-param>
            
    <param-name>contextConfigLocation</param-name>
            
    <param-value>
                /WEB-INF/applicationContext.xml
            
    </param-value>
        
    </context-param>
        
    <context-param>
            
    <param-name>log4jConfigLocation</param-name>
            
    <param-value>/WEB-INF/log4j.properties</param-value>
        
    </context-param>
        
    <listener>
            
    <listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>
        
    </listener>
        
    <listener>
            
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
        
    </listener>
        
    <filter>
            
    <filter-name>encodingFilter</filter-name>
            
    <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
            
    <init-param>
                
    <param-name>encoding</param-name>
                
    <param-value>UTF-8</param-value>
            
    </init-param>
        
    </filter>
        
    <!-- z這裏要特別注意,這裏是在加載struts的核心類FilterDispatcher -->
        
    <filter>
            
    <filter-name>struts2</filter-name>
            
    <filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
        
    </filter>
        
    <filter-mapping>
            
    <filter-name>struts2</filter-name>
            
    <url-pattern>/*</url-pattern>
        
    </filter-mapping>

        
    <welcome-file-list>
            
    <welcome-file>login.jsp</welcome-file>
        
    </welcome-file-list>
    </web-app>
  • 我們在來看看spring的配置文件。這個文件用來處理hibernate的一些數據庫配置和管理bean
    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
        xmlns:xsi
    ="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation
    ="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">

        
    <bean id="propertyConfigurer"
            class
    ="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
            
    <property name="locations">
                
    <list>
                    
    <!-- 這裏面我把連接數據庫的參數封裝到了datasource.properties這個文件裏 -->
                    
    <value>classpath:datasource.properties</value>
                
    </list>
            
    </property>
        
    </bean>
        
    <!-- 從datasource.properties這個文件獲得參數 -->
        
    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close"
            dependency-check
    ="none">
            
    <property name="driverClass">
                
    <value>${datasource.driverClassName}</value>
            
    </property>
            
    <property name="jdbcUrl">
                
    <value>${datasource.url}</value>
            
    </property>
            
    <property name="user">
                
    <value>${datasource.username}</value>
            
    </property>
            
    <property name="password">
                
    <value>${datasource.password}</value>
            
    </property>
            
    <property name="acquireIncrement">
                
    <value>${c3p0.acquireIncrement}</value>
            
    </property>
            
    <property name="initialPoolSize">
                
    <value>${c3p0.initialPoolSize}</value>
            
    </property>
            
    <property name="minPoolSize">
                
    <value>${c3p0.minPoolSize}</value>
            
    </property>
            
    <property name="maxPoolSize">
                
    <value>${c3p0.maxPoolSize}</value>
            
    </property>
            
    <property name="maxIdleTime">
                
    <value>${c3p0.maxIdleTime}</value>
            
    </property>
            
    <property name="idleConnectionTestPeriod">
                
    <value>${c3p0.idleConnectionTestPeriod}</value>
            
    </property>
            
    <property name="maxStatements">
                
    <value>${c3p0.maxStatements}</value>
            
    </property>
            
    <property name="numHelperThreads">
                
    <value>${c3p0.numHelperThreads}</value>
            
    </property>
        
    </bean>
        
    <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
            
    <property name="dataSource">
                
    <ref local="dataSource" />
            
    </property>
            
    <!-- 加載hibernate的hbm.xml文件這個文件用映射表和model之間的關係 -->
            
    <property name="mappingResources">
                
    <list>
                    
    <value>net/ask123/ecommerce/domain/User.hbm.xml</value>
                
    </list>
            
    </property>
            
    <property name="hibernateProperties">
                
    <props>
                    
    <prop key="hibernate.dialect">${hibernate.dialect}</prop>
                    
    <prop key="hibernate.show_sql">${hibernate.show_sql}</prop>
                    
    <prop key="hibernate.jdbc.fetch_size">${hibernate.jdbc.fetch_size}</prop>
                    
    <prop key="hibernate.jdbc.batch_size">${hibernate.jdbc.batch_size}</prop>
                
    </props>
            
    </property>
        
    </bean>
        
    <!-- 配置事務管理器bean,使用HibernateTransactionManager事務管理器 -->
        
    <bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
            
    <!-- 爲事務管理器注入sessionFactory" -->
            
    <property name="sessionFactory" ref="sessionFactory" />
        
    </bean>
        
    <!-- 配置事務攔截器Bean -->
        
    <bean id="transactionInterceptor"
            class
    ="org.springframework.transaction.interceptor.TransactionInterceptor">
            
    <!-- 爲事務攔截器bean注入一個事物管理器 -->
            
    <property name="transactionManager" ref="transactionManager"></property>
            
    <property name="transactionAttributes">
                
    <!-- 定義事務傳播屬性 -->
                
    <props>
                    
    <prop key="insert*">PROPAGATION_REQUIRED</prop>
                    
    <prop key="update*">PROPAGATION_REQUIRED</prop>
                    
    <prop key="save*">PROPAGATION_REQUIRED</prop>
                    
    <prop key="add*">PROPAGATION_REQUIRED</prop>
                    
    <prop key="remove*">PROPAGATION_REQUIRED</prop>
                    
    <prop key="delete*">PROPAGATION_REQUIRED</prop>
                    
    <prop key="get*">PROPAGATION_REQUIRED,readOnly</prop>
                    
    <prop key="find*">PROPAGATION_REQUIRED,readOnly</prop>
                    
    <prop key="load*">PROPAGATION_REQUIRED,readOnly</prop>
                    
    <prop key="change*">PROPAGATION_REQUIRED</prop>
                    
    <!-- <prop key="*">PROPAGATION_REQUIRED,readOnly</prop> -->
                
    </props>
            
    </property>
        
    </bean>
        
    <!-- 定義BeanNameAutoProxyCreator -->
        
    <bean class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator">
            
    <!-- 指定滿足哪些bean自動生成業務代理 -->
            
    <property name="beanNames">
                
    <!-- 需要自動創建事務代理的bean -->
                
    <list>
                    
    <value>userService</value>
                
    </list>
                
    <!-- 其它需要自動創建事務代理的bean -->
            
    </property>
            
    <property name="interceptorNames">
                
    <list>
                    
    <value>transactionInterceptor</value>
                    
    <!-- 可增加其它的interceptor -->
                
    </list>
            
    </property>
        
    </bean>
    </beans>
  • 往下就是struts。xml了這個文件和以前的struts的配置文件差不多
    <!DOCTYPE struts PUBLIC
        "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
        "http://struts.apache.org/dtds/struts-2.0.dtd" 
    >
    <struts>
        
    <package name="" namespace="" extends="struts-default">
            
    <!-- 配置一個action -->
            
    <action name="login" class="loginAction">
                
    <!-- 這裏的type指的是模板,默認是jsp -->
                
    <!--  這裏的意思是登錄成功後跳轉到 /index.jsp 頁面 -->
                
    <result name="success">
                    
    <param name="location">/welcome.jsp</param>
                
    </result>
                
    <!--  登錄失敗後跳轉到 /error.jsp 頁面 -->
                
    <result name="error" type="dispatcher">
                    
    <param name="location">/error.jsp</param>
                
    </result>
                
    <!--  如果驗證失敗,則返回登錄頁 -->
                
    <result name="input" type="dispatcher">
                    
    <param name="location">/login.jsp</param>
                
    </result>
                
    <!--  <interceptor-ref name="validationWorkflowStack" />-->
            
    </action>
            
            
    <!-- userAction -->
            
    <action name="list" class="userAction" method="findUsersAll">
                
    <result name="list">
                    
    <param name="location">/list.jsp</param>
                
    </result>
            
    </action>
            
            
    <action name="saveUser" class="userAction" method="saveUser">
                
    <result name="success" type="redirect">list.action</result>
            
    </action>
            
            
    <action name="delUser" class="userAction" method="delUser">
                
    <result name="success" type="redirect">list.action</result>
            
    </action>
            
        
    </package>
    </struts>
  • 好累,最後一個了struts.properties
    #告訴struts加載spring
    struts.objectFactory 
    = spring
  • 工具xml(datasoruce.propertise)
    datasource.type=oracle
    datasource.driverClassName
    =oracle.jdbc.driver.OracleDriver
    datasource.url
    =jdbc:oracle:thin:@192.168.1.2:1521:ask
    datasource.username
    =ask
    datasource.password
    =ask

    datasource.maxActive
    =10
    datasource.maxIdle
    =2
    datasource.maxWait
    =120000
    datasource.whenExhaustedAction
    =1
    datasource.validationQuery
    =select 1 from dual
    datasource.testOnBorrow
    =true
    datasource.testOnReturn
    =false

    c3p0.acquireIncrement
    =3
    c3p0.initialPoolSize
    =3
    c3p0.idleConnectionTestPeriod
    =900
    c3p0.minPoolSize
    =2
    c3p0.maxPoolSize
    =50
    c3p0.maxStatements
    =100
    c3p0.numHelperThreads
    =10
    c3p0.maxIdleTime
    =60

    hibernate.dialect
    =org.hibernate.dialect.OracleDialect
    hibernate.jdbc.batch_size
    =25
    hibernate.jdbc.fetch_size
    =50
    hibernate.show_sql
    =true
    hibernate.connection.release_mode
    =after_transaction
    好了,重要的配置文件已經介紹完了。如果你現在看的雲裏霧裏沒關係,先有個感性的認識我們在慢慢來。
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章