SpringMVC+Mybatis 环境搭建手记(上)

本文详细介绍如何手动搭建SpringMVC环境,包括配置web.xml、DispatcherServlet及springmvc-servlet.xml等关键步骤。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

转载请注明本文地址:http://blog.youkuaiyun.com/zzq1992126/article/details/54174774

最近工作进度很紧,已经半年没有空闲更新博客,最近有个其他组的同事需要使用java开发,于是找我搭建一个流行的java框架。上次搭建这个环境是一年多以前了,当时好多东西理解的不够透彻,并且使用Maven搭建,这次感觉到Maven对jar包的版本控制不是很理想,并且同事明确说不需要Maven,于是这次从头开始搭建一个Spring+Mybatis的框架。中间遇到了一些问题,也有很多收获,这里整理一下,做个分享,也是一个记录。

一.单纯Mybatis搭建

最开始的想法是搭建一个Servlet+Mybatis的环境,因为现在流行的都是RESTful风格的架构,于是想要使用Servlet+Mybatis做一个简单即用的框架。由于以前从来没有单纯的搭建过Mybatis框架,这里参考了@五月的仓颉 的博客。由于我基本是按照他的文章一路配置下来的,基本没有什么问题,他讲解的也很细致。这里直接粘贴他博客的连接出来,方便大家查看。
http://www.cnblogs.com/xrq730/p/5254301.html
http://www.cnblogs.com/xrq730/p/5256221.html
http://www.cnblogs.com/xrq730/p/5276988.html
http://www.cnblogs.com/xrq730/p/5289638.html

全文共六篇,不过后面两篇文章的对Spring整合似乎偏向事务管理,便没有继续下去。这四篇文章对搭建过程介绍的已经很详细了,我就不再重复说明。我自己搭建完的项目已经上传到百度云,有需要的可以留言。

二. 单独SpringMVC环境搭建

SpringMVC环境的搭建最麻烦的就是下载jar包,之前一次是使用Maven配置pom文件,然后从Maven中央仓库下载,这次jar包直接手动下载这里配置。下载地址:
http://maven.springframework.org/release/org/springframework/spring/ 这里我下载的是4.1.6版本。

1.下载完成后在Eclipse中新建一个Dynamic Web Project,新建是勾选生成web.xml选项。把下载的zip文件解压,在lib中找到所有以RELEASE结尾的jar包,扔到项目的lib目录下。其实这里可以只把相关的jar包导入,不过为了后续方便,直接全都扔进去吧。

2.在web.xml中配置SpringMVC转发控制器DispatcherServlet。

DispatcherServlet的配置如下:

  <servlet>
      <!-- 定义servlet名 -->
      <servlet-name>springmvc</servlet-name>
      <!-- 定义servlet对应的java类 -->
      <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
      <!-- 设置启动参数 -->
      <init-param>
            <param-name>contextConfigLocation</param-name>
            <!-- 启动参数配置文件位置,目前的配置是位于src下的 springmvc-servlet.xml-->
            <param-value>classpath:springmvc-servlet.xml</param-value>
      </init-param>
      <!-- 设置servlet随tomcat启动 -->
      <load-on-startup>1</load-on-startup>
  </servlet>

  <servlet-mapping>
      <!-- 定义sprngmvc这个servlet需要过滤的url,当前配置是全部url -->
      <servlet-name>springmvc</servlet-name>
      <url-pattern>/</url-pattern>
  </servlet-mapping>

本质上是配置了一个自动启动的Servlet,其实在Servlet3.0规范中,已经可以用注解配置,不过由于这里引用的是Spring的jar包,所以还是要在web.xml中配置一下。可能有人对xml的语法不是很熟悉,我已经在贴出的代码中添加了注释。

网上有的文章贴出了需要在web.xml中添加监听器:

  <listener>    
        <listener-class>org.springframework.web.context.request.RequestContextListener</listener-class>    
  </listener> 

这个监听器的作用我查了一下是为了可以让bean拥有request、session和global session这三个新添加的web作用域。如果不配置,不会影响spring启动,可以直接配置好,也可以先不做处理,等到需要相关配置的时候再行添加。

后面我又在web.xml中添加了一个字符编码过滤器,全部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" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
  <display-name>springMVC</display-name>
  <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>

  <!-- 配置servlet -->
  <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:springmvc-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>

  <!-- 配置作用域监听listener -->
  <listener>    
        <listener-class>org.springframework.web.context.request.RequestContextListener</listener-class>    
  </listener> 

  <!-- 配置字符过滤器 -->
  <filter>
        <filter-name>characterEncodingFilter</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>characterEncodingFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>   
</web-app>

3.在项目src下新建一个xml文件,起名叫做springmvc-servlet.xml,其作用是在DispatcherServlet加载时提供其所需的初始化参数,其中配置如下:

<context:component-scan base-package="springmvc"/>

配置Spring扫描的包。在xml配置了这个标签后,spring可以自动去扫描base-pack下面或者子包下面的Java文件。

<mvc:default-servlet-handler />

配置不拦截静态资源

<mvc:annotation-driven />

配置注册了DefaultAnnotationHandlerMapping与AnnotationMethodHandlerAdapter这两个bean,这两个bean具体的作用后续再说。

    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" 
            id="internalResourceViewResolver">
        <!-- 前缀 -->
        <property name="prefix" value="/webapps/page/" />
        <!-- 后缀 -->
        <property name="suffix" value=".jsp" />
    </bean>

配置要添加的前缀和后缀

全部写完的springmvc-servlet.xml如下:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    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-4.1.xsd
        http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd">                    

    <!-- scan the package and the sub package -->
    <context:component-scan base-package="springmvc"/>

    <!-- don't handle the static resource -->
    <mvc:default-servlet-handler />

    <!-- if you use annotation you must configure following setting -->
    <mvc:annotation-driven />

    <!-- configure the InternalResourceViewResolver -->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" 
            id="internalResourceViewResolver">
        <!-- 前缀 -->
        <property name="prefix" value="/webapps/page/" />
        <!-- 后缀 -->
        <property name="suffix" value=".jsp" />
    </bean>

</beans>

到这里单独的SpringMVC基本就配置完成了,不过没有整合Mybatis,可以先写一个功能来进行测试。虽然开发大型项目会稍显不够,不过作为一个简单的REST风格的后端框架来说是足够了。

以下是测试用的Servlet,由于是REST风格,直接回显了一个json字符串。需要注意的是,不知道为什么,tomcat本身的Servlet-api.jar没有生效,使用HttpServletRequest 和HttpServletResponse ,需要手动的从tomcat/lib中复制Servlet-api.jar到项目lib中。

package springmvc.basecontroller;

import java.io.IOException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

@Controller
@RequestMapping("/mvc")
public class BaseController {
    @RequestMapping("/hello")
    public void hello(@RequestParam(value="key")String key, @RequestParam(value="value") String value,HttpServletRequest request,HttpServletResponse response) throws IOException{        
        System.out.println(key+" "+value);
        response.getWriter().println("{\""+ key +"\":\""+ value +"\"}");
    }
}

访问路径:http://localhost:8080/项目名/mvc/hello?key=123&value=abc

后记:
本来想要一气呵成写完Spring+Mybatis整合的,但下笔发现确实很耗费时间,为了保证质量,先写一篇单独配置这两个框架,整合到一起的工作留到下次再写。

如果需要项目代码,可以回复留言。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值