SpringMVC4+hibernate4學習筆記(一)=>配置詳解


SpringMVC4+hibernate4學習筆記(一)=>配置詳解

鑒於目前資料大多數都是基於spring3的配置,本人在最初搭建的時候遇到很多問題,由此記錄下來僅供參考

/* 2014-09-04 by feilengcui008@gmail.com */

使用的jar文件

springframework4.0.6(為了方便整個先導入)
hibernate4.3.6 /required/*下所有jar 以及 /optional下的c3p0(為了使用c3p0作為dataSource,使用其連接池)
jstl.jar standard.jar ----為了使用jstl標簽庫
apoalliance.jar ----在AOP一些地方依賴這個庫
commons-logging.jar

配置詳細步驟

  • 第一步,配置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" xmlns:web="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>app</display-name>
         
         <!-- context啟動時加載hibennate的配置文件 -->
         <context-param>
             <param-name>contextConfigLocation</param-name>
             <param-value>/WEB-INF/spring-*.xml</param-value>
         </context-param>
         
         <listener>
             <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
         </listener>
         
         <!-- 設置spring的前端分發器,接受請求 -->
         <servlet>
             <servlet-name>myservlet</servlet-name>
             <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
             <load-on-startup>1</load-on-startup>
        </servlet>
        <servlet-mapping>
            <servlet-name>myservlet</servlet-name>
            <url-pattern>/</url-pattern>
        </servlet-mapping>
        
        <!-- 默認錯誤頁面的設置 -->
        <error-page>
            <error-code>404</error-code>
            <location>/WEB-INF/jsp/404.jsp</location>
        </error-page>
    
        <!-- <error-page>
        <exception-type>java.lang.Exception</exception-type>
            <location>/WEB-INF/jsp/exception.jsp</location>
        </error-page> -->
    
     </web-app>
    
  • 第二步,myservlet-servlet.xml(DispatcherServlet)的配置文件相關部分,注意,如果在配置中用到了aop,tx,mvc等標簽,須在xmlns中導入。
      <?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:p="http://www.springframework.org/schema/p"
             xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:tx="http://www.springframework.org/schema/tx"
             xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context"
             xmlns:util="http://www.springframework.org/schema/util"
             xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
                                http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd 
                                http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd 
                                http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
                                http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd">
    
          <!-- 掃描注解配置的包 -->
          <context:component-scan base-package="com.tan.*" />
          
          <!-- 基於注釋的事務,當注釋中發現@Transactional時,使用id為“transactionManager”的事務管理器 -->
          <!-- 如果沒有設置transaction-manager的值,則spring以缺省默認的事務管理器來處理事務,默認事務管理器為第一個加載的事務管理器 -->
          <tx:annotation-driven transaction-manager="transactionManager" />
          
          <!-- 設置spring的mvc用注解 -->
          <mvc:annotation-driven />
          
          <!-- 設置handler的映射方式,前面注解是其中一種 -->
          <!-- HandlerMapping <bean class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping"/> 
          <bean class="org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter"/> -->
          
          <!-- 設置試圖的解析ViewResolver -->
          <bean
              class="org.springframework.web.servlet.view.InternalResourceViewResolver">
          <!-- <property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/> 此處不加這行似乎也能在jsp中用jstl,只要正確引入了tag-->
              <property name="prefix" value="/WEB-INF/jsp/" />
              <property name="suffix" value=".jsp" />
          </bean>
          <!-- 可以使用基於url的handlermapping <bean name="/hello" class="com.tan.controller.MyController"/> -->
      </beans>
    
  • 第三步,hibenate相關的配置,spring-hibernate.xml。配置數據源->交給sessionFactory->交給spring事物管理transactionManager->spring接手
      <?xml version="1.0" encoding="UTF-8"?>
      <beans xmlns="http://www.springframework.org/schema/beans"
             xmlns:context="http://www.springframework.org/schema/context"
             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:tx="http://www.springframework.org/schema/tx"
             xmlns:aop="http://www.springframework.org/schema/aop"
             xsi:schemaLocation="http://www.springframework.org/schema/beans     
                                 http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
                                 http://www.springframework.org/schema/context
                                 http://www.springframework.org/schema/context/spring-context-4.0.xsd
                                 http://www.springframework.org/schema/aop
                                 http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
                                 http://www.springframework.org/schema/tx
                                 http://www.springframework.org/schema/tx/spring-tx-4.0.xsd">
    
      <!-- Hibernate4 -->
      <!-- shiyongproperties文件保存jdbs以及hibernate的相關變量,在具體配置處使用屬性zhi值,必須在Spring配置文件的最前面加載,放在src目錄 -->
      <context:property-placeholder location="classpath:persistence-mysql.properties" />
    
      <!-- 獲取數據源的幾種方式DriverManagerDataSource、dbcp、c3p0,后兩種支持連接池 -->
      <!-- class="org.apache.tomcat.dbcp.dbcp.BasicDataSource" 有連接池作用 -->
      <!-- class="org.springframework.jdbc.datasource.DriverManagerDataSource"無連接池作用 -->
      <!-- <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> 
               <property name="driverClassName" value="${jdbc.driverClassName}" /> 
               <property name="url" value="${jdbc.url}" /> 
               <property name="username" value="${jdbc.user}" /> 
               <property name="password" value="${jdbc.pass}" /> 
           </bean> -->
    
      <!-- c3p0 有連接池作用,使用properties文件下的屬性值,也可以直接填-->
      <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
          <property name="driverClass" value="${jdbc.driverClassName}" />
          <property name="jdbcUrl" value="${jdbc.url}" />
          <property name="user" value="${jdbc.user}" />
          <property name="password" value="${jdbc.pass}" />
          <property name="minPoolSize" value="2" />
          <property name="maxPoolSize" value="50" />
          <property name="initialPoolSize" value="10" />
          <property name="maxIdleTime" value="60" />
          <property name="acquireIncrement" value="2" />
      </bean>
      
      <!-- 配置sessionFactory,統一管理一個數據庫的連接 -->
      <bean id="sessionFactory"
          class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
          <property name="dataSource" ref="dataSource" />
          <property name="packagesToScan">
      	    <list>
      		    <!-- 可以加多個包,需要hibenate映射的類的包 -->
      		    <value>com.tan.model</value>
      	    </list>
          </property>
          <property name="hibernateProperties">
      	    <props>
      		    <prop key="hibernate.hbm2ddl.auto">${hibernate.hbm2ddl.auto}</prop>
      		    <prop key="hibernate.dialect">${hibernate.dialect}</prop>
      		    <prop key="hibernate.show_sql">${hibernate.show_sql}</prop>
      		    <!-- <prop key="hibernate.current_session_context_class">thread</prop> -->
      	    </props>
          </property>
      </bean>
    
      <!-- 配置Hibernate事務管理器 -->
      <bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
          <property name="sessionFactory" ref="sessionFactory" />
      </bean>
    
      <!-- 配置事務異常封裝 -->
      <bean id="persistenceExceptionTranslationPostProcessor" class="org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor" />
      </beans>
    
  • 第四步,添加persistence-mysql.properties文件,如果在spring-hibenate.xml直接配置數據源的值,就不需要
      # jdbc.X
      jdbc.driverClassName=com.mysql.jdbc.Driver
      jdbc.url=jdbc:mysql://localhost:3306/springmvc(你的數據庫名)?createDatabaseIfNotExist=true&amp;useUnicode=true&amp;characterEncoding=utf8
      jdbc.user=root
      jdbc.pass=
      # hibernate.X
      hibernate.connection.driverClass=com.mysql.jdbc.Driver
      hibernate.connection.url=jdbc:mysql://localhost:3306/springmvc(你的數據庫名)
      hibernate.dialect=org.hibernate.dialect.MySQL5Dialect
      hibernate.connection.username=root
      hibernate.connection.password=
      hibernate.show_sql=true
      hibernate.hbm2ddl.auto=update #如果沒有表則創建,如果表結構更改則自動更新


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM