SSH(Spring+Struts2+Hibernate)整合

本博文介紹目前較流行的三大(Spring+Struts2+Hibernate)框架的整合。

一般整合需要以下幾個步驟:

1、首先導入相應的jar包

  Spring所需的jar包如下圖:
wKiom1e4XVuCzjZ9AAA_8KhJWmM066.jpg  

  Struts所需的jar包如下圖:

wKioL1e4XY-ANgTUAAA-AbNJedE939.jpg

   hibernate所需的jar包如下圖:

wKiom1e4XbWgV54jAAA5asJuSSc343.jpg

   一些共同所需的jar包如下圖:

wKiom1e4XcuTbM4kAAApu7KykDg609.jpg

其中mysql-connector-java-5.1.33-bin.jar是連接mysql數據庫所需的jar包。

將上述的jar包拷貝到項目的lib目錄下。

2、spring和struts整合

只需要Struts2-spring-plugin-2.3.24.jar包即可,上面已經引入。

在web.xml加入

<listener>

   <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>

</listener>

   ContextLoaderListener實現ServletContextListener接口,在服務器啓動時加載,

   默認自動載入置於WEB-INF目錄下的Spring配置文件applicationContext.xml

3、spring和hibernate整合

在Src目錄下添加SpringContext.xml文件。

(1)引入聲明式事務支持的XML命名空間

<?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:context="http://www.springframework.org/schema/context"
    xmlns:tx="http://www.springframework.org/schema/tx" 
    xmlns:aop="http://www.springframework.org/schema/aop"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xmlns:jee="http://www.springframework.org/schema/jee"
    xsi:schemaLocation="http://www.springframework.org/schema/mvc
    http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd
        http://www.springframework.org/schema/tx 
        http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
        http://www.springframework.org/schema/aop 
        http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
        http://www.springframework.org/schema/beans 
        http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
        http://www.springframework.org/schema/context 
        http://www.springframework.org/schema/context/spring-context-3.0.xsd
        http://www.springframework.org/schema/jee

(2)註解配置以及讀取外部文件

    <!-- 打開基於註解的配置 --> 
    <context:annotation-config/> 
     
    <!-- 開啓組件自動掃描 -->
    <context:component-scan base-package="com.icon.packSample"/>  
    <!-- 讀取外部屬性文件 -->
    <context:property-placeholder location="classpath:dataSource.properties"/>

(3)dataSource配置

    常用的連接池技術
    (i)apache的數據源 org.apache.commons.dbcp.BasicDataSource        
    需要引入這些jar包commons-dbcp-1.4.jar  commons-pool-1.6.jar
    (ii)阿里巴巴的數據源 com.alibaba.druid.pool.DruidDataSource
    (iii)c3p0 com.mchange.v2.c3p0.ComboPooledDataSource
    (iv)Proxool
 <!-- 配置數據源 -->

    <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource" 
        init-method="init" destroy-method="close">
        <!-- 初始化連接數量; -->
        <property name="initialSize" value="0" />
        <!-- 最大併發連接數 -->
        <property name="maxActive" value="20" />
        <!-- 最大空閒連接數 -->
        <property name="maxIdle" value="20" />
        <!-- 最小空閒連接數 -->
        <property name="minIdle" value="0" />
        <!-- 最大等待時長 -->
        <property name="maxWait" value="60000" />
        <!-- 超過時間限制是否回收 -->
        <property name="removeAbandoned" value="true" />
        <!-- 超過時間限制多長; -->
        <property name="removeAbandonedTimeout" value="180"/>      
         
         <!-- ${username},${url},${password},${driverClassName}表示佔位符,在外部文件中賦值,
         我們在ClassPath目錄下創建dataSource.properties對這些變量賦值 -->
        <!-- 數據源連接參數配置; -->
        <property name="username" value="${username}"/>
        <property name="url" value="${url}"/>
        <property name="password" value="${password}"/>
        <property name="driverClassName" value="${driverClassName}"/>
         
    </bean>

(4)sessionFactory配置

<!-- 配置SessionFactory -->
    <bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
        <property name="dataSource" ref="dataSource"/>    
        <property name="packagesToScan" value="net.xinqushi.model.model"/>
        <property name="hibernateProperties">
           <props>
              <prop key="hibernate.dialect">${hibernate.dialect}</prop>
              <prop key="hibernate.show_sql">${hibernate.show_sql}</prop>
              <prop key="hibernate.hbm2ddl.auto">${hibernate.hbm2ddl.auto}</prop>
              <prop key="hibernate.format_sql">${hibernate.format_sql}</prop>
            </props>
        </property>
    </bean>

(5)配置事務管理器

    <!-- 配置事務管理器 -->
    <bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
        <property name="sessionFactory" ref="sessionFactory"/>
    </bean>

(6)

初始化hibernateTemplate
  <bean id="hibernateTemplate" class="org.springframework.orm.hibernate3.HibernateTemplate">
   <property name="sessionFactory" ref="sessionFactory"></property>
  </bean>

(7)配置事務管理      

    <!-- 定義切面 -->
    <aop:config>
        <aop:pointcut expression="execution(* net.xinqushi.service.impl.*.* (..))" id="txPointCut"/>
        <aop:advisor advice-ref="txAdvice" pointcut-ref="txPointCut"/>
    </aop:config>
      
    <!-- 聲明式事務 -->
    <tx:advice id="txAdvice" transaction-manager="transactionManager">            
        <tx:attributes>
      <tx:method name="add*" propagation="REQUIRED"/>
      <tx:method name="get*" read-only="true" propagation="REQUIRED"/>
        </tx:attributes>        
    </tx:advice>
<!-- 開啓事務註解
    <tx:annotation-driven transaction-manager="transactionManager"/>
     -->

附:dataSource.properties

db.username=root
db.password=123456
db.url=jdbc:mysql://localhost:3306/album
db.driverClassName=com.mysql.jdbc.Driver
hibernate.dialect=org.hibernate.dialect.MySQLDialect
hibernate.show_sql=true
hibernate.hbm2ddl.auto=update
hibernate.format_sql=true

到此就將完整的SSH環境搭建好了。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章