---
title: "spring mvc + spring + mybatis环境搭建"
author: "luoping544"
date: "2014-07-10T17:03:29Z"
tags: []
categories: ["企业架构"]
slug: "spring-mvc-spring-mybatis"
draft: false
lastmod: "2014-07-10T17:07:59Z"
id: 2090475
url: "https://luoping544.iteye.com/blog/2090475"
---

**spring mvc + spring + mybatis环境搭建**

**使用工具：**

maven,  eclipse , mysql, druid, hibernate-validate jsr303

 

**整个工程已经上传附件中。**

 

在这记录搭建过程中，遇到的问题。

 

**搭建顺序**

先搭建spring 和 mybatis的集成，然后junit测试通过再进行 spring mvc的集成。

 

**1、spring 和 mybatis的集成**

会用到spring.xml和spring-mybatis.xml

使用的jar主要是

spring-core.jar

mybatis-spring.jar

mytais.jar

 

**2、spring mvc集成**

配置文件spring-mvc.xml,  web.xml

使用的jar：

spring-webmvc.jar

 

**3、注意：web.xml中**

&lt;context-param&gt;  
   &lt;param-name&gt;contextConfigLocation&lt;/param-name&gt;  
   &lt;param-value&gt;classpath:spring.xml,classpath:spring-mybatis.xml&lt;/param-value&gt;  
  &lt;/context-param&gt;

 

&lt;!-- 配置spring mvc --&gt;  
  &lt;servlet&gt;  
   &lt;servlet-name&gt;springMvc&lt;/servlet-name&gt;  
   &lt;servlet-class&gt;org.springframework.web.servlet.DispatcherServlet&lt;/servlet-class&gt;  
   &lt;init-param&gt;  
    &lt;param-name&gt;contextConfigLocation&lt;/param-name&gt;  
    &lt;param-value&gt;classpath:spring-mvc.xml&lt;/param-value&gt;  
   &lt;/init-param&gt;  
   &lt;load-on-startup&gt;1&lt;/load-on-startup&gt;  
  &lt;/servlet&gt;  
  &lt;servlet-mapping&gt;  
   &lt;servlet-name&gt;springMvc&lt;/servlet-name&gt;  
   &lt;url-pattern&gt;/&lt;/url-pattern&gt;  
  &lt;/servlet-mapping&gt;

这两个名字要一致，否则会报莫名的错误。

 

**4、静态文件不在spring管理的配置**

&lt;mvc:annotation-driven /&gt;

&lt;!-- spring mvc对静态资源的访问 --&gt;  
 &lt;mvc:resources location="/image/\*\*" mapping="/image/\*\*" /&gt;  
 &lt;mvc:resources location="/js/\*\*" mapping="/js/\*\*" /&gt;  
 &lt;mvc:resources location="/style/\*\*" mapping="/style/\*\*" /&gt;

 

这样，js，图片等文件才能正常访问。

 

**5、jsp** 引入js方式

&lt;script type="text/javascript" src="&lt;%=request.getContextPath()%&gt;/js/jquery/jquery-1.9.0.min.js"&gt;&lt;/script&gt;