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========================
<?xml version="1.0"encoding="UTF-8"?>
<web-app version="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-->