您的位置:首页 > 编程语言 > Java开发

springmvc+hibernate的一个简单实例

2016-11-17 11:48 483 查看
刚刚进到新的公司,看到项目都是用webservice+springmvc的形式,之前一直用的是Struts的。觉得有必要看下且记录下来。因为之前有记录过注解注入方式,但这么久没有接触了,忘记了。还好自己有记录在博客里面。
环境:spring4.0.4.RELEASE,hibernate4.3.10Final,包都是在官网上面下载。

eclipse+mysql做的springmvc+hibernate,采用的都是注解的形式。

看下工程的目录






OK,先说下步骤。

eclipse先建立一个web工程,如果你用的是web3.0的话就要创建一下web.xml(默认是不创建的)
首先看下web.xml
[code=xml;toolbar:false"><?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">
  <display-name>springmvc</display-name>
  
  <context-param>
   <param-name>contextConfigLocation</param-name>
   <param-value>classpath:spring-beans.xml</param-value>
  </context-param>
  <listener>
   <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>
  
  <!-- 防止乱码 -->
   <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>
</filter>

<!-- 如果不添加就会在hibernate4整合报错 -->
<filter>
<filter-name>OpenSessionInViewFilter</filter-name>
<filter-class>
org.springframework.orm.hibernate4.support.OpenSessionInViewFilter
</filter-class>
<init-param>
<param-name>sessionFactoryBeanName</param-name>
<param-value>sessionFactory</param-value>
</init-param>
</filter>

<filter-mapping>
<filter-name>OpenSessionInViewFilter</filter-name>
<url-pattern>*.do</url-pattern>
</filter-mapping>

<!-- springmvc-dispatcherServlet-->
  <servlet>
   <servlet-name>springmvc</servlet-name>
   <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
   <load-on-startup>1</load-on-startup>
  </servlet>
  <servlet-mapping>
   <servlet-name>springmvc</servlet-name>
   <url-pattern>*.do</url-pattern>
  </servlet-mapping>
  
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.jsp</welcome-file>
  </welcome-file-list>
</web-app>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: