Spring4+Hibernate4+SpringMVC的整合

關於SSH的整合我也是在網上各種找資料,研究了一整天才整合成功的,網上完整詳細的教程我到現在還沒找到過,都是各種博客裏面東拼西湊搞出來的。
首先要下載Spring4.x以及Hibernate4.x的jar包放到WEB-INF\lib文件夾裏面。
然後用myeclipse將項目變成hibernate工程,將生成的java文件以及*.cfg.xml文件刪除掉,再然後連接數據庫生成映射文件以及實體文件。
然後src下面新建一個config文件夾放入Spring和Springmvc的配置文件。如圖
接下來整合過程
新建一個applicationContext.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:mvc="http://www.springframework.org/schema/mvc"
    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/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd
        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd
        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.3.xsd"> 
        <!-- 屬性文件路徑 -->
         <context:property-placeholder location="classpath*:/config.properties" />
         <!-- 配置掃描註解包 -->
          <context:component-scan base-package="com.serviceimpl"></context:component-scan>
  <context:component-scan base-package="com.dao.impl"></context:component-scan>
   <context:component-scan base-package="com.controller"></context:component-scan>
   <context:component-scan base-package="com.interpoter"></context:component-scan>

        <!-- 配置jdbc數據源 dataSource-->
        <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">  
        <!-- 數據源驅動 -->
        <property name="driverClassName" value="com.mysql.jdbc.Driver"></property>         
         <!-- 數據源鏈接地址 -->
        <property name="url" value="jdbc:mysql://127.0.0.1:3306/healthy"></property>       
         <!-- 用戶名 -->
        <property name="username" value="root"></property>  
         <!-- 密碼 -->   
        <property name="password" value="961215"></property>  
    </bean>  

    <!-- 配置sessionFactory dao層裏面的-->
    <bean id="sessionFactory"
        class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
        <!-- 數據源 -->
        <property name="dataSource" ref="dataSource" />
        <!-- 配置實體類的映射文件路徑 -->
        <property name="mappingLocations" value="classpath:/com/model/*.hbm.xml"/>
        <!-- hibernate的相關屬性配置 -->
        <property name="hibernateProperties">
            <value>
            <!-- 由於hibernate的數據庫連接是配置在屬性文件裏面的,所以這裏還要配置一下jdbc連接(個人理解) -->     
            hibernate.connection.username=root
            hibernate.connection.password=961215
    hibernate.connection.url=jdbc:mysql://localhost:3306/healthy
                <!-- 設置數據庫方言 -->
        hibernate.dialect=org.hibernate.dialect.MySQLDialect
                <!-- 設置自動創建|更新|驗證數據庫表結構 -->
                hibernate.hbm2ddl.auto=update
                <!-- 是否在控制檯顯示sql -->
                hibernate.show_sql=true
                <!-- 是否格式化sql,優化顯示 -->
                hibernate.format_sql=true
                <!-- 是否開啓二級緩存 -->
                hibernate.cache.use_second_level_cache=false
                <!-- 是否開啓查詢緩存 -->
                hibernate.cache.use_query_cache=false
                <!-- 數據庫批量查詢最大數 -->
                hibernate.jdbc.fetch_size=50
                <!-- 數據庫批量更新、添加、刪除操作最大數 -->
                hibernate.jdbc.batch_size=50
                <!-- 是否自動提交事務 -->
                hibernate.connection.autocommit=true
                <!-- 指定hibernate在何時釋放JDBC連接 -->
                hibernate.connection.release_mode=auto
                <!-- 創建session方式 hibernate4.x 的方式 -->
                hibernate.current_session_context_class=org.springframework.orm.hibernate4.SpringSessionContext
                <!-- javax.persistence.validation.mode默認情況下是auto的,就是說如果不設置的話它是會自動去你的classpath下面找一個bean-validation**包 
                    所以把它設置爲none即可 -->
                javax.persistence.validation.mode=none
            </value>  
        </property>

        <!-- 自動掃描實體對象的包結構中存放實體類 -->
        <property name="packagesToScan" value="com.model" />
    </bean>
    <!-- 從這開始就是開啓事務的配置了 -->  
     <!-- hibernate交由spring託管 -->

    <bean id="txManager"  
                class="org.springframework.orm.hibernate4.HibernateTransactionManager">  
        <property name="sessionFactory" ref="sessionFactory"/>  

    </bean>  
    <!-- 配置通知 -->
    <tx:advice id="txAdvice" transaction-manager="txManager" >  
         <tx:attributes>
            <!-- 凡是以這些名字開頭的都通過請求,並且允許修改數據庫 -->
            <tx:method name="insert*" read-only="false" propagation="REQUIRED" />

            <tx:method name="find*" read-only="true" propagation="REQUIRED" />

            <tx:method name="save*" read-only="false" propagation="REQUIRED" />

            <tx:method name="update*" read-only="false" propagation="REQUIRED" />

            <tx:method name="delete*" read-only="false" propagation="REQUIRED" /> 
  </tx:attributes>
    </tx:advice>  
    <!-- 切面配置 -->
   <aop:config> <!-- 將項目的service層交給spring託管 -->
  <aop:pointcut id="bussinessService"   
   expression="execution(* com.service.*.*(..))" />
  <aop:advisor pointcut-ref="bussinessService"
   advice-ref="txAdvice" />

 </aop:config>
        </beans>

下面是SpringMVC的配置文件

<?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:mvc="http://www.springframework.org/schema/mvc"
    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/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd
        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd
        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.3.xsd">  
  <!-- 掃描註解包 -->
 <context:component-scan base-package="com.serviceimpl"></context:component-scan>
  <context:component-scan base-package="com.dao.impl"></context:component-scan>
   <context:component-scan base-package="com.controller"></context:component-scan>

  <!-- 視圖解析器 -->
  <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
   <property name="prefix" value="/jsp/" />  
   <property name="suffix" value=".jsp" /> 
  </bean>

<!-- 第一種靜態處理 處理css,js,photo -->
    <mvc:resources location="/photo/" mapping="/photo/**"></mvc:resources>
<!-- 第二種靜態處理 處理css,js,photo -->
    <mvc:default-servlet-handler/>
    <mvc:annotation-driven></mvc:annotation-driven><!-- 一般都要配置 -->
    <!-- 攔截器 -->

 <mvc:interceptors>
 <mvc:interceptor>
 <mvc:mapping path="/ToA"/> 
 <bean class="com.interpoter.Interptor"></bean>
 </mvc:interceptor>
  <mvc:interceptor>
 <mvc:mapping path="/**"/>  
 <bean class="com.interpoter.RegisterInterpoter"></bean>
 </mvc:interceptor>
 </mvc:interceptors>
 <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver"/> 

</beans>

最後是web文件配置

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
  <display-name>Healthy_3</display-name>
  <!-- spring配置文件applictioncontext路徑 -->
<context-param>  
        <param-name>contextConfigLocation</param-name>  
        <param-value>classpath:config/applicationContext.xml</param-value>  
  </context-param>  
  <!--   配置spring啓動listener入口 -->  
  <listener>  
     <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>  
  </listener>  
  <servlet>
  <!-- springMVC配置文件路徑 -->
  <servlet-name>Healthy_3</servlet-name>
  <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>  
            <param-name>contextConfigLocation</param-name>    
             <param-value>classpath:config/Healthy_3-servlet.xml</param-value>   
        </init-param> 
        <load-on-startup>1</load-on-startup>
  </servlet>
  <servlet-mapping>
  <servlet-name>Healthy_3</servlet-name>
  <url-pattern>/</url-pattern>
  </servlet-mapping>
  <filter>
  <filter-name>OpenSessionInViewFilter</filter-name>
  <filter-class>
   org.springframework.orm.hibernate4.support.OpenSessionInViewFilter
  </filter-class>
  <init-param>
   <param-name>FlushMode</param-name>
   <param-value>AUTO</param-value>
  </init-param>
 </filter>
  <filter-mapping>
        <filter-name>OpenSessionInViewFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>
   <!-- 字符集過濾器 -->
    <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>
    <filter-mapping>
        <filter-name>encodingFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>

</web-app>
    新人渣渣,還望有大神前來多多指教。
發佈了39 篇原創文章 · 獲贊 20 · 訪問量 5萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章