1. 程式人生 > >springmvc-spring4.3-hibernate5框架整合

springmvc-spring4.3-hibernate5框架整合

                                                             springmvc-spring4.3-hibernate5三大框架整合

       為了跟上時代的潮流,我決定用hibernate5,同時也複習一下以前所學的知識,這整合也費了我一番功夫,所以我決定將這個專案的主要部分記錄下來,一方面做為以後的參考,一方面分享給廣大的java學習愛好者 
              1、首先是properties檔案和xml檔案
       

       2、其次是引入jar包,這部分在我上傳的工程裡可以看到,所以先不截圖了

              3、根據mvc架構建幾個包,寫好對應的類
                             這裡我簡單的展示一下xml檔案是怎麼配置的吧
      1). spring-mvc-hibernate.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:util="http://www.springframework.org/schema/util"
xmlns:tx="http://www.springframework.org/schema/tx" xmlns:task="http://www.springframework.org/schema/task"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-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
           http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd
           http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-4.3.xsd
           http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.3.xsd"
default-autowire="byName" default-lazy-init="false">


<!-- 自動掃描dao和service包(自動注入) -->
<context:component-scan base-package="com.ssh.dao" />
<context:component-scan base-package="com.ssh.service" />
<context:component-scan base-package="com.ssh.daoImpl" />
<context:component-scan base-package="com.ssh.serviceImpl" />
<!-- 載入service,此時要排除要controller,因為controller已經spring-mvc中載入過了 -->
<context:component-scan base-package="com.ssh.controller">
<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller" />
</context:component-scan>


<!-- 引入屬性檔案 -->
<context:property-placeholder location="classpath:config/properties/dbconfig.properties" />


<!-- 配置資料來源1 -->
<bean name="dataSource"  class="org.apache.commons.dbcp.BasicDataSource"  
destroy-method="close">
<property name="driverClassName" value="${diver_name}"/>
<property name="url" value="${jdbc.url}" />
<property name="username" value="${jdbc.username}" />
<property name="password" value="${jdbc.password}" />
<property name="maxActive" value="100" />
<property name="maxIdle" value="30" />
<property name="maxWait" value="500" />
<property name="defaultAutoCommit" value="true" />
<property name="timeBetweenEvictionRunsMillis" value="10000" />
<property name="minEvictableIdleTimeMillis" value="10000" />
<property name="validationQuery" value="select 1" />
<property name="testOnBorrow" value="true" />
<property name="testOnReturn" value="true" />
<property name="testWhileIdle" value="true" />
</bean>   
<bean id="sessionFactory"
class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
   <property name="hibernateProperties"> 
<props>
                <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>  
                <prop key="hibernate.show_sql">true</prop>  
                <prop key="hibernate.format_sql">true</prop>  
                <prop key="hibernate.hbm2ddl.auto">update</prop>   
</props>
</property>
<!-- 註解方式配置 -->
<property name="packagesToScan">
<list>
<value>com.ssh.entity</value>
</list>
</property>
</bean>


<bean id="transactionManager" class="org.springframework.orm.hibernate5.HibernateTransactionManager">
         <property name="sessionFactory" ref="sessionFactory" /> 
    </bean>
<tx:annotation-driven transaction-manager="transactionManager" />  
    
    <!-- 配置事務異常封裝 -->
    <bean id="persistenceExceptionTranslationPostProcessor"
        class="org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor" />
  
 <aop:config>
  <!-- 通過aop定義事務增強切面 -->
 <aop:pointcut expression="execution(* com.ssh.serviceImpl.*.*(..))" id="pointcut" /> 
 <aop:advisor advice-ref="advice" pointcut-ref="pointcut" /> 
 </aop:config>
 <aop:aspectj-autoproxy/>
 <tx:advice id="advice" transaction-manager="transactionManager">
 <tx:attributes>
     <tx:method name="query*" read-only="true" propagation="REQUIRED" />
              <tx:method name="find*" read-only="true" propagation="REQUIRED" />
              <tx:method name="add*"  propagation="REQUIRED" />
              <tx:method name="save*"  propagation="REQUIRED" />
              <tx:method name="delete*"  propagation="REQUIRED" />
              <tx:method name="update*"  propagation="REQUIRED" />
             <tx:method name="*"  propagation="REQUIRED" />
     </tx:attributes>
 </tx:advice>
</beans>

2). spring-mvc-hibernate.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
      http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
       http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context-4.3.xsd
         http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd 
http://cxf.apache.org/jaxws 
    http://cxf.apache.org/schemas/jaxws-4.3.xsd">

<!-- 自動掃描controller包下的所有類,使其認為spring mvc的控制器 -->
<!-- 載入controller的時候,不載入service,因為此時事物並未生效,若此時載入了service,那麼事物無法對service進行攔截 -->
<context:component-scan base-package="com.ssh.controller">
<context:exclude-filter type="annotation"
expression="org.springframework.stereotype.Service" />
</context:component-scan>
    <mvc:annotation-driven/>
    <mvc:default-servlet-handler/>

<!-- 預設的檢視解析器 在上邊的解析錯誤時使用 (預設使用html)- -->
<bean id="defaultViewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="viewClass"
value="org.springframework.web.servlet.view.JstlView" />
<property name="contentType" value="text/html" />
<property name="prefix" value="/WEB-INF/page/" />
<property name="suffix" value=".jsp" />
</bean>

<!-- 避免IE執行AJAX時,返回JSON出現下載檔案 -->
<!-- 設定返回json的字元編碼 -->
<bean id="mappingJacksonHttpMessageConverter"
 class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
<property name="supportedMediaTypes">
  <list>
  <value>text/html;charset=UTF-8</value>
  </list>
  </property>
</bean> 

<!-- 啟動Spring MVC的註解功能,完成請求和註解POJO的對映, 配置一個基於註解的定製的WebBindingInitializer,解決日期轉換問題,方法級別的處理器對映 -->
<bean
class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter" >
<property name="cacheSeconds" value="0" />
<property name="messageConverters">
<list >
  <ref bean="mappingJacksonHttpMessageConverter" />
  </list>
</property>
</bean>

    <!-- 上傳  -->
<bean class="org.springframework.web.multipart.commons.CommonsMultipartResolver"  
            id="multipartResolver">  
        <property name="resolveLazily" value="true" />  
        <property name="maxUploadSize" value="20971520" />  
        <property name="defaultEncoding" value="utf-8" />       
    </bean>  
</beans>

3). web.xml

<?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>SSHProject</display-name>
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>

  <!-- 載入所有的配置檔案 -->  
  <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath*:config/xml/spring-*.xml</param-value>  
  </context-param>
  <context-param>
<param-name>log4jConfigLocation</param-name>
<param-value>classpath:config/properties/log4j.properties</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>
</listener>
  <!-- 配置Spring監聽 -->  
  <listener>  
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>  
  </listener>  
  <!-- 配置SpringMVC -->  
  <servlet>  
    <servlet-name>springMVC</servlet-name>  
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>  
    <init-param>  
        <param-name>contextConfigLocation</param-name>  
        <param-value>classpath*:config/xml/spring-mvc.xml</param-value>  
    </init-param>  
    <load-on-startup>1</load-on-startup>  
  </servlet>  
  <servlet-mapping>
    <servlet-name>springMVC</servlet-name>
    <url-pattern>/</url-pattern>
  </servlet-mapping>
  <servlet-mapping>
    <servlet-name>springMVC</servlet-name>
    <url-pattern>*.do</url-pattern>
  </servlet-mapping>
   <servlet-mapping>
    <servlet-name>springMVC</servlet-name>
    <url-pattern>*.shtml</url-pattern>
  </servlet-mapping>
  <servlet-mapping>
    <servlet-name>springMVC</servlet-name>
    <url-pattern>*.jpg</url-pattern>
  </servlet-mapping>
  <servlet-mapping>
    <servlet-name>springMVC</servlet-name>
    <url-pattern>*.png</url-pattern>
  </servlet-mapping>
  <servlet-mapping>
    <servlet-name>springMVC</servlet-name>
    <url-pattern>*.gif</url-pattern>
  </servlet-mapping>
  <servlet-mapping>
    <servlet-name>springMVC</servlet-name>
    <url-pattern>*.js</url-pattern>
  </servlet-mapping>
  <servlet-mapping>
    <servlet-name>springMVC</servlet-name>
    <url-pattern>*.css</url-pattern>
  </servlet-mapping>  
  <error-page>
    <error-code>404</error-code>
    <location>/page/common/404.jsp</location>
  </error-page> 
  
  <!-- 配置字符集 -->  
  <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>  
    <init-param>  
        <param-name>forceEncoding</param-name>  
        <param-value>true</param-value>  
    </init-param>  
  </filter>  
  <filter-mapping>  
    <filter-name>encodingFilter</filter-name>  
    <url-pattern>/*</url-pattern>  
  </filter-mapping> 
  
  <!-- 配置Session -->  
  <filter>  
    <filter-name>openSession</filter-name>  
    <filter-class>org.springframework.orm.hibernate5.support.OpenSessionInViewFilter</filter-class>  
    <init-param>
<param-name>singleSession</param-name>
<param-value>true</param-value>
</init-param>
  </filter>  
  <filter-mapping>  
    <filter-name>openSession</filter-name>  
    <url-pattern>/*</url-pattern>  
  </filter-mapping>   
</web-app>

以上就是主要xml檔案的詳細配置,我搭的這個框架主要實現了ajax,hibernate註解生成表,靜態資源攔截功能,最重要的是這個框架真正實現了getcurrentSession事務管理功能,這是我搭這個框架所要達到的最終目的,就是用hibernate5來管理事務,哈哈,終於跟上時代的潮流了!!!