SpringMVC+Hibernate+Spring整合实例(一)

SpringMVC又一个漂亮的web框架,他与Struts2并驾齐驱,Struts出世早而占据了一定优势,我在博客《Struts1+Hibernate+Spring整合实例》中做了一个简单的实例,介绍了SSH1的基本搭建方式,Struts2是根据Struts1发展而来,博客中就没有贴SSH2的例子,只对比了下Struts1和Struts2异同,通过对比,SSH2的搭建基本不在话下了。下面同样做一个简单的应用实例,介绍SpringMVC的基本用法,接下来的博客也将梳理一下Struts2和SpringMVC的一些异同,通过梳理和旧知识的联系,让学习的成本变低,花很短的时间就可以了解一门貌似新的技术,其实本质没变。

下面开始实例,这个实例的需求是对用户信息进行增删改查。首先创建一个web项目test_ssh,目录结构及需要的Jar包如下图:

SpringMVC+Hibernate+Spring整合实例(一)_第1张图片

SpringMVC+Hibernate+Spring整合实例(一)_第2张图片SpringMVC+Hibernate+Spring整合实例(一)_第3张图片

创建一个User实体类,放在Entity包下,采用注解的方式:

[java] view plain copy
  1. package com.tgb.entity; 
  2.  
  3. import javax.persistence.Column; 
  4. import javax.persistence.Entity; 
  5. import javax.persistence.GeneratedValue; 
  6. import javax.persistence.Id; 
  7. import javax.persistence.Table; 
  8.  
  9. import org.hibernate.annotations.GenericGenerator; 
  10.  
  11. @Entity 
  12. @Table(name="T_USER") 
  13. public class User { 
  14.  
  15.     @Id 
  16.     @GeneratedValue(generator="system-uuid") 
  17.     @GenericGenerator(name = "system-uuid",strategy="uuid") 
  18.     @Column(length=32) 
  19.     private String id; 
  20.      
  21.     @Column(length=32) 
  22.     private String userName; 
  23.      
  24.     @Column(length=32) 
  25.     private String age; 
  26.  
  27.     public String getId() { 
  28.         return id; 
  29.     } 
  30.  
  31.     public void setId(String id) { 
  32.         this.id = id; 
  33.     } 
  34.  
  35.     public String getUserName() { 
  36.         return userName; 
  37.     } 
  38.  
  39.     public void setUserName(String userName) { 
  40.         this.userName = userName; 
  41.     } 
  42.  
  43.     public String getAge() { 
  44.         return age; 
  45.     } 
  46.  
  47.     public void setAge(String age) { 
  48.         this.age = age; 
  49.     } 
  50.      

本篇关于SpringMVC基本都会采用注解的方式,首先配置好数据源以及事务spring-common.xml,放在config.spring包下:

[html] view plain copy
  1. <?xml version="1.0" encoding="UTF-8"?> 
  2. <beans xmlns="http://www.springframework.org/schema/beans" 
  3.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
  4.     xmlns:context="http://www.springframework.org/schema/context" 
  5.     xmlns:mvc="http://www.springframework.org/schema/mvc" 
  6.     xsi:schemaLocation="http://www.springframework.org/schema/beans  
  7.     http://www.springframework.org/schema/beans/spring-beans.xsd"> 
  8.      
  9.     <!-- 配置数据源 --> 
  10.     <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource" > 
  11.         <property name="driverClassName" value="com.mysql.jdbc.Driver"></property> 
  12.         <property name="url" value="jdbc:mysql://localhost/test_ssh"></property> 
  13.         <property name="username" value="root"></property> 
  14.         <property name="password" value="1"></property> 
  15.     </bean> 
  16.      
  17.     <!-- 配置SessionFactory --> 
  18.     <bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean"> 
  19.         <property name="dataSource" ref="dataSource" /> 
  20.         <property name="hibernateProperties"> 
  21.             <props> 
  22.                 <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop> 
  23.                 <prop key="hibernate.hbm2ddl.auto">update</prop> 
  24.                 <prop key="hibernate.show_sql">true</prop> 
  25.                 <prop key="hibernate.format_sql">true</prop> 
  26.             </props> 
  27.         </property> 
  28.         <property name="annotatedClasses"> 
  29.             <list> 
  30.                 <value>com.tgb.entity.User</value> 
  31.             </list> 
  32.         </property> 
  33.     </bean> 
  34.      
  35.     <!-- 配置一个事务管理器 --> 
  36.     <bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager"> 
  37.         <property name="sessionFactory" ref="sessionFactory"/> 
  38.     </bean> 
  39.      
  40.     <!-- 配置事务,使用代理的方式 --> 
  41.     <bean id="transactionProxy" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean" abstract="true">   
  42.         <property name="transactionManager" ref="transactionManager"></property>   
  43.         <property name="transactionAttributes">   
  44.             <props>   
  45.                 <prop key="add*">PROPAGATION_REQUIRED,-Exception</prop>   
  46.                 <prop key="modify*">PROPAGATION_REQUIRED,-myException</prop>   
  47.                 <prop key="del*">PROPAGATION_REQUIRED</prop>   
  48.                 <prop key="*">PROPAGATION_REQUIRED</prop>   
  49.             </props>   
  50.         </property>   
  51.     </bean>  
  52. </beans> 

然后配置关于SpringMVC的内容,下面配置中都有注释说明,就不再赘述,spring-mvc.xml放在config.spring包下:

[html] view plain copy
  1. <?xml version="1.0" encoding="UTF-8"?> 
  2. <beans xmlns="http://www.springframework.org/schema/beans" 
  3.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
  4.     xmlns:context="http://www.springframework.org/schema/context" 
  5.     xmlns:mvc="http://www.springframework.org/schema/mvc" 
  6.     xsi:schemaLocation="http://www.springframework.org/schema/beans  
  7.     http://www.springframework.org/schema/beans/spring-beans.xsd 
  8.     http://www.springframework.org/schema/context 
  9.     http://www.springframework.org/schema/context/spring-context-3.2.xsd 
  10.     http://www.springframework.org/schema/mvc 
  11.     http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd"> 
  12.      
  13.     <!-- 注解扫描包 --> 
  14.     <context:component-scan base-package="com.tgb" /> 
  15.  
  16.     <!-- 开启注解 --> 
  17.     <mvc:annotation-driven /> 
  18.      
  19.     <!-- 静态资源(js/image)的访问 --> 
  20.     <mvc:resources location="/js/" mapping="/js/**"/> 
  21.  
  22.     <!-- 定义视图解析器 -->   
  23.     <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"> 
  24.         <property name="prefix" value="/"></property> 
  25.         <property name="suffix" value=".jsp"></property> 
  26.     </bean> 
  27. </beans> 

完成这些共用的配置之后,来配置web项目起点web.xml:

[html] view plain copy
  1. <?xml version="1.0" encoding="UTF-8"?> 
  2. <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/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5"> 
  3.   <display-name>json_test</display-name> 
  4.   <welcome-file-list> 
  5.     <welcome-file>login.jsp</welcome-file> 
  6.   </welcome-file-list> 
  7.    
  8.   <!-- 加载所有的配置文件 --> 
  9.   <context-param> 
  10.     <param-name>contextConfigLocation</param-name> 
  11.     <param-value>classpath*:config/spring/spring-*.xml</param-value> 
  12.   </context-param> 
  13.    
  14.   <!-- 配置Spring监听 --> 
  15.   <listener> 
  16.     <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> 
  17.   </listener> 
  18.    
  19.   <!-- 配置SpringMVC --> 
  20.   <servlet> 
  21.     <servlet-name>springMVC</servlet-name> 
  22.     <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> 
  23.     <init-param> 
  24.         <param-name>contextConfigLocation</param-name> 
  25.         <param-value>classpath*:config/spring/spring-mvc.xml</param-value> 
  26.     </init-param> 
  27.     <load-on-startup>1</load-on-startup> 
  28.   </servlet> 
  29.   <servlet-mapping> 
  30.     <servlet-name>springMVC</servlet-name> 
  31.     <url-pattern>/</url-pattern> 
  32.   </servlet-mapping> 
  33.    
  34.   <!-- 配置字符集 --> 
  35.   <filter> 
  36.     <filter-name>encodingFilter</filter-name> 
  37.     <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class> 
  38.     <init-param> 
  39.         <param-name>encoding</param-name> 
  40.         <param-value>UTF-8</param-value> 
  41.     </init-param> 
  42.     <init-param> 
  43.         <param-name>forceEncoding</param-name> 
  44.         <param-value>true</param-value> 
  45.     </init-param> 
  46.   </filter> 
  47.   <filter-mapping> 
  48.     <filter-name>encodingFilter</filter-name> 
  49.     <url-pattern>/*</url-pattern> 
  50.   </filter-mapping> 
  51.    
  52.   <!-- 配置Session --> 
  53.   <filter> 
  54.     <filter-name>openSession</filter-name> 
  55.     <filter-class>org.springframework.orm.hibernate4.support.OpenSessionInViewFilter</filter-class> 
  56.   </filter> 
  57.   <filter-mapping> 
  58.     <filter-name>openSession</filter-name> 
  59.     <url-pattern>/*</url-pattern> 
  60.   </filter-mapping> 
  61. </web-app> 

读者需自行下载jquery包,放到webContent文件夹下的js包下。然后创建几个测试页面,分别如下:

Login.jsp,项目的入口界面。

[html] view plain copy
  1. <h5><a href="/test_ssh/user/getAllUser">进入用户管理页</a></h5> 

Index.jsp,用户管理的主界面

[html] view plain copy
  1. <%@ page language="java" contentType="text/html; charset=UTF-8" 
  2.     pageEncoding="UTF-8"%> 
  3. <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> 
  4. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 
  5. <html> 
  6. <head> 
  7. <script type="text/javascript" src="../js/jquery-1.7.1.js"></script> 
  8. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 
  9. <title>Insert title here</title> 
  10. <script type="text/javascript"> 
  11.     function del(id){ 
  12.         $.get("/test_ssh/user/delUser?id=" + id,function(data){ 
  13.             if("success" == data.result){ 
  14.                 alert("删除成功"); 
  15.                 window.location.reload(); 
  16.             }else{ 
  17.                 alert("删除失败"); 
  18.             } 
  19.         }); 
  20.     } 
  21. </script> 
  22. </head> 
  23. <body> 
  24.     <h6><a href="/test_ssh/user/toAddUser">添加用户</a></h6> 
  25.     <table border="1"> 
  26.         <tbody> 
  27.             <tr> 
  28.                 <th>姓名</th> 
  29.                 <th>年龄</th> 
  30.                 <th>操作</th> 
  31.             </tr> 
  32.             <c:if test="${!empty userList }"> 
  33.                 <c:forEach items="${userList }" var="user"> 
  34.                     <tr> 
  35.                         <td>${user.userName }</td> 
  36.                         <td>${user.age }</td> 
  37.                         <td> 
  38.                             <a href="/test_ssh/user/getUser?id=${user.id }">编辑</a> 
  39.                             <a href="javascript:del('${user.id }')">删除</a> 
  40.                         </td> 
  41.                     </tr>              
  42.                 </c:forEach> 
  43.             </c:if> 
  44.         </tbody> 
  45.     </table> 
  46. </body> 
  47. </html> 

addUser.jsp,添加用户界面

[html] view plain copy
  1. <%@ page language="java" contentType="text/html; charset=UTF-8" 
  2.     pageEncoding="UTF-8"%> 
  3. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 
  4. <html> 
  5. <head> 
  6. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 
  7. <title>Insert title here</title> 
  8. <script type="text/javascript"> 
  9.     function addUser(){ 
  10.         var form = document.forms[0]; 
  11.         form.action = "/test_ssh/user/addUser"; 
  12.         form.method="post"; 
  13.         form.submit(); 
  14.     } 
  15. </script> 
  16. </head> 
  17. <body> 
  18.     <h1>添加用户</h1> 
  19.     <form action="" name="userForm"> 
  20.         姓名:<input type="text" name="userName"> 
  21.         年龄:<input type="text" name="age"> 
  22.         <input type="button" value="添加" onclick="addUser()"> 
  23.     </form> 
  24. </body> 
  25. </html> 

editUser.jsp,修改用户信息界面。

[html] view plain copy
  1. <%@ page language="java" contentType="text/html; charset=UTF-8"   pageEncoding="UTF-8"%> 
  2. <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>   
  3. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 
  4. <html> 
  5. <head> 
  6. <script type="text/javascript" src="../js/jquery-1.7.1.js"></script> 
  7. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 
  8. <title>Insert title here</title> 
  9. </head> 
  10. <body> 
  11.     <h1>编辑用户</h1> 
  12.     <form action="/test_ssh/user/updateUser" name="userForm" method="post"> 
  13.         <input type="hidden" name="id" value="${user.id }"> 
  14.         姓名:<input type="text" name="userName" value="${user.userName }"> 
  15.         年龄:<input type="text" name="age" value="${user.age }"> 
  16.         <input type="submit" value="编辑" > 
  17.     </form> 
  18. </body> 
  19. </html> 

你可能感兴趣的:(spring,数据库,Hibernate,ssh,Web应用)