spring mvc 整合hibernate

1.导入所有spring包及AOP支持包
2.spring mvc 日志处理:commons.logging.jar
3.导入所有hibernate包(需要的)
4.hibernate日志处理:log4j.jar、
5.DBCP和commons.pool.jar
6.spring mvc JSTL包
7.数据库驱动和beans.xml,log.xml,jdbc.properties

1.web.xml

<servlet>
    <servlet-name>user</servlet-name>
       <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
      <load-on-startup>1</load-on-startup>
  </servlet>
  <servlet-mapping>
    <servlet-name>user</servlet-name>
    <url-pattern>/</url-pattern>
  </servlet-mapping>
  
   <!--2.siteMesh:sitemesh-->
  <filter>
    <filter-name>sitemesh</filter-name>
    <filter-class>com.opensymphony.sitemesh.webapp.SiteMeshFilter</filter-class>
  </filter>

  <filter-mapping>
    <filter-name>sitemesh</filter-name>
    <url-pattern>/*</url-pattern>
  </filter-mapping>
   <!--2.siteMesh:sitemesh-->
  <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>
    
    <!-- 创建spring的监听器 -->
    <listener>
        <listener-class>
            org.springframework.web.context.ContextLoaderListener
        </listener-class>
    </listener>
    <!-- spring的监听器可以通过这个上下文参数来获取beans.xml的位置 -->
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>
            classpath*:beans.xml
        </param-value>
    </context-param>
    <filter>
        <filter-name>openSessionInViewFilter</filter-name>
        <filter-class>
  org.springframework.orm.hibernate3.support.OpenSessionInViewFilter
        </filter-class>
    </filter>
    
    <filter-mapping>
    <filter-name>openSessionInViewFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>
    
    <filter>
        <filter-name>SystemContextFilter</filter-name>
        <filter-class>org.itat.web.SystemContextFilter</filter-class>
<!--         <init-param>
            <param-name>pageSize</param-name>
            <param-value>15</param-value>
        </init-param>-->
     </filter>

    <filter-mapping>
        <filter-name>SystemContextFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>
    
    <filter>
        <filter-name>LoginFilter</filter-name>
        <filter-class>org.itat.web.LoginFilter</filter-class>
     </filter>

    <filter-mapping>
        <filter-name>LoginFilter</filter-name>
        <url-pattern>/user/*</url-pattern>
    </filter-mapping>

2.user-servlet.xml
<context:component-scan base-package="org.itat.web"/>
    <mvc:annotation-driven/>
    <!-- 将静态文件指定到某个特殊文件夹中统一处理3.0.5之后支持 -->
    <mvc:resources location="/resources/" mapping="/resources/**"></mvc:resources>
    
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <!-- <property name="viewClass" 
            value="org.springframework.web.servlet.view.JstlView"/> -->
        <!-- JSTL支持,3.5之后默认支持 -->
        <property name="prefix" value="/WEB-INF/jsp/"/>
        <property name="suffix" value=".jsp"/>
    </bean>
    
    <bean class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver">
        <property name="exceptionMappings">
            <props>
                <prop key="org.itat.model.UserException">error</prop>
            </props>
        </property>
    </bean>
    
    <!-- 设置multipart才能完成文件上传 -->
    <!-- <bean id="multipartResolver"
    class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
        <property name="maxUploadSize" value="50000000"/>
    </bean> -->

3.bean.xml
<context:annotation-config/>
   <context:component-scan base-package="org.itat"/>
   
   <!-- 此处dbcp类路径发生变化,属于dbcp新版与老版兼容问题 -->
   <bean id="dataSource" class="org.apache.commons.dbcp2.BasicDataSource" destroy-method="close">
        <property name="driverClassName" value="${jdbc.driverClassName}"/>
        <property name="url" value="${jdbc.url}"/>
        <property name="username" value="${jdbc.username}"/>
        <property name="password" value="${jdbc.password}"/>
        <property name="initialSize" value="1"/>
        <property name="minIdle" value="1"/>
    </bean>
    
    <context:property-placeholder location="classpath:jdbc.properties"/>
    
    <bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
        <property name="dataSource" ref="dataSource"/>
        <property name="packagesToScan" >
            <value>org.itat.model</value>
        </property>
        <property name="hibernateProperties">
          <!-- <value>
            hibernate.dialect=org.hibernate.dialect.HSQLDialect
          </value> -->
          <props>
              <prop key="hibernate.dialect">org.hibernate.dialect.PostgreSQLDialect</prop>
            <prop key="show_sql">true</prop>
            <prop key="hibernate.hbm2ddl.auto">update</prop>
            <prop key="hibernate.format_sql">false</prop>
          </props>
        </property>
      </bean>
    <!-- 配置spring的事务处理 -->
    <!-- 1.创建事务管理器 -->
    <bean id="transactionManager"
                class="org.springframework.orm.hibernate3.HibernateTransactionManager">
        <property name="sessionFactory" ref="sessionFactory"/>
    </bean>
    <!-- 配置AOP,spring通过AOP来进行事务管理的 -->
      <aop:config>
      <!-- 设置pointcut表示那些方法要加事务处理 -->
      <!-- 以下事务声明在DAO中,但是通常都会在service处理多个业务对象逻辑的关系,诸如删除、更新等
      此时如果在执行一个步骤后抛出异常,就会导致数据不完整,所以事务不应该在DAO层处理,而应该是在service
      这也就是spring所提供的一个非常方便的工具:声明式事务 -->
            <aop:pointcut id="allMethods"
            expression="execution(* org.itat.service.*.*(..))"/>
        <!-- 通过advisor来确定具体要加入事务控制的方法 -->
        <aop:advisor advice-ref="txAdvice" pointcut-ref="allMethods"/>
      </aop:config>
    <!-- 配置哪些方法要加入事务控制 -->
      <tx:advice id="txAdvice" transaction-manager="transactionManager">
        <tx:attributes>
        <!-- 让所有的方法都加入事务管理,为了提高效率,可以把一些查询之类的方法设置为只读的事务 -->
            <tx:method name="*" propagation="REQUIRED" read-only="true" />
            <!-- 以下方法都是可能涉及修改的方法,就无法设置为只读 -->
              <tx:method name="add*" propagation="REQUIRED"/>
              <tx:method name="del*" propagation="REQUIRED"/> 
              <tx:method name="update*" propagation="REQUIRED"/> 
              <tx:method name="save*" propagation="REQUIRED"/>    
        </tx:attributes>
      </tx:advice>

      <bean id="hibernateTemplate" class="org.springframework.orm.hibernate3.HibernateTemplate">
          <property name="sessionFactory" ref="sessionFactory"/>
      </bean>

4.dao
@Resource
    public void setSuperSessionFactory(SessionFactory sessionFactory){
        this.setSessionFactory(sessionFactory);
    }


你可能感兴趣的:(spring mvc 整合hibernate)