spring mvc高级篇(一):Spring+SpringMVC+Hibernate整合

来源:互联网 发布:php选择题答题系统 编辑:程序博客网 时间:2024/05/21 17:33

1.1整合思路分析

Java Web应用开发经过多年的发展,已经形成了一套成熟的程序结构。一个典型的使用了SpringMVC和Hibernate框架的应用,其结构如下图所示。


SpringMVC+Hibernate应用的程序结构

       SpringMVC的主控制器DispatcherServlet接到请求后会调用特定的Controller。在Controller中又会调用业务类(Service)来执行业务逻辑。如果需要访问数据库,业务类则会继续调用数据库访问对象(DAO)。而在数据库访问对象中,则需要调用SessionFactory提供的Session实例的方法执行具体操作。Session最终会通过Connection等JDBC API来实现增删该查等操作,而Connction可以通过配置的数据源(DataSource)来提供。

       通过以上分析不难看出,程序执行过程中依赖的方向是Controller--->Service--->DAO--->Session(由SessionFactory提供)--->Connection(由DataSource提供),当使用Spring IOC进行依赖管理时,依赖注入的方向则正好与相反。下面通过对核心配置文件的讲解及功能点的展示来讲解Spring+SpringMVC+Hibernate框架的搭建过程。

 

1.2 开发环境

开发工具:myeclipse8.6.1

数据库:mysql5.5.23

服务器: tomcat6.0.37

框架版本: spring3.2.2+hibernate3.3.2

 

1.3 案例开发步骤

步骤一:在myeclipse8.6中新建web工程SHMvc,拷贝如下包到lib目下:

 

代码的目录结构如下图所示:


 

步骤二:编写web.xml 配置文件,代码如下:

=================================web.xml========================

<?xmlversion="1.0"encoding="UTF-8"?>

<web-appversion="2.5"

    xmlns="http://java.sun.com/xml/ns/javaee"

    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee

    http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">

  <!-- 配置统一错误处理页面start -->

  <error-page>

    <error-code>404</error-code>

    <location>/Err404.html</location>

  </error-page>

  <error-page>

    <error-code>500</error-code>

    <location>/Err500.html</location>

  </error-page>

  <!-- 配置统一错误处理页面end -->

  <!-- 配置登录验证过滤器start -->

  <filter>

    <filter-name>loginFilter</filter-name>

    <filter-class>com.wx.filters.LoginFilter</filter-class>

    <!-- 不做拦截的请求-->

    <init-param>

         <param-name>exclusions</param-name>

         <param-value>SHMvc/,.js,.gif,.jpg,.jpeg,.png,.css,.ico,

        Login.jsp,Login.php,Register.jsp,register.php,CheckUser.php,

         ShowOneUser.jsp,ShowResult.jsp,Trans.php,ShowOneUser.php

         </param-value>

     </init-param>

  </filter>

  <filter-mapping>

    <filter-name>loginFilter</filter-name>

    <url-pattern>/*</url-pattern>

  </filter-mapping>

  <!-- 配置登录验证过滤器end -->

 

  <!--druid连接池监控  config start -->

  <filter>

     <filter-name>DruidWebStatFilter</filter-name>

     <filter-class>com.alibaba.druid.support.http.WebStatFilter</filter-class>

     <init-param>

         <param-name>exclusions</param-name>

         <param-value>*.js,*.gif,*.jpg,*.png,*.css,*.ico,/druid/*</param-value>

     </init-param>

  </filter>

  <filter-mapping>

     <filter-name>DruidWebStatFilter</filter-name>

     <url-pattern>/*</url-pattern>

  </filter-mapping>

  <servlet>

        <servlet-name>DruidStatView</servlet-name>

        <servlet-class>com.alibaba.druid.support.http.StatViewServlet</servlet-class>

  </servlet>

  <servlet-mapping>

        <servlet-name>DruidStatView</servlet-name>

        <url-pattern>/druid/*</url-pattern>

  </servlet-mapping>

  <!--http://localhost:8080/SHMvc/druid/index.html    end-->

  <!-- 解决hibernate延迟加载问题的处理 start -->

  <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>*.php</url-pattern>

  </filter-mapping>

  <!-- 解决hibernate延迟加载问题的处理 end -->

 

  <!-- 配置spring mvc的字符集过滤  start-->

  <filter>

    <filter-name>encode</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>encode</filter-name>

    <url-pattern>/*</url-pattern>

  </filter-mapping>

  <!-- 配置spring mvc的字符集过滤  end-->

 

  <!--配置 springmvc前端控制器  start-->

  <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:configs/mvc/springmvc-dispatcher.xml</param-value>

    </init-param>

    <load-on-startup>1</load-on-startup>

  </servlet>

 

  <servlet-mapping>

    <servlet-name>springmvc</servlet-name>

    <url-pattern>*.php</url-pattern>

  </servlet-mapping>

  <!--配置 springmvc前端控制器  end-->

 

  <!-- 加载log4jstart -->

  <context-param>

    <param-name>log4jConfigLocation</param-name>

    <param-value>classpath:configs/log4j.properties</param-value>

  </context-param>

  <listener>

    <listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>

  </listener>

  <!-- 加载log4j end-->

 

  <!-- 加载spring容器 start-->

  <context-param>

    <param-name>contextConfigLocation</param-name>

    <param-value>classpath:configs/applicationContext.xml</param-value>

  </context-param>

  <listener>

    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>

  </listener>

  <!-- 加载spring容器 end-->

 

  <welcome-file-list>

    <welcome-file>Login.jsp</welcome-file>

  </welcome-file-list>

</web-app>

================================================================

     web.xml是我们web工程的核心配置所在。在这里,我们配置了统一错误处理、登录验证过滤器、druid连接池监控、解决hibernate延迟加载问题的过滤器、spring mvc的字符集过滤、spring mvc前端控制器、加载log4j、加载spring容器等相关配置。相关代码请看web.xml的注释部分。

重点知识讲解:(1) Spring需要启动容器才能为其他框架提供服务,由于Web应用程序的人口是被Web服务器控制的,所以,无法在main()方法中通过创建ClassPallXmIApplicationContext对象来启动Spring容器。Spring提供了一个监听器类org.springframework.web.context.ContextLoaderListener来解决这个问题。该监听器实现了ServletContextListener接口,可以在Web容器启动的时候初始化Spring容器。当然,前提是需要在web.xml中配置好这个监听器。具体请参见上面配置代码及相关的注释部分。

  (2) Spring为我们提供了一个名为OpenSessionInViewFilter的过滤器,可以和前面提到的事务管理器及HibernateDaoSupport很好地配合。其作用是把一个Hibernate Session和一次完整的请求过程相绑定,在请求开始时开启Session,请求结束时关闭Session。这使得在一次请求的完整周期中,所使用的HibernateSession是唯一的且一直保持开启的可用状态,比较简便地解决了诸如延迟加载等问题。具体请参见上面的配置代码及相关的注释部分。

 

步骤三:建立hibernate二级缓存的配置文件,如下所示:

=========================echache.xml==============================

<ehcache>

    <diskStorepath="java.io.tmpdir"/>

 

    <defaultCache

       maxElementsInMemory="100"

       eternal="false"

       timeToIdleSeconds="120"

       timeToLiveSeconds="120"

       overflowToDisk="true"

       />

</ehcache>

=====================================================================

 

步骤四:建立hibernate核心配置文件,如下所示:

======================hibernate.cfg.xml==============================

<!DOCTYPEhibernate-configuration PUBLIC

    "-//Hibernate/Hibernate Configuration DTD 3.0//EN"

    "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">

<hibernate-configuration>

<session-factory>

    <!-- 配置数据库方言(即数据库的类型) -->

    <propertyname="dialect">

       org.hibernate.dialect.MySQLDialect

    </property>

    <!-- 把会话绑定到线程上 -->

    <propertyname="current_session_context_class">thread</property>

    <!-- 是否展示sql -->

    <propertyname="show_sql">true</property>

    <!-- 是否格式化sql -->

    <propertyname="format_sql">true</property>

    <!-- 二级缓存的提供类 -->

    <propertyname="hibernate.cache.provider_class">

       org.hibernate.cache.EhCacheProvider

    </property>

    <!-- 二级缓存配置文件的路径--> 

    <propertyname="hibernate.cache.provider_configuration_file_resource_path">configs/ehcache.xml</property>

    <!-- 是否开启二级缓存 -->

    <propertyname="hibernate.cache.use_second_level_cache">true</property>

    <!-- 是否开启查询缓存 -->

    <propertyname="hibernate.cache.use_query_cache">true</property>

</session-factory>

</hibernate-configuration>

=====================================================================

 

步骤五:建立druid数据源的属性文件,如下所示:

========================= druidConfig.properties=====================

driverClassName=com.mysql.jdbc.Driver

url=jdbc\:mysql\://localhost\:3306/schooldb

username=root

password=123456

#配置监控统计拦截的filters,去掉后监控界面sql无法统计

filters=stat

#配置初始化大小

initialSize=6

#配置初始化最大连接数

maxActive=20

#配置最小空闲连接数

minIdle=3

#配置获取连接等待超时的时间,1分钟

maxWait=60000

#检测连接是否有效的SQL

validationQuery=SELECT 'x'

#申请连接的时候检测,如果空闲时间大于timeBetweenEvictionRunsMillis,执行validationQuery检测连接是否有效

testWhileIdle=true

#申请连接时执行validationQuery检测连接是否有效,做了这个配置会降低性能

testOnBorrow=false

#归还连接时执行validationQuery检测连接是否有效,做了这个配置会降低性能

testOnReturn=false

#启用PSCache,必须配置大于0,当大于0时,poolPreparedStatements自动触发修改为true

maxPoolPreparedStatementPerConnectionSize=20

#对于长时间不使用的连接强制关闭

removeAbandoned=true

#超过30秒的空闲连接就可以被关闭了,单位是秒

removeAbandonedTimeout=30

#配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒

timeBetweenEvictionRunsMillis=10000

#配置一个连接在池中最小生存的时间,单位是毫秒

minEvictableIdleTimeMillis=30000

=====================================================================

 

步骤六:建立log4j的属性文件,如下所示:

========================= log4j.properties=====================

log4j.rootLogger=INFO, Console

 

#Console

log4j.appender.Console=org.apache.log4j.ConsoleAppender

log4j.appender.Console.layout=org.apache.log4j.PatternLayout

log4j.appender.Console.layout.ConversionPattern=%d[%t] %-5p [%c] %m%n

=====================================================================

 

步骤七:建立spring的核心配置文件,如下所示:

========================= applicationContext.xml=====================

<?xmlversion="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:aop="http://www.springframework.org/schema/aop"

    xmlns:tx="http://www.springframework.org/schema/tx"

    xsi:schemaLocation="http://www.springframework.org/schema/beans

     http://www.springframework.org/schema/beans/spring-beans-3.0.xsd

     http://www.springframework.org/schema/aop

     http://www.springframework.org/schema/aop/spring-aop-3.0.xsd

     http://www.springframework.org/schema/tx

     http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">

   

    <!-- 导入dao层和service层的bean配置 -->

    <importresource="classpath:configs/spring/applicationContext-dao.xml"/>

    <importresource="classpath:configs/spring/applicationContext-service.xml"/>

   

    <!-- 加载druid数据源的属性文件 -->

    <beanid="config"class="org.springframework.beans.factory.config.PreferencesPlaceholderConfigurer">

       <propertyname="location">

           <value>classpath:configs/druidConfig.properties</value>

       </property>

    </bean>

   

    <!-- druid数据源的配置 -->

    <beanid="druidDataSource"class="com.alibaba.druid.pool.DruidDataSource"

        init-method="init"destroy-method="close">

       <propertyname="driverClassName"value="${driverClassName}"/>

       <propertyname="url"value="${url}"/>

       <propertyname="username"value="${username}"/>

       <propertyname="password"value="${password}"/>

       <propertyname="filters"value="${filters}"/>

       <propertyname="initialSize"value="${initialSize}"/>

       <propertyname="maxActive"value="${maxActive}"/>

       <propertyname="minIdle"value="${minIdle}"/>

       <propertyname="maxWait"value="${maxWait}"/>

       <propertyname="validationQuery"value="${validationQuery}"/>

        <propertyname="testWhileIdle"value="${testWhileIdle}"/>

       <propertyname="testOnBorrow"value="${testOnBorrow}"/>

       <propertyname="testOnReturn"value="${testOnReturn}"/>

       <propertyname="maxPoolPreparedStatementPerConnectionSize"value="${maxPoolPreparedStatementPerConnectionSize}"/>

       <propertyname="removeAbandoned"value="${removeAbandoned}"/>

       <propertyname="removeAbandonedTimeout"value="${removeAbandonedTimeout}"/>

       <propertyname="timeBetweenEvictionRunsMillis"value="${timeBetweenEvictionRunsMillis}"/>

       <propertyname="minEvictableIdleTimeMillis"value="${minEvictableIdleTimeMillis}"/>

    </bean>

   

    <!-- 配置sessionFactory,即在这里整合hibernate-->

    <beanid="sessionFactory"class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">

       <propertyname="dataSource"ref="druidDataSource"></property>

       <propertyname="configLocation">

           <value>classpath:configs/hibernate.cfg.xml</value>

       </property>

       <propertyname="mappingLocations">

           <list>

              <value>classpath:com/wx/entitys/*.hbm.xml</value>

           </list>

       </property>

    </bean>

    <!-- 配置sessionFactory end-->

   

    <!-- 统一事务处理的配置start -->

    <beanid="txManager"class="org.springframework.orm.hibernate3.HibernateTransactionManager">

       <propertyname="sessionFactory"ref="sessionFactory"></property>

    </bean>

    <tx:adviceid="txAdvise"transaction-manager="txManager">

       <tx:attributes>

           <tx:methodname="find*"read-only="true"/>

           <tx:methodname="search*"read-only="true"/>

           <tx:methodname="query*"read-only="true"/>

           <tx:methodname="get*"read-only="true"/>

           <tx:methodname="add*"propagation="REQUIRED"/>

           <tx:methodname="save*"propagation="REQUIRED"/>

           <tx:methodname="do*"propagation="REQUIRED"/>

           <tx:methodname="update*"propagation="REQUIRED"/>

           <tx:methodname="del*"propagation="REQUIRED"/>

       </tx:attributes>

    </tx:advice>

    <aop:config>

       <aop:pointcutexpression="execution(*com.wx.service.*.*(..))"id="myCut"/>

       <aop:advisoradvice-ref="txAdvise"pointcut-ref="myCut"/>

    </aop:config>

    <!-- 统一事务处理的配置end -->

</beans>

=====================================================================

 

步骤八:编写dao层的配置文件applicationContext-dao.xml,如下所示:

===================== applicationContext-dao.xml====================

<?xmlversion="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:aop="http://www.springframework.org/schema/aop"

    xmlns:tx="http://www.springframework.org/schema/tx"

    xsi:schemaLocation="http://www.springframework.org/schema/beans

     http://www.springframework.org/schema/beans/spring-beans-3.0.xsd

     http://www.springframework.org/schema/aop

     http://www.springframework.org/schema/aop/spring-aop-3.0.xsd

     http://www.springframework.org/schema/tx

     http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">

   

    <beanid="userDao"class="com.wx.dao.user.UserDaoImpl">

       <propertyname="sessionFactory"ref="sessionFactory">

       </property>

    </bean>

    <beanid="acctDao"class="com.wx.dao.account.AcountDaoImpl">

       <propertyname="sessionFactory"ref="sessionFactory">

       </property>

    </bean>

    <beanid="studentDao"class="com.wx.dao.school.StudentDaoImpl">

       <propertyname="sessionFactory"ref="sessionFactory">

       </property>

    </bean>

    <beanid="deptDao"class="com.wx.dao.school.DepartmentDaoImpl">

       <propertyname="sessionFactory"ref="sessionFactory">

       </property>

    </bean>

</beans>

=====================================================================

 

步骤九:编写service层的配置文件applicationContext-service.xml,如下所示:

====================applicationContext-service.xml==================

<?xmlversion="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:aop="http://www.springframework.org/schema/aop"

    xmlns:tx="http://www.springframework.org/schema/tx"

    xsi:schemaLocation="http://www.springframework.org/schema/beans

     http://www.springframework.org/schema/beans/spring-beans-3.0.xsd

     http://www.springframework.org/schema/aop

     http://www.springframework.org/schema/aop/spring-aop-3.0.xsd

     http://www.springframework.org/schema/tx

     http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">

   

    <beanid="userService"class="com.wx.service.UserService">

       <propertyname="userDao"ref="userDao"></property>

    </bean>

    <beanid="schoolService"class="com.wx.service.SchoolService">

       <propertyname="deptDao"ref="deptDao"></property>

       <propertyname="studentDao"ref="studentDao"></property>

    </bean>

    <beanid="acctService"class="com.wx.service.AccountService">

       <propertyname="acctDao"ref="acctDao"></property>

    </bean>

</beans>

=====================================================================

步骤十:编写spring mvc的核心配置文件springmvc-dispatcher.xml,如下所示:

==================== springmvc-dispatcher.xml==================

<?xmlversion="1.0"encoding="UTF-8"?>

<!DOCTYPEbeans PUBLIC "-//SPRING//DTDBEAN//EN"

 "http://www.springframework.org/dtd/spring-beans.dtd">

<beans>   

    <!-- 导入控制器的bean配置 -->

    <importresource="classpath:configs/mvc/springmvc-controler.xml"/>

    <!-- 视图解析器 -->

    <beanid="resourceView"

       class="org.springframework.web.servlet.view.InternalResourceViewResolver">

       <propertyname="prefix"value="/"></property>

       <propertyname="suffix"value=".jsp"></property>

    </bean>

    <!-- 映射处理器 -->

    <beanid="simpleUrlMapping"

         class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">

         <propertyname="mappings">

            <props>

                <propkey="/Login.php">userControl</prop>

                <propkey="/Quit.php">userControl</prop>

                <propkey="/Trans.php">acctControl</prop>

                <propkey="/GetAllDept.php">deptControl</prop>

                <propkey="/QueryStu.php">studentControl</prop>

                <propkey="/Register.php">userControl</prop>

                <propkey="/ShowOneUser.php">userControl</prop>

                <propkey="/CheckUser.php">userControl</prop>

            </props>

         </property>

    </bean>

   

    <!-- 方法名称解析器 -->

    <beanid="springMethodNameResolver"

         class="org.springframework.web.servlet.mvc.multiaction.PropertiesMethodNameResolver">

       <propertyname="mappings">

           <props>

              <propkey="/Login.php">login</prop>

              <propkey="/Quit.php">quit</prop>

              <propkey="/Trans.php">doTransfer</prop>

              <propkey="/GetAllDept.php">getAllDeptNames</prop>

              <propkey="/QueryStu.php">queryAllStu</prop>

              <propkey="/Register.php">register</prop>

              <propkey="/ShowOneUser.php">showOne</prop>

              <propkey="/CheckUser.php">isUserExist</prop>

           </props>

       </property>

    </bean>

</beans>

=====================================================================

 

步骤十一:编写控制器的配置文件springmvc-controler.xml,如下所示:

==================== springmvc-controler.xml==============================

<?xmlversion="1.0"encoding="UTF-8"?>

<!DOCTYPEbeans PUBLIC "-//SPRING//DTDBEAN//EN"

 "http://www.springframework.org/dtd/spring-beans.dtd">

<beans>   

    <beanid="userControl"class="com.wx.controler.user.UserControl">

       <propertyname="methodNameResolver"ref="springMethodNameResolver">

       </property>

       <propertyname="userServ"ref="userService"></property>

    </bean>   

    <beanid="acctControl"class="com.wx.controler.account.AccountControl">

       <propertyname="methodNameResolver"ref="springMethodNameResolver">

       </property>

       <propertyname="acctServ"ref="acctService"></property>

    </bean>

    <beanid="deptControl"class="com.wx.controler.school.DepartControl">

       <propertyname="methodNameResolver"ref="springMethodNameResolver">

       </property>

       <propertyname="schoolServ"ref="schoolService"></property>

    </bean>

    <beanid="studentControl"class="com.wx.controler.school.StudentControl">

       <propertyname="methodNameResolver"ref="springMethodNameResolver">

       </property>

       <propertyname="schoolServ"ref="schoolService"></property>

    </bean>

</beans>

=====================================================================

说明:其它的java类和jsp代码不一一列出,因为实在太多了,有需要请联系我。

 

步骤十二: 启动tomcat,发布项目,在地址栏分别输入 http://localhost:8080/SHMvc

进行测试。 登录后的结果如图所示:

 联系作者:


阅读全文
2 0
原创粉丝点击