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

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

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




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

[java] view plaincopy
package com.tgb.entity; 
 
import javax.persistence.Column; 
import javax.persistence.Entity; 
import javax.persistence.GeneratedValue; 
import javax.persistence.Id; 
import javax.persistence.Table; 
 
import org.hibernate.annotations.GenericGenerator; 
 
@Entity 
@Table(name="T_USER") 
public class User { 
 
    @Id 
    @GeneratedValue(generator="system-uuid") 
    @GenericGenerator(name = "system-uuid",strategy="uuid") 
    @Column(length=32) 
    private String id; 
     
    @Column(length=32) 
    private String userName; 
     
    @Column(length=32) 
    private String age; 
 
    public String getId() { 
        return id; 
    } 
 
    public void setId(String id) { 
        this.id = id; 
    } 
 
    public String getUserName() { 
        return userName; 
    } 
 
    public void setUserName(String userName) { 
        this.userName = userName; 
    } 
 
    public String getAge() { 
        return age; 
    } 
 
    public void setAge(String age) { 
        this.age = age; 
    } 
     

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

[html] view plaincopy
 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xmlns:context="http://www.springframework.org/schema/context" 
    xmlns:mvc="http://www.springframework.org/schema/mvc" 
    xsi:schemaLocation="http://www.springframework.org/schema/beans  
    http://www.springframework.org/schema/beans/spring-beans.xsd"> 
     
     
     
         
         
         
         
   
 
     
     
     
         
         
             
                org.hibernate.dialect.MySQLDialect 
                update 
                true 
                true 
           
 
       
 
         
             
                com.tgb.entity.User 
           
 
       
 
   
 
     
     
     
         
   
 
     
     
       
           
           
               
                PROPAGATION_REQUIRED,-Exception   
                PROPAGATION_REQUIRED,-myException   
                PROPAGATION_REQUIRED   
                PROPAGATION_REQUIRED   
           
   
       
   
   
  
 
然后配置关于SpringMVC的内容,下面配置中都有注释说明,就不再赘述,spring-mvc.xml放在config.spring包下:

[html] view plaincopy
 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xmlns:context="http://www.springframework.org/schema/context" 
    xmlns:mvc="http://www.springframework.org/schema/mvc" 
    xsi:schemaLocation="http://www.springframework.org/schema/beans  
    http://www.springframework.org/schema/beans/spring-beans.xsd 
    http://www.springframework.org/schema/context 
    http://www.springframework.org/schema/context/spring-context-3.2.xsd 
    http://www.springframework.org/schema/mvc 
    http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd"> 
     
     
     
 
     
     
     
     
     
 
       
     
         
         
   
 
 
完成这些共用的配置之后,来配置web项目起点web.xml:

[html] view plaincopy
 
 
  json_test 
   
    login.jsp 
 
 
   
   
   
    contextConfigLocation 
    classpath*:config/spring/spring-*.xml 
 
 
   
   
   
    org.springframework.web.context.ContextLoaderListener 
 
 
   
   
   
    springMVC 
    org.springframework.web.servlet.DispatcherServlet 
     
        contextConfigLocation 
        classpath*:config/spring/spring-mvc.xml 
   
 
    1 
 
 
   
    springMVC 
    / 
 
 
   
   
   
    encodingFilter 
    org.springframework.web.filter.CharacterEncodingFilter 
     
        encoding 
        UTF-8 
   
 
     
        forceEncoding 
        true 
   
 
 
 
   
    encodingFilter 
    /* 
 
 
   
   
   
    openSession 
    org.springframework.orm.hibernate4.support.OpenSessionInViewFilter 
 
 
   
    openSession 
    /* 
 
 
 
读者需自行下载jquery包,放到webContent文件夹下的js包下。然后创建几个测试页面,分别如下:

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

[html] view plaincopy
进入用户管理页
 
Index.jsp,用户管理的主界面

[html] view plaincopy
<%@ page language="java" contentType="text/html; charset=UTF-8" 
    pageEncoding="UTF-8"%> 
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> 
 
 
 
 
 
Insert title here 
 
 
 
   
添加用户
 
     
         
             
                 
                 
                 
             
             
                 
                   
 
                         
                         
                         
                                  
                 
             
         
   
姓名年龄操作
${user.userName }${user.age } 
                            编辑 
                            删除 
                       
 
 
 
addUser.jsp,添加用户界面

[html] view plaincopy
<%@ page language="java" contentType="text/html; charset=UTF-8" 
    pageEncoding="UTF-8"%> 
 
 
 
 
Insert title here 
 
 
 
   

添加用户

 
   
 
        姓名: 
        年龄: 
         
   
 
 
 
editUser.jsp,修改用户信息界面。

[html] view plaincopy
<%@ page language="java" contentType="text/html; charset=UTF-8" 
    pageEncoding="UTF-8"%> 
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>   
 
 
 
 
 
Insert title here 
 
 
   

编辑用户

 
   
 
         
        姓名: 
        年龄: 
         
   
 
 
 

你可能感兴趣的:(SpringMVC+Hibernate+Spring整合实例(一))