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

转自 http://blog.csdn.net/bjyfb/article/details/8998267

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.       
  51. }  

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

[html] view plain copy
  1.   
  2.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  3.     xmlns:context="http://www.springframework.org/schema/context"  
  4.     xmlns:mvc="http://www.springframework.org/schema/mvc"  
  5.     xsi:schemaLocation="http://www.springframework.org/schema/beans   
  6.     http://www.springframework.org/schema/beans/spring-beans.xsd">  
  7.       
  8.       
  9.       
  10.           
  11.           
  12.           
  13.           
  14.       
  15.       
  16.       
  17.       
  18.           
  19.           
  20.               
  21.                 org.hibernate.dialect.MySQLDialect  
  22.                 update  
  23.                 true  
  24.                 true  
  25.               
  26.           
  27.           
  28.               
  29.                 com.tgb.entity.User  
  30.               
  31.           
  32.       
  33.       
  34.       
  35.       
  36.           
  37.       
  38.       
  39.       
  40.         
  41.             
  42.             
  43.                 
  44.                 PROPAGATION_REQUIRED,-Exception    
  45.                 PROPAGATION_REQUIRED,-myException    
  46.                 PROPAGATION_REQUIRED    
  47.                 PROPAGATION_REQUIRED    
  48.                 
  49.             
  50.        
  51.   

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

[html] view plain copy
  1.   
  2.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  3.     xmlns:context="http://www.springframework.org/schema/context"  
  4.     xmlns:mvc="http://www.springframework.org/schema/mvc"  
  5.     xsi:schemaLocation="http://www.springframework.org/schema/beans   
  6.     http://www.springframework.org/schema/beans/spring-beans.xsd  
  7.     http://www.springframework.org/schema/context  
  8.     http://www.springframework.org/schema/context/spring-context-3.2.xsd  
  9.     http://www.springframework.org/schema/mvc  
  10.     http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd">  
  11.       
  12.       
  13.       
  14.   
  15.       
  16.       
  17.       
  18.       
  19.       
  20.   
  21.         
  22.       
  23.           
  24.           
  25.       
  26.   

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

[html] view plain copy
  1.   
  2.   
  3.   json_test  
  4.     
  5.     login.jsp  
  6.     
  7.     
  8.     
  9.     
  10.     contextConfigLocation  
  11.     classpath*:config/spring/spring-*.xml  
  12.     
  13.     
  14.     
  15.     
  16.     org.springframework.web.context.ContextLoaderListener  
  17.     
  18.     
  19.     
  20.     
  21.     springMVC  
  22.     org.springframework.web.servlet.DispatcherServlet  
  23.       
  24.         contextConfigLocation  
  25.         classpath*:config/spring/spring-mvc.xml  
  26.       
  27.     1  
  28.     
  29.     
  30.     springMVC  
  31.     /  
  32.     
  33.     
  34.     
  35.     
  36.     encodingFilter  
  37.     org.springframework.web.filter.CharacterEncodingFilter  
  38.       
  39.         encoding  
  40.         UTF-8  
  41.       
  42.       
  43.         forceEncoding  
  44.         true  
  45.       
  46.     
  47.     
  48.     encodingFilter  
  49.     /*  
  50.     
  51.     
  52.     
  53.     
  54.     openSession  
  55.     org.springframework.orm.hibernate4.support.OpenSessionInViewFilter  
  56.     
  57.     
  58.     openSession  
  59.     /*  
  60.     
  61.   

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

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

[html] view plain copy
  1. 进入用户管理页
      

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.   
  5.   
  6.   
  7.   
  8.   
  9. Insert title here  
  10.   
  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.   
  22.   
  23.   
  24.     
    添加用户
      
  25.       
  26.           
  27.               
  28.                 姓名  
  29.                 年龄  
  30.                 操作  
  31.               
  32.               
  33.                   
  34.                       
  35.                         ${user.userName }  
  36.                         ${user.age }  
  37.                           
  38.                             编辑  
  39.                             删除  
  40.                           
  41.                                    
  42.                   
  43.               
  44.           
  45.       
  46.   
  47.   

addUser.jsp,添加用户界面

[html] view plain copy
  1. <%@ page language="java" contentType="text/html; charset=UTF-8"  
  2.     pageEncoding="UTF-8"%>  
  3.   
  4.   
  5.   
  6.   
  7. Insert title here  
  8.   
  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.   
  16.   
  17.   
  18.     

    添加用户

      
  19.       
  20.         姓名:  
  21.         年龄:  
  22.           
  23.       
  24.   
  25.   

editUser.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.   
  5.   
  6.   
  7.   
  8.   
  9. Insert title here  
  10.   
  11.   
  12.     

    编辑用户

      
  13.       
  14.           
  15.         姓名:  
  16.         年龄:  
  17.           
  18.       
  19.   
  20.   

还有success.jsp和error.jsp页面,无代码,就不再展示。

框架越来越多,越来越好用,但随之而来的繁杂的、各成体系的配置怎么办?项目大了感觉注解靠谱些。

下篇继续。

你可能感兴趣的:(sshm)