全网整合营销服务商

电脑端+手机端+微信端=数据同步管理

免费咨询热线:400-708-3566

浅谈SpringMVC+Spring3+Hibernate4开发环境搭建

早期的项目比较简单,多是用JSP 、Servlet + JDBC 直接搞定,后来使用 Struts1(Struts2)+Spring+Hibernate, 严格按照分层概念驱动项目开发,这次又使用 Spring MVC取代Struts来进行开发。

MVC已经是现代Web开发中的一个很重要的部分,下面介绍一下SpringMVC+Spring3+Hibernate4的开发环境搭建

先大致看一下项目结构:

 

具体的代码不再演示,主要是走了一个很平常的路线,mvc-servcie-dao-hibernate的结构,源码可以下载,主要看一下配置文件。解释见注释

web.xml

<?xml version="1.0" encoding="UTF-8"?> 
<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"> 
 <display-name>SpringMVC</display-name> 
 <welcome-file-list> 
  <welcome-file>index.jsp</welcome-file> 
 </welcome-file-list> 
  
 <!-- 配置Spring -->  
 <context-param> 
    <param-name>contextConfigLocation</param-name> 
    <param-value>classpath*:config/spring-*.xml</param-value> 
 </context-param> 
  
  
 <listener> 
   <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> 
 </listener> 
  
  
 <!-- 配置SpringMVC --> 
 <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*:config/spring-servlet.xml</param-value> 
  </init-param> 
  <load-on-startup>1</load-on-startup> 
 </servlet> 
  
 <servlet-mapping> 
  <servlet-name>springMVC</servlet-name> 
  <url-pattern>/</url-pattern> 
 </servlet-mapping> 
  
 <!-- 设置字符集 --> 
 <filter> 
  <filter-name>encodingFilter</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>encodingFilter</filter-name> 
  <url-pattern>/*</url-pattern> 
 </filter-mapping> 
  
  
 <!-- 控制Session的开关 --> 
 <filter> 
    <filter-name>openSession</filter-name> 
    <filter-class>org.springframework.orm.hibernate4.support.OpenSessionInViewFilter</filter-class> 
 </filter> 
  
 <filter-mapping> 
  <filter-name>openSession</filter-name> 
  <url-pattern>/*</url-pattern> 
 </filter-mapping> 
  
</web-app> 

spring-servlet.xml

<?xml version="1.0" encoding="UTF-8"?> 
<beans xmlns="http://www.springframework.org/schema/beans" 
  xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p" 
  xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
  xsi:schemaLocation="http://www.springframework.org/schema/beans  
   http://www.springframework.org/schema/beans/spring-beans-3.0.xsd  
   http://www.springframework.org/schema/context  
   http://www.springframework.org/schema/context/spring-context.xsd  
   http://www.springframework.org/schema/mvc  
   http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd"> 
 
  <!-- 注解扫描的包 --> 
  <context:component-scan base-package="com.jialin" /> 
 
  <!-- 开启注解方案1 --> 
  <!-- 注解方法处理 --> 
  <!-- <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"  
    /> --> 
  <!-- 注解类映射处理 --> 
  <!-- <bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping"></bean> --> 
 
  <!-- 开启注解方案2 --> 
  <mvc:annotation-driven /> 
 
  <!-- 静态资源访问,方案1 --> 
  <mvc:resources location="/img/" mapping="/img/**" /> 
  <mvc:resources location="/js/" mapping="/js/**" /> 
 
  <!-- 静态资源访问,方案2 --> 
  <!-- <mvc:default-servlet-handler/> --> 
 
  <!-- 视图解释类 --> 
  <bean id="viewResolver" 
    class="org.springframework.web.servlet.view.InternalResourceViewResolver"> 
    <property name="prefix" value="/"></property> 
    <!--可为空,方便实现自已的依据扩展名来选择视图解释类的逻辑 --> 
    <property name="suffix" value=".jsp"></property> 
  </bean> 
 
  <!-- 上传文件bean --> 
  <!-- <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">  
    <property name="defaultEncoding" value="utf-8" /> <property name="maxUploadSize"  
    value="10485760000" /> <property name="maxInMemorySize" value="40960" />  
    </bean> --> 
 
</beans>  

spring-hibernate.xml

<?xml version="1.0" encoding="UTF-8"?> 
<beans xmlns="http://www.springframework.org/schema/beans" 
  xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p" 
  xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
  xsi:schemaLocation="http://www.springframework.org/schema/beans  
   http://www.springframework.org/schema/beans/spring-beans-3.0.xsd  
   http://www.springframework.org/schema/context  
   http://www.springframework.org/schema/context/spring-context.xsd  
   http://www.springframework.org/schema/mvc  
   http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd"> 
   
  <!-- 配置数据源 --> 
  <bean id="dataSource" 
    class="org.springframework.jdbc.datasource.DriverManagerDataSource"> 
    <property name="driverClassName" value="com.mysql.jdbc.Driver" /> 
    <property name="url" value="jdbc:mysql://127.0.0.1/springmvc" /> 
    <property name="username" value="root" /> 
    <property name="password" value="123456" /> 
  </bean> 
 
  <!-- 配置hibernate SessionFactory--> 
  <bean id="sessionFactory" 
    class="org.springframework.orm.hibernate4.LocalSessionFactoryBean"> 
    <property name="dataSource" ref="dataSource" /> 
    <property name="hibernateProperties"> 
      <props> 
        <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop> 
        <prop key="hibernate.hbm2ddl.auto">update</prop> 
        <prop key="hibernate.show_sql">true</prop> 
        <prop key="hiberante.format_sql">true</prop> 
      </props> 
    </property> 
    <property name="configLocations"> 
      <list> 
        <value> 
          classpath*:config/hibernate/hibernate.cfg.xml 
        </value> 
      </list> 
    </property> 
  </bean> 
 
  <!-- 事务管理器 --> 
  <bean id="transactionManager" 
    class="org.springframework.orm.hibernate4.HibernateTransactionManager"> 
    <property name="sessionFactory" ref="sessionFactory"></property> 
  </bean> 
   
  <!-- 事务代理类 --> 
  <bean id="transactionBese" 
    class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean" 
    lazy-init="true" abstract="true"> 
    <property name="transactionManager" ref="transactionManager"></property> 
    <property name="transactionAttributes"> 
      <props> 
        <prop key="add*">PROPAGATION_REQUIRED,-Exception</prop> 
        <prop key="update*">PROPAGATION_REQUIRED,-Exception</prop> 
        <prop key="insert*">PROPAGATION_REQUIRED,-Exception</prop> 
        <prop key="modify*">PROPAGATION_REQUIRED,-Exception</prop> 
        <prop key="delete*">PROPAGATION_REQUIRED,-Exception</prop> 
        <prop key="del*">PROPAGATION_REQUIRED,-Exception</prop> 
        <prop key="get*">PROPAGATION_NEVER</prop> 
      </props> 
    </property> 
  </bean> 
 
</beans>  

spring-core.xml

<?xml version="1.0" encoding="UTF-8"?> 
<beans xmlns="http://www.springframework.org/schema/beans"  
 xmlns:context="http://www.springframework.org/schema/context"  
 xmlns:p="http://www.springframework.org/schema/p"  
 xmlns:mvc="http://www.springframework.org/schema/mvc"  
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
 xsi:schemaLocation="http://www.springframework.org/schema/beans  
   http://www.springframework.org/schema/beans/spring-beans-3.0.xsd  
   http://www.springframework.org/schema/context  
   http://www.springframework.org/schema/context/spring-context.xsd  
   http://www.springframework.org/schema/mvc  
   http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd"> 
    
  <!-- 引入其他配置文件,可以为多个 --> 
  <import resource="classpath*:config/spring/spring-user.xml"/> 
   
 </beans>  

spring-user.xml

<?xml version="1.0" encoding="UTF-8"?> 
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN 2.0//EN" "http://www.springframework.org/dtd/spring-beans-2.0.dtd" [ 
<!ENTITY contextInclude SYSTEM "org/springframework/web/context/WEB-INF/contextInclude.xml"> 
]> 
 
<beans> 
  <!-- Spring Bean --> 
  <bean id="userDao" class="com.jialin.dao.UserDao"> 
    <property name="sessionFactory" ref="sessionFactory"></property> 
  </bean> 
   
  <bean id="userManagerBase" class="com.jialin.service.UserManager"> 
    <property name="userDao" ref="userDao"></property> 
  </bean> 
   
  <!-- parent为transactionBese,表示支持事务 --> 
  <bean id="userManager" parent="transactionBese"> 
    <property name="target" ref="userManagerBase"></property> 
  </bean> 
   
</beans> 

hibernate.cfg.xml

<!DOCTYPE hibernate-configuration PUBLIC 
    "-//Hibernate/Hibernate Configuration DTD 3.0//EN" 
    "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd"> 
 
<hibernate-configuration> 
  <session-factory> 
    <!-- 引入需要映射的类 --> 
    <mapping class="com.jialin.entity.User"/> 
  </session-factory> 
</hibernate-configuration> 

下面再来看看Controller

package com.jialin.controller; 
 
import java.io.IOException; 
import java.io.PrintWriter; 
import java.util.List; 
 
import javax.annotation.Resource; 
import javax.servlet.http.HttpServletRequest; 
import javax.servlet.http.HttpServletResponse; 
 
import org.springframework.stereotype.Controller; 
import org.springframework.web.bind.annotation.RequestMapping; 
 
import com.jialin.entity.User; 
import com.jialin.service.IUserManager; 
 
@Controller  //类似Struts的Action 
@RequestMapping("/user")  
public class UserController { 
   
  @Resource(name="userManager") // 获取spring配置文件中bean的id为userManager的,并注入 
  private IUserManager userManager; 
   
  @RequestMapping("/addUser")  // 请求url地址映射,类似Struts的action-mapping 
  public String addUser(User user){ 
    if(userManager.addUser(user)) 
    { 
      // 重定向 
      return "redirect:/user/getAllUser"; 
    }else 
    { 
      return "/fail"; 
    } 
     
  } 
   
  @RequestMapping("/updateUser") 
  public String updateUser(User user,HttpServletRequest request){ 
    if (userManager.updateUser(user)) 
    { 
      user = userManager.getOneUser(user); 
      request.setAttribute("user", user); 
      return "/UserEdit"; 
    }else 
    { 
      return "/fail"; 
    } 
     
  } 
   
  @RequestMapping("/delUser") 
  public void delUser(User user,HttpServletResponse response){ 
    String result = "{\"result\":\"error\"}"; 
     
    if(userManager.delUser(user)){ 
      result = "{\"result\":\"success\"}"; 
    } 
    PrintWriter out = null; 
    response.setContentType("application/json"); 
     
    try { 
      out = response.getWriter(); 
      out.write(result); 
    } catch (IOException e) { 
      e.printStackTrace(); 
    } 
 
  } 
  @RequestMapping("/toAddUser") 
  public String toAddUser(){ 
    return "/UserAdd"; 
  } 
   
  @RequestMapping("/toUpdateUser") 
  public String toUpdateUser(User user,HttpServletRequest request){ 
    User user1=userManager.getOneUser(user); 
     
    request.setAttribute("user1", user1); 
     
    return "/UserEdit"; 
  } 
   
  @RequestMapping("/getAllUser") 
  public String getAllUser(HttpServletRequest request){ 
     
    List userList=userManager.getAllUser(); 
     
    request.setAttribute("userlist", userList); 
     
    return "/UserMain"; 
  } 
   
} 

源码下载……

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。


# 搭建spring  # hibernate  # springmvc  # springmvc3  # hibernate4  # Spring+SpringMVC+Hibernate项目环境搭建的步骤(图文)  # 基于springMvc+hibernate的web application的构建  # 轻松玩转BootstrapTable(后端使用SpringMVC+Hibernate)  # Java框架篇:Spring+SpringMVC+hibernate整合开发  # springmvc4+hibernate4分页查询功能实现  # Spring+SpringMVC+Hibernate整合实例讲解  # 配置文件  # 看一下  # 走了  # 多个  # 扩展名  # 再来  # 很重要  # 管理器  # 介绍一下  # 可以下载  # 大家多多  # 源码下载  # 为空  # 上传文件  # 主要是  # 很平常  # 重定向  # 严格按照  # orm  # true 


相关文章: 如何续费美橙建站之星域名及服务?  设计网站制作公司有哪些,制作网页教程?  如何确保西部建站助手FTP传输的安全性?  网站建设制作、微信公众号,公明人民医院怎么在网上预约?  较简单的网站制作软件有哪些,手机版网页制作用什么软件?  香港服务器网站测试全流程:性能评估、SEO加载与移动适配优化  济南专业网站制作公司,济南信息工程学校怎么样?  建站主机选购指南与交易推荐:核心配置解析  一键制作网站软件下载安装,一键自动采集网页文档制作步骤?  代刷网站制作软件,别人代刷火车票靠谱吗?  建站之星图片链接生成指南:自助建站与智能设计教程  一键网站制作软件,义乌购一件代发流程?  如何选择高效稳定的ISP建站解决方案?  如何制作公司的网站链接,公司想做一个网站,一般需要花多少钱?  企业网站制作费用多少,企业网站空间一般需要多大,费用是多少?  如何将凡科建站内容保存为本地文件?  视频网站app制作软件,有什么好的视频聊天网站或者软件?  如何在万网ECS上快速搭建专属网站?  网站制作员失业,怎样查看自己网站的注册者?  如何在IIS7上新建站点并设置安全权限?  建站主机是否属于云主机类型?  如何打造高效商业网站?建站目的决定转化率  青浦网站制作公司有哪些,苹果官网发货地是哪里?  存储型VPS适合搭建中小型网站吗?  如何自定义建站之星模板颜色并下载新样式?  已有域名和空间如何搭建网站?  可靠的网站设计制作软件,做网站设计需要什么样的电脑配置?  专业网站建设制作报价,网页设计制作要考什么证?  javascript中对象的定义、使用以及对象和原型链操作小结  如何快速搭建支持数据库操作的智能建站平台?  如何在云主机快速搭建网站站点?  Swift中循环语句中的转移语句 break 和 continue  如何快速使用云服务器搭建个人网站?  网站广告牌制作方法,街上的广告牌,横幅,用PS还是其他软件做的?  如何用低价快速搭建高质量网站?  如何设置并定期更换建站之星安全管理员密码?  已有域名和空间,如何快速搭建网站?  清除minerd进程的简单方法  ppt在线制作免费网站推荐,有什么下载免费的ppt模板网站?  建站之星云端配置指南:模板选择与SEO优化一键生成  建站之星2.7模板:企业网站建设与h5定制设计专题  css网站制作参考文献有哪些,易聊怎么注册?  如何选择网络建站服务器?高效建站必看指南  如何通过商城免费建站系统源码自定义网站主题?  建站之星后台管理如何实现高效配置?  ,想在网上投简历,哪几个网站比较好?  建站之星如何保障用户数据免受黑客入侵?  如何选择可靠的免备案建站服务器?  深圳网站制作公司好吗,在深圳找工作哪个网站最好啊?  昆明网站制作哪家好,昆明公租房申请网上登录入口? 

您的项目需求

*请认真填写需求信息,我们会在24小时内与您取得联系。