一、安装java环境(略)

    这方面资料很多

    我的环境是JDK6+eclipse4 j2ee版,自带Maven等一些插件

二、安装Maven(略)

    Maven安装简单装好后修改 根目录/conf/settings.xml,配置好maven库目录

    maven搭建 Maven3.0+Spring MVC4+Spring 4+Mybatis3+junit4_第1张图片

    打开eclipse windows-preference-maven-userSettings-global settings选择maven安装目录的settings.xml文件

    点击maven在主配置上勾选,这时maven会下载index可能会很慢。需要等等

    maven搭建 Maven3.0+Spring MVC4+Spring 4+Mybatis3+junit4_第2张图片

三、创建maven工程

    点击Eclipse菜单栏File->New->Ohter->Maven->选择项目目录->next->选择项目类型

    maven搭建 Maven3.0+Spring MVC4+Spring 4+Mybatis3+junit4_第3张图片

maven搭建 Maven3.0+Spring MVC4+Spring 4+Mybatis3+junit4_第4张图片

    搜索web,创建项目->next

maven搭建 Maven3.0+Spring MVC4+Spring 4+Mybatis3+junit4_第5张图片

填写groupId和artifact Id->finish

maven搭建 Maven3.0+Spring MVC4+Spring 4+Mybatis3+junit4_第6张图片

项目配置

右击项目-new

maven搭建 Maven3.0+Spring MVC4+Spring 4+Mybatis3+junit4_第7张图片

创建如下几个文件

maven搭建 Maven3.0+Spring MVC4+Spring 4+Mybatis3+junit4_第8张图片

配置build path

maven搭建 Maven3.0+Spring MVC4+Spring 4+Mybatis3+junit4_第9张图片

分别修改输出路径为

src/main/resources        对应        target/classes

src/main/java        对应        target/classes

src/test/resources        对应        target/test-classes

src/test/java        对应        target/test-classes

maven搭建 Maven3.0+Spring MVC4+Spring 4+Mybatis3+junit4_第10张图片

设置JDK版本

maven搭建 Maven3.0+Spring MVC4+Spring 4+Mybatis3+junit4_第11张图片

maven搭建 Maven3.0+Spring MVC4+Spring 4+Mybatis3+junit4_第12张图片

设置部署程序集(Web Deployment Assembly)

maven搭建 Maven3.0+Spring MVC4+Spring 4+Mybatis3+junit4_第13张图片    

把两个test目录去掉!!!

到此项目算是部署完成了。如果有其他问题百度一下吧!!

三 spring mvc+spring+mybatis配置

首先pom.xml文件内容

< project  xmlns = "http://maven.apache.org/POM/4.0.0"  xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance"
   xsi:schemaLocation = "http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd" >
   < modelVersion >4.0.0 modelVersion >
   < groupId >com.xxl.maven_web groupId >
   < artifactId >xxl_maven_web artifactId >
   < packaging >war packaging >
   < version >0.0.1-SNAPSHOT version >
   < name >xxl_maven_web Maven Webapp name >
   < url >http://maven.apache.org url >
   < build >
     < finalName >xxl_maven_web finalName >
   build >
   < dependencies >
       < dependency >
           < groupId >org.springframework groupId >
           < artifactId >spring-web artifactId >
           < version >4.0.6.RELEASE version >
       dependency >
       < dependency >
           < groupId >junit groupId >
           < artifactId >junit artifactId >
           < version >4.11 version >
           < scope >test scope >
       dependency >
       < dependency >
           < groupId >javax.servlet groupId >
           < artifactId >servlet-api artifactId >
           < version >2.5 version >
       dependency >
       < dependency >
           < groupId >org.springframework groupId >
           < artifactId >spring-aspects artifactId >
           < version >4.0.6.RELEASE version >
       dependency >
       < dependency >
           < groupId >org.springframework groupId >
           < artifactId >spring-webmvc artifactId >
           < version >4.0.6.RELEASE version >
       dependency >
       < dependency >
           < groupId >org.springframework groupId >
           < artifactId >spring-context-support artifactId >
           < version >4.0.6.RELEASE version >
       dependency >
       < dependency >
           < groupId >org.freemarker groupId >
           < artifactId >freemarker artifactId >
           < version >2.3.20 version >
       dependency >
       < dependency >
           < groupId >commons-logging groupId >
           < artifactId >commons-logging artifactId >
           < version >1.2 version >
       dependency >
       < dependency >
           < groupId >commons-lang groupId >
           < artifactId >commons-lang artifactId >
           < version >2.6 version >
       dependency >
       < dependency >
           < groupId >org.springframework groupId >
           < artifactId >spring-mock artifactId >
           < version >2.0.8 version >
           < scope >test scope >
       dependency >
       < dependency >
           < groupId >log4j groupId >
           < artifactId >log4j artifactId >
           < version >1.2.14 version >
       dependency >
       < dependency >
           < groupId >org.mybatis groupId >
           < artifactId >mybatis artifactId >
           < version >3.2.7 version >
       dependency >
       < dependency >
           < groupId >org.mybatis groupId >
           < artifactId >mybatis-spring artifactId >
           < version >1.2.2 version >
       dependency >
       < dependency >
           < groupId >mysql groupId >
           < artifactId >mysql-connector-java artifactId >
           < version >5.1.32 version >
       dependency >
       < dependency >
           < groupId >org.springframework groupId >
           < artifactId >spring-tx artifactId >
           < version >4.0.6.RELEASE version >
       dependency >
       < dependency >
           < groupId >org.springframework groupId >
           < artifactId >spring-jdbc artifactId >
           < version >4.0.6.RELEASE version >
       dependency >
       < dependency >
           < groupId >org.apache.commons groupId >
           < artifactId >commons-collections4 artifactId >
           < version >4.0 version >
       dependency >
       < dependency >
           < groupId >commons-dbcp groupId >
           < artifactId >commons-dbcp artifactId >
           < version >1.4 version >
       dependency >
       < dependency >
           < groupId >commons-pool groupId >
           < artifactId >commons-pool artifactId >
           < version >1.6 version >
       dependency >
       < dependency >
           < groupId >org.springframework groupId >
           < artifactId >spring-test artifactId >
           < version >4.0.6.RELEASE version >
           < scope >test scope >
       dependency >
   dependencies >
project >

web.xml

xml  version = "1.0"  encoding = "UTF-8" ?>
< web-app  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" 
          version = "2.5"  >
     
    
     < context-param >
         < param-name >webAppRootKey param-name >
         < param-value >maven.xxl_web.root param-value >
     context-param >
 
    
     < listener >
         < listener-class >org.springframework.web.util.Log4jConfigListener 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 >
     
    
     < context-param >
         < param-name >contextConfigLocation param-name >
         < param-value >classpath:applicationContext-*.xml param-value >
     context-param >
 
    
     < servlet >
         < servlet-name >dispatcher servlet-name >
         < servlet-class >org.springframework.web.servlet.DispatcherServlet servlet-class >
         < init-param >
             < param-name >contextConfigLocation param-name >
             < param-value >/WEB-INF/dispatcher-servlet.xml param-value >
         init-param >
         < load-on-startup >1 load-on-startup >
     servlet >
     < servlet-mapping >
         < servlet-name >dispatcher servlet-name >
         < url-pattern >*.html url-pattern >
        
     servlet-mapping >
     
    
     < listener >
         < listener-class >org.springframework.web.context.ContextLoaderListener listener-class >
     listener >  
     
     
     < welcome-file-list >
         < welcome-file >/login.jsp welcome-file >
     welcome-file-list >
 
web-app >

dispatcher-servlet.xml

xml  version = "1.0"  encoding = "UTF-8" ?>
< beans  xmlns = "http://www.springframework.org/schema/beans" 
        xmlns:aop = "http://www.springframework.org/schema/aop" 
        xmlns:context = "http://www.springframework.org/schema/context"
        xmlns:mvc = "http://www.springframework.org/schema/mvc"
        xmlns:p = "http://www.springframework.org/schema/p"
        xmlns:tx = "http://www.springframework.org/schema/tx" 
        xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://www.springframework.org/schema/aop 
         http://www.springframework.org/schema/aop/spring-aop-3.0.xsd 
         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-3.0.xsd 
         http://www.springframework.org/schema/mvc 
         http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd 
         http://www.springframework.org/schema/tx 
         http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">
 
    
      
     
   
     < context:component-scan  base-package = "com.xxl.app.**.control"  />
     
     < mvc:resources  mapping = "/p_w_picpaths/**"  location = "/p_w_picpaths/"  cache-period = "31556926" />
     < mvc:resources  mapping = "/js/**"  location = "/js/"  cache-period = "31556926" />
     < mvc:resources  mapping = "/css/**"  location = "/css/"  cache-period = "31556926" />
 
     < bean  name = "handlerAdapter"  class = "org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter"  >
        
     bean >
    
     < bean  name = "handlerMapping"  class = "org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping" >
        
     bean >  
       
     < bean  id = "freemarkerConfigurer"  class = "org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer" >
         < property  name = "templateLoaderPath"  value = "/WEB-INF/view/"  />
         < property  name = "defaultEncoding"  value = "UTF-8"  />
         < property  name = "freemarkerSettings" >
             < props >
                 < prop  key = "template_update_delay" >10 prop >
                 < prop  key = "locale" >zh_CN prop >
                 < prop  key = "datetime_format" >yyyy-MM-dd HH:mm:ss prop >
                 < prop  key = "date_format" >yyyy-MM-dd prop >
                 < prop  key = "number_format" >#.## prop >
             props >
         property >
     bean >
       
     < bean  id = "viewResolver"  class = "org.springframework.web.servlet.view.freemarker.FreeMarkerViewResolver" >  
         < property  name = "viewClass"  value = "org.springframework.web.servlet.view.freemarker.FreeMarkerView"  />  
         < property  name = "suffix"  value = ".html"  />  
         < property  name = "contentType"  value = "text/html;charset=UTF-8"  />
         < property  name = "exposeRequestAttributes"  value = "true"  />
         < property  name = "exposeSessionAttributes"  value = "true"  />
         < property  name = "exposeSpringMacroHelpers"  value = "true"  />
     bean >
     
     < bean  id = "messageSource"  class = "org.springframework.context.support.ResourceBundleMessageSource"
         p:basename = "i18n/messages"  />
 
     
    
     
    
     
    
     
    
 
beans >

maven搭建 Maven3.0+Spring MVC4+Spring 4+Mybatis3+junit4_第14张图片

maven搭建 Maven3.0+Spring MVC4+Spring 4+Mybatis3+junit4_第15张图片

applicationContext-bean.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:tx = "http://www.springframework.org/schema/tx"
     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-3.0.xsd
         http://www.springframework.org/schema/tx 
         http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">
         
     < context:component-scan  base-package = "com.xxl.app.**.service"  />
 
    
     < bean  class = "com.xxl.app.base.SpringContextHolder"  lazy-init = "false"  />
     
    
     < tx:annotation-driven  transaction-manager = "transactionManager"  />
       
     < bean  id = "transactionManager"  class = "org.springframework.jdbc.datasource.DataSourceTransactionManager" >  
         < property  name = "dataSource"  ref = "dataSource"  />
     bean >
     
    
     < context:property-placeholder  location = "classpath:ini.properties" />
     
    
     < bean  id = "dataSource"  class = "org.apache.commons.dbcp.BasicDataSource"  destroy-method = "close" >
       < property  name = "driverClassName"  value = "${driver}" />
       < property  name = "url"  value = "${url}" />
       < property  name = "username"  value = "${username}" />
       < property  name = "password"  value = "${password}" />
       < property  name = "maxActive"  value = "${maxActive}" />
       < property  name = "maxIdle"  value = "${maxIdle}" />
       < property  name = "minIdle"  value = "${minIdle}" />
     bean >
     
    
     < bean  id = "sqlSessionFactory"  class = "org.mybatis.spring.SqlSessionFactoryBean" >
         < property  name = "dataSource"  ref = "dataSource" />
         < property  name = "configLocation"  value = "classpath:mybatis.xml" />
        
     bean >
     
    
     < bean  class = "org.mybatis.spring.mapper.MapperScannerConfigurer" >
         < property  name = "basePackage"  value = "com.xxl.app.**.dao" />
         < property  name = "markerInterface"  value = "com.xxl.app.base.dao.SqlMapper" />
     bean >
 
     
beans >

mybaitis.xml

xml  version = "1.0"  encoding = "UTF-8" ?>
< configuration >
    
configuration >

单元测试代码

package  xxl.test.service;
 
import  org.junit.Test;
import  org.junit.runner.RunWith;
import  org.springframework.beans.factory.annotation.Autowired;
import  org.springframework.test.context.ContextConfiguration;
import  org.springframework.test.context.junit4.SpringJUnit4Cla***unner;
import  org.springframework.test.context.transaction.TransactionConfiguration;
import  org.springframework.transaction.annotation.Transactional;
 
import  com.xxl.app.base.service.IIndexService;
 
@ContextConfiguration (locations={ "classpath:applicationContext-bean.xml" })
@RunWith (SpringJUnit4Cla***unner. class )
@Transactional
//如果是true不会改变数据库数据,如果是false会改变数据
@TransactionConfiguration (transactionManager= "transactionManager" ,defaultRollback= true )
public  class  IndexService {
     
     @Autowired
     private  IIndexService indexService;
     
     @Test
     public  void  TestIndex(){
         
         indexService.doIndex();

     }
 
}

http://www.open-open.com/lib/view/open1409641766369.html