Spring + Spring MVC+Hibernate框架整合詳細(xì)配置

具體配置參數(shù):

Spring: spring-framework-4.2.2
Hibernate: hibernate-release-4.2.21.Final
Eclipse : eclipse MARS.2
MySQL : mysql 5.5 +Navicat Premiumd視圖器
System: win 8.1

Spring-framework下載(附地址)

需要下載的文件前面兩個(gè)就夠了,包和參考文檔

  • spring-framework-4.2.2.RELEASE-dist.zip 包

  • spring-framework-4.2.2.RELEASE-docs.zip 文檔

  • spring-framework-4.2.2.RELEASE-schema.zip 配置

導(dǎo)入SSH框架整合所需的jar包

  • Spring的jar包
  • Hibernate(附地址)的jar包
  • 第三方j(luò)ar包(日志包)
  • 數(shù)據(jù)庫(kù)的jar包

配置web.xml

手動(dòng)創(chuàng)建一個(gè)config文件夾用與存放配置的文件,這里方便說(shuō)明記為cf

<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath:xx.xml</param-value>
</context-param>
<listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

其中xx.xml文件是需要你在cf中手動(dòng)創(chuàng)建的配置文件,里面具體內(nèi)容后面配置,這里相當(dāng)于是告訴系統(tǒng)文件在哪,這里為了說(shuō)明方便命名為spring.xml

     <servlet>
        <servlet-name>dispatcherServlet</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath:xxx.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>dispatcherServlet</servlet-name>
        <url-pattern>/*</url-pattern>
    </servlet-mapping>

這里的xxx.xml同上,可自己命名,這里命名為springMVC.xml

其中filter-class里的名字不用記,可以通過(guò)ctrl+shift+T輸入
CharacterEncodingFilter獲得,且這一步需放在所有過(guò)濾器最前面,才有效果

    <filter>
    <filter-name>characterEncodingFilter</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>characterEncodingFilter</filter-name>
    <url-pattern>/*</url-pattern>
</filter-mapping>

其中filter-name里的類名可以通過(guò)ctrl+shift+T輸入HiddenHttpMethodFilter獲得,由于瀏覽器form表單只支持GET與POST請(qǐng)求,而DELETE、PUT等method并不支持,Spring3.0添加了一個(gè)過(guò)濾器,可以將這些請(qǐng)求轉(zhuǎn)換為標(biāo)準(zhǔn)的http方法,使得支持GET、POST、PUT與DELETE請(qǐng)求,該過(guò)濾器為HiddenHttpMethodFilter

  <filter>
      <filter-name>hiddenHttpMethodFilter</filter-name>
    <filter-class>org.springframework.web.filter.HiddenHttpMethodFilter</filter-class>
   </filter>
  <filter-mapping>
    <filter-name>hiddenHttpMethodFilter</filter-name>
    <url-pattern>/*</url-pattern>
  </filter-mapping>
  • 配置SpringMVC

  • 導(dǎo)入命名空間
    打開我們自己定義的配置文件springMVC.xml選擇Namespace,勾選context和MVC

  • 添加組成掃描,包含過(guò)濾注解

          <context:component-scan base-package="com.jikexueyuancrm"
             use-default-filters="false">
             <context:include-filter type="annotation"
                 expression="org.springframework.stereotype.Controller" />
             <context:include-filter type="annotation"
           expression="org.springframework.web.bind.annotation.ControllerAdvice" />
         </context:component-scan>
    
  • 添加視圖解析器

    其中class類名可以通過(guò)ctrl+shift+T輸入InternalResourceViewResolver獲得

           <bean
               class="org.springframework.web.servlet.view.InternalResourceViewResolver">
             <property name="prefix" value="/"></property>
             <property name="suffix" value=".jsp"></property>
         </bean> 
    
  • 配置靜態(tài)資源

         <mvc:default-servlet-handler />
    
  • 配置注解

        <mvc:annotation-driven />
    
  • 配置Spring

    這里用到cp30的包,C3P0是一個(gè)開源的JDBC連接池,它實(shí)現(xiàn)了數(shù)據(jù)源和JNDI綁定,支持JDBC3規(guī)范和JDBC2的標(biāo)準(zhǔn)擴(kuò)展。

      <bean class="com.mchange.v2.c3p0.ComboPooledDataSource" id="dataSource">
      <property name="user" value="${jdbc.user}"></property>
      <property name="password" value="${jdbc.passowrd}"></property>
      <property name="driverClass" value="${jdbc.driverClass}"></property>
      <property name="jdbcUrl" value="${jdbc.jdbcUrl}"></property>
       </bean>
    

    需要orm包-Object Relational Mapping 對(duì)象關(guān)系映射,POJO(Plain Ordinary Java Object)簡(jiǎn)單的Java對(duì)象,實(shí)際就是普通JavaBeans

        <bean class="org.springframework.orm.hibernate4.LocalSessionFactoryBean"
      id="sessionFactory">
      <!-- 配置數(shù)據(jù)源 -->
      <property name="dataSource" ref="dataSource"></property>
      <!-- 找到實(shí)體包(pojo) -->
      <property name="namingStrategy">
          <bean class="org.hibernate.cfg.ImprovedNamingStrategy"></bean>
      </property>
      <property name="packagesToScan" value="com.jianlam.entity"></property>
    
    • 配置hibernate常用屬性

      <property name="hibernateProperties">
          <props>
              <!-- 數(shù)據(jù)庫(kù)的方言 -->
              <prop key="hibernate.dialect">org.hibernate.dialect.MySQLInnoDBDialect</prop>
              <prop key="hibernate.show_sql">true</prop>
              <prop key="hibernate.format_sql">true</prop>
              <prop key="hibernate.hbm2ddl.auto">update</prop>
          </props>
      </property>
       </bean>
      
    • 配置hibernate事物管理器

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

測(cè)試

創(chuàng)建好實(shí)體類和數(shù)據(jù)庫(kù)表,查看連接操作數(shù)據(jù)庫(kù)是否能正常運(yùn)作,若沒有問題則配置成功!

     private spring ctx = null;

    @Test
    public void testDataSource() throws SQLException {
        //檢查spring配置
        ctx = new ClassPathXmlApplicationContext("spring.xml");
//          System.out.println(ctx);
                //檢查數(shù)據(jù)庫(kù)連接
        DataSource dataSource = ctx.getBean(DataSource.class);

//         System.out.println(dataSource.getConnection().toString());
       //檢查hibernate配置
        SessionFactory sessionFactory = ctx.getBean(SessionFactory.class);
        System.out.println(sessionFactory);

        Session session = sessionFactory.openSession();
        Transaction tx = session.beginTransaction();
        //測(cè)試數(shù)據(jù)庫(kù)
        Users  user = new Users  ("jianlam", "123456");
        session.save(user);
        tx.commit();
        session.close();

    }

如果你的mysql有問題或?qū)τ贛VC模式下所需的包分類有所困惑可參考:這里

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容