SSH 框架整合(maven版本 xml配置方式 連線池c3p0)

沒事偷著樂琅發表於2017-03-09

如何整合Struts2 Hibernate 和Spring 首先你必須瞭解三個框架是如何獨立執行的:
程式碼下載:http://download.csdn.net/detail/zhanglinlang/9775160

Struts2 是通過在web.xml 中 Filter攔截器來啟動自己的核心類StrutsPrepareAndExecuteFilter,完成事件的分發。

Spring 是通過 在web.xml 註冊監聽器來監聽ContextLoaderListener,監聽web應用的啟動事件來完成自己的框架啟動工作。

Hibernate 是通過建立SessionFactory工廠手動載入配置檔案來完成啟動工作,Hibernate是可以通過程式碼建立SessionFactory,就可以交給Spring通過ioc來控制該類的例項化。

下面就是整合程式碼:目錄結構如下
這裡寫圖片描述

首先在resource目錄下建立 config目錄方便管理:
首先配置
Hibernate

<!DOCTYPE hibernate-configuration PUBLIC
    "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
    "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">

<hibernate-configuration>
    <session-factory name="foo">

        <!--  配置   列印sql語句到控制檯   可選 -->
        <property name="hibernate.show_sql">true</property>
        <property name="hibernate.format_sql">true</property>
        <!-- 
            (重要配置)Hibernate自動建表 配置 
            create:先刪除,再建立
            update:如果表不存在就建立,不一樣就更新,一樣就什麼都不做。
            create-drop:初始化時建立表,SessionFactory執行close()時刪除表。
            validate:驗證表結構是否一致,如果不一致,就拋異常。
         -->
        <property name="hbm2ddl.auto">update</property>

        <!-- 
            設定預設的事務隔離級別:
            隔離級別        對應的整數表示
            READ UNCOMMITED 1
            READ COMMITED   2
            REPEATABLE READ 4
            SERIALIZEABLE   8
         -->
        <property name="connection.isolation">4</property>

        <!-- 使用二級快取,預設是未開啟的。 -->
        <!-- 指定要使用的快取的提供商,這也就開啟了二級快取 
        <property name="cache.provider_class">org.hibernate.cache.HashtableCacheProvider</property>
        <property name="cache.provider_class">org.hibernate.cache.EhCacheProvider</property>
        -->
        <!-- 開啟使用查詢快取 
        <property name="cache.use_query_cache">true</property>
        -->
        <!-- 指定要使用二級快取的實體類
        <class-cache usage="read-write" class="cn.itcast.l_second_cache.Employee"/>
        <class-cache usage="read-write" class="cn.itcast.l_second_cache.Department"/>
        <collection-cache usage="read-write" collection="cn.itcast.l_second_cache.Department.employees"/>
        -->

        <!-- 匯入對映檔案 -->
        <mapping resource="com/zll/entity/User.hbm.xml"/>
        <mapping resource="com/zll/entity/Student.hbm.xml"/>

    </session-factory>
</hibernate-configuration>

Struts.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
    "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
    "http://struts.apache.org/dtds/struts-2.0.dtd">

<struts>
     <package name="person" namespace="/" extends="struts-default">
        <!-- (測試使用)action的建立交給Spring來管理,這裡的class 只需要填寫spring裡對應action物件的bean的id -->
        <action name="userAction" class="userAction" />
     </package>
</struts>

Spring.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: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/context
           http://www.springframework.org/schema/context/spring-context-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">

    <!-- (重要步驟)c3p0連線池配置 -->
    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
        <property name="driverClass" value="com.mysql.jdbc.Driver"></property>
        <property name="jdbcUrl" value="jdbc:mysql:///hibernate?characterEncoding=utf-8"></property>
        <property name="user" value="root"></property>
        <property name="password" value="root"></property>
    </bean>

    <!-- (重要步驟)spring整合Hibernate配置,這裡將Hibernate建立sessionFactory的方法交給Spring管理。 -->
    <bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
        <property name="dataSource" ref="dataSource"></property>
        <property name="configLocations" value="classpath:config/hibernate.cfg.xml"></property>
    </bean>

    <!-- (重要步驟)配置Hibernate 事務 如果不開啟,資料庫是 read_only狀態 -->
    <bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
        <property name="sessionFactory" ref="sessionFactory"></property>
    </bean>

    <!-- 開啟事務註解 -->
    <tx:annotation-driven transaction-manager="transactionManager"/>

    <!-- 配置Hibernate 的jdbc模板 -->
    <bean id="hibernateTemplate" class="org.springframework.orm.hibernate4.HibernateTemplate">
        <property name="sessionFactory" ref="sessionFactory"></property>
    </bean>

    <!-- 測試程式碼,通過匯入檔案方式,可以將多個模組分開配置 -->
    <import resource="classpath:config/testTemple.xml"/>
</beans>

testTemple.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: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/context
           http://www.springframework.org/schema/context/spring-context-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">

    <!-- 配置測試用的實體類 -->
    <bean id="userAction" class="com.zll.action.UserAction" scope="prototype">
        <property name="userService" ref="userService"></property>
    </bean>

    <bean id="userService" class="com.zll.service.UserService">
        <property name="userDao" ref="userDaoImp"></property>
    </bean>

    <bean id="userDaoImp" class="com.zll.dao.UserDaoImp">
        <property name="hibernateTemplate" ref="hibernateTemplate"></property>
    </bean>


</beans>

最重要的:
web.xml

<!DOCTYPE web-app PUBLIC
 "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
 "http://java.sun.com/dtd/web-app_2_3.dtd" >

<web-app>
    <display-name>Archetype Created Web Application</display-name>

    <!-- Spring ContextLoaderListener 配置檔案的設定,如果不配置 預設讀取 WEB-INF下的applicationContext.xml -->
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:config/spring.xml</param-value>
    </context-param>


    <!-- (重要配置) 註冊struts的 StrutsPrepareAndExecuteFilter 類,攔截訪問,並根據訪問路徑進行分發 -->
    <filter>
        <filter-name>struts2</filter-name>
        <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
        <init-param>
            <param-name>config</param-name>
            <param-value>struts-default.xml,struts-plugin.xml,config/struts.xml</param-value>
        </init-param>
    </filter>
    <!-- 攔截/* 的所有訪問,都會交給 -->
    <filter-mapping>
        <filter-name>struts2</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>
    <!-- (重要配置)Spring 啟動程式碼,註冊監聽器,監聽servlet啟動, -->
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
</web-app>

最後pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.zll</groupId>
    <artifactId>sshTest</artifactId>
    <packaging>war</packaging>
    <version>0.0.1-SNAPSHOT</version>
    <name>sshTest Maven Webapp</name>
    <url>http://maven.apache.org</url>
    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <spring.version>4.1.4.RELEASE</spring.version>
        <hibernate.version>4.3.8.Final</hibernate.version>
        <struts2.version>2.3.20</struts2.version>
        <jackson.version>2.5.0</jackson.version>
    </properties>


    <dependencies>

        <!-- junit -->
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
            <scope>test</scope>
        </dependency>

        <!-- spring -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-core</artifactId>
            <version>${spring.version}</version>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-beans</artifactId>
            <version>${spring.version}</version>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>${spring.version}</version>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-tx</artifactId>
            <version>${spring.version}</version>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-web</artifactId>
            <version>${spring.version}</version>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-test</artifactId>
            <version>${spring.version}</version>
            <scope>provided</scope>
        </dependency>

        <!-- 關係型資料庫整合時需配置 如hibernate jpa等 -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-orm</artifactId>
            <version>${spring.version}</version>
        </dependency>

        <!-- hibernate -->
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-core</artifactId>
            <version>${hibernate.version}</version>
        </dependency>

        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-ehcache</artifactId>
            <version>${hibernate.version}</version>
        </dependency>

        <!-- struts2 -->
        <dependency>
            <groupId>org.apache.struts</groupId>
            <artifactId>struts2-core</artifactId>
            <version>${struts2.version}</version>

            <exclusions>
                <exclusion>
                    <!-- Hibernate已經還有該包的依賴 -->
                    <artifactId>javassist</artifactId>
                    <groupId>javassist</groupId>
                </exclusion>
            </exclusions>
        </dependency>

        <dependency>
            <groupId>org.apache.struts</groupId>
            <artifactId>struts2-spring-plugin</artifactId>
            <version>${struts2.version}</version>
        </dependency>

        <dependency>
            <groupId>org.apache.struts</groupId>
            <artifactId>struts2-convention-plugin</artifactId>
            <version>${struts2.version}</version>
        </dependency>

        <!-- log4j -->
        <dependency>
            <groupId>log4j</groupId>
            <artifactId>log4j</artifactId>
            <version>1.2.17</version>
        </dependency>

        <!-- mysql連線 -->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.34</version>
        </dependency>
        <!-- 資料來源 c3p0 -->
        <dependency>
            <groupId>com.mchange</groupId>
            <artifactId>c3p0</artifactId>
            <version>0.9.5-pre8</version>
        </dependency>
        <dependency>
            <groupId>com.mchange</groupId>
            <artifactId>mchange-commons-java</artifactId>
            <version>0.2.12</version>
        </dependency>

        <!-- json -->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>fastjson</artifactId>
            <version>1.2.3</version>
        </dependency>

        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-annotations</artifactId>
            <version>${jackson.version}</version>
        </dependency>

        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-core</artifactId>
            <version>${jackson.version}</version>
        </dependency>

        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-databind</artifactId>
            <version>${jackson.version}</version>
        </dependency>

        <!-- aop -->
        <dependency>
            <groupId>org.aspectj</groupId>
            <artifactId>aspectjweaver</artifactId>
            <version>1.8.4</version>
        </dependency>

        <!-- servlet -->
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>servlet-api</artifactId>
            <version>3.0-alpha-1</version>
            <scope>provided</scope>
        </dependency>

        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>jstl</artifactId>
            <version>1.2</version>
        </dependency>

    </dependencies>

    <build>
        <finalName>spring_struts2_Hibernate_demo</finalName>

        <plugins>
            <!-- Run the JUnit unit tests in an isolated classloader -->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>2.4.2</version>
                <configuration>
                    <skipTests>true</skipTests>
                </configuration>

            </plugin>

            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-war-plugin</artifactId>
                <version>2.3</version>
                <configuration>
                    <webXml>src/main/webapp/WEB-INF/web.xml</webXml>
                </configuration>
            </plugin>

            <!-- generate java doc -->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-javadoc-plugin</artifactId>
                <version>2.9.1</version>
                <configuration>
                    <javadocDirectory>target/javadoc</javadocDirectory>
                    <reportOutputDirectory>target/javadoc</reportOutputDirectory>
                    <charset>UTF-8</charset>
                    <encoding>UTF-8</encoding>
                    <docencoding>UTF-8</docencoding>
                    <show>private</show>
                </configuration>
            </plugin>

            <!-- 部署至本機 -->
            <plugin>
                <groupId>org.codehaus.cargo</groupId>
                <artifactId>cargo-maven2-plugin</artifactId>
                <version>1.0</version>
                <configuration>
                    <container>
                        <containerId>tomcat6x</containerId>
                        <home>F:\apache-tomcat-7.0.27</home>
                    </container>
                    <configuration>
                        <type>existing</type>
                        <home>F:\apache-tomcat-7.0.27</home>
                    </configuration>
                </configuration>
            </plugin>
        </plugins>

    </build>
</project>

整合細節知識點(瞭解):
Struts2 和 Spring整合:就是將action的建立交由Spring管理。配置檔案中class只需要配置Spring中bean的id就可以。

 <action name="userAction" class="SpringBeanId" />

Hibernate和Spring整合:
1、建立連線池的物件交由spring管理
2、Spring提供了SessionFactory的封裝類LocalSessionFactory,配置該類,並將資料來源配置給它。
3、配置Spring的HibernateTemplate,將SessionFactory配置給他。替代session完成資料操作。
4、spring 事務管理配置,替代Hibernate事務管理

相關文章