Eclipse + Maven + SSM框架整合 超详细的讲解

Eclipse + Maven + SSM框架整合 

一 、前言
用maven我们能做什么,有什么好处?
用maven,我们可以方便统一的进行jar包管理,jar包版本升级,快速项目构建以及分模块开发等相关功能。另外 Maven对于项目目录结构、测试用例命名方式等内容都有既定的规则,比如:不像我们在传统的框架中测试我们需要在classpath下单独创建一个测试的文件目录等。再次,maven还为全世界的Java开发者提供了一个免费的中央仓库,在其中几乎可以找到任何的流行开源软件。
maven使用得好,项目开发速度就会成倍的提升。总之,项目越大,越能体现maven的优势。

二 、软件环境

本次讲解使用的环境如下:

Eclipse 
apache-maven-3.3.9
JDK1.8
apache-tomcat-7.0.79

三、maven安装配置和Eclipse中maven的集成

这里不再详细说明,再次我提供一个参考地址:
https://blog.csdn.net/ailo555/article/details/50099721
maven的安装和配置: https://jingyan.baidu.com/article/4f7d5712a1306c1a21192746.html

四 、Eclipse+Maven创建webapp项目

首先看下一Maven的项目结构:

Eclipse + Maven + SSM框架整合 超详细的讲解_第1张图片

1、开启eclipse,右键new——》other,输入maven 如下图找到maven project

Eclipse + Maven + SSM框架整合 超详细的讲解_第2张图片

2、选择maven project,显示创建maven项目的窗口,勾选如图所示,Create a simple project(skip archetype selection表示跳过骨架,骨架表示一些创建模板),这里我们不使用模板更加简单(这只是其中的一种方式)。

Eclipse + Maven + SSM框架整合 超详细的讲解_第3张图片

3、输入maven项目的基本信息,这里我们要注重前四项,其中Version我们按照默认信息不管,但如果是web工程,在Packaging栏我们要选择war,如下图所示:

Eclipse + Maven + SSM框架整合 超详细的讲解_第4张图片

4、完成maven项目的创建,生成相应的maven项目结果,(但是会有一定的错误,不过不要紧)如下所示:

Eclipse + Maven + SSM框架整合 超详细的讲解_第5张图片

报错是因为webapp目录下没有web.xml。我们随便从其它web工程里面拷贝WEB-INF目录过来即可,完成后如图:

Eclipse + Maven + SSM框架整合 超详细的讲解_第6张图片

你会发现还有一个错误未解决,这是因为jdk版本不一致的问题(默认是1.5的)

5、接下来拷贝下面这段代码到pom.xml里面,用来确定项目的jdk编译版本,如(这里给出代码,可粘贴复制):

< build >
     < plugins >
       < plugin >
         < groupId >org.apache.maven.plugins groupId >
        < artifactId >maven-compiler-plugin artifactId >
        < version >3.5.1 version >
        
          < configuration >
           < source > 1.8 source >
           < target > 1.8 target >
           < encoding > utf-8 encoding >
         configuration >
       plugin >
     plugins >
build >
即:
Eclipse + Maven + SSM框架整合 超详细的讲解_第7张图片

这时候,项目又会报错,我们配置完成后,需要执行一次更新项目配置的动作。选中项目 --> 右键 -->Maven --> Update Project(eclipseluna 版本,helios 或indigo 版本选择 Update Project Configuration)。更新完成之后,错误消失,jdk版本也更改过来了,如图所示:

Eclipse + Maven + SSM框架整合 超详细的讲解_第8张图片

还有另一种方法就是不用在prom.xml添加这些代码,我们需要到响应的配置文件中去改:
如: Windows --> show view --> Navigator 找到如下目录(对着两个文件进行修改)

Eclipse + Maven + SSM框架整合 超详细的讲解_第9张图片

修改后为如图:(修改前这里就不展示了,前面也说了默认是1.5的)
Eclipse + Maven + SSM框架整合 超详细的讲解_第10张图片
Eclipse + Maven + SSM框架整合 超详细的讲解_第11张图片

同样更改完之后,选中项目 --> 右键 -->Maven --> Update Project,错误消失

Eclipse + Maven + SSM框架整合 超详细的讲解_第12张图片

这两种方法相对来说第一种比较简单,我很喜欢第一种

6、 经过前面的五个步骤我们的Maven项目已经搭建好了,接下来就是jar包的导入了; 在pom.xml中添加jar包依赖(这里给出的是SSM的一个配置,相对应的SSH相信大家也会找到可以去Maven中央仓库去找,下边我会介绍)

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/xsd/maven-4.0.0.xsd " >
   < modelVersion > 4.0.0 modelVersion >
   < groupId > com.zxk groupId >
   < artifactId > MavenSSMPrj artifactId >
   < version > 0.0.1-SNAPSHOT version >
   < packaging > war packaging >
   
   < dependencies >
     
      < dependency >
        < groupId > junit groupId >
        < artifactId > junit artifactId >
        < version > 3.8.1 version >
        < scope > test scope >
      dependency >
     
      < dependency >
          < groupId > mysql groupId >
          < artifactId > mysql-connector-java artifactId >
          < version > 5.1.39 version >
      dependency >
          
      < dependency >
          < groupId > org.mybatis groupId >
          < artifactId > mybatis artifactId >
          < version > 3.4.1 version >
      dependency >
     
      < dependency >
          < groupId > org.mybatis groupId >
          < artifactId > mybatis-spring artifactId >
          < version > 1.3.1 version >
      dependency >
     
      < dependency >
          < groupId > org.aspectj groupId >
          < artifactId > aspectjweaver artifactId >
          < version > 1.8.9 version >
      dependency >
      < dependency >
          < groupId > org.springframework groupId >
          < artifactId > spring-aop artifactId >
          < version > 4.3.7.RELEASE version >
      dependency >
      < dependency >
          < groupId > org.springframework groupId >
          < artifactId > spring-core artifactId >
          < version > 4.3.7.RELEASE version >
      dependency >
      < dependency >
          < groupId > org.springframework groupId >
          < artifactId > spring-aspects artifactId >
          < version > 4.3.7.RELEASE version >
      dependency >
      < dependency >
          < groupId > org.springframework groupId >
          < artifactId > spring-beans artifactId >
          < version > 4.3.7.RELEASE version >
      dependency >
      < dependency >
          < groupId > org.springframework groupId >
          < artifactId > spring-context artifactId >
          < version > 4.3.7.RELEASE version >
      dependency >
      < dependency >
          < groupId > org.springframework groupId >
          < artifactId > spring-context-support artifactId >
          < version > 4.3.7.RELEASE version >
      dependency >
      < dependency >
          < groupId > org.springframework groupId >
          < artifactId > spring-expression artifactId >
          < version > 4.3.7.RELEASE version >
      dependency >
      < dependency >
          < groupId > org.springframework groupId >
          < artifactId > spring-instrument artifactId >
          < version > 4.3.7.RELEASE version >
      dependency >
      < dependency >
          < groupId > org.springframework groupId >
          < artifactId > spring-instrument-tomcat artifactId >
          < version > 4.3.7.RELEASE version >
      dependency >
      < dependency >
          < groupId > org.springframework groupId >
          < artifactId > spring-jdbc artifactId >
          < version > 4.3.7.RELEASE version >
      dependency >
      < dependency >
          < groupId > org.springframework groupId >
          < artifactId > spring-jms artifactId >
          < version > 4.3.7.RELEASE version >
      dependency >
      < dependency >
          < groupId > org.springframework groupId >
          < artifactId > spring-messaging artifactId >
          < version > 4.3.7.RELEASE version >
      dependency >
      < dependency >
          < groupId > org.springframework groupId >
          < artifactId > spring-orm artifactId >
          < version > 4.3.7.RELEASE version >
      dependency >
      < dependency >
          < groupId > org.springframework groupId >
          < artifactId > spring-oxm artifactId >
          < version > 4.3.7.RELEASE version >
      dependency >
      < dependency >
          < groupId > org.springframework groupId >
          < artifactId > spring-test artifactId >
          < version > 4.3.7.RELEASE version >
          < scope > test scope >
      dependency >
      < dependency >
          < groupId > org.springframework groupId >
          < artifactId > spring-tx artifactId >
          < version > 4.3.7.RELEASE version >
      dependency >
      < dependency >
          < groupId > org.springframework groupId >
          < artifactId > spring-web artifactId >
          < version > 4.3.7.RELEASE version >
      dependency >
      < dependency >
          < groupId > org.springframework groupId >
          < artifactId > spring-webmvc artifactId >
          < version > 4.3.7.RELEASE version >
      dependency >
      < dependency >
          < groupId > org.springframework groupId >
          < artifactId > spring-webmvc-portlet artifactId >
          < version > 4.3.7.RELEASE version >
      dependency >
      < dependency >
          < groupId > org.springframework groupId >
          < artifactId > spring-websocket artifactId >
          < version > 4.3.7.RELEASE version >
      dependency >   
          
      < dependency >
          < groupId > taglibs groupId >
          < artifactId > standard artifactId >
          < version > 1.1.2 version >
      dependency >
      < dependency >
          < groupId > jstl groupId >
          < artifactId > jstl artifactId >
          < version > 1.2 version >
      dependency >
     
          
      < dependency >
          < groupId > com.mchange groupId >
          < artifactId > c3p0 artifactId >
          < version > 0.9.5.2 version >
      dependency >
      < dependency >
          < groupId > com.mchange groupId >
          < artifactId > mchange-commons-java artifactId >
          < version > 0.2.11 version >
      dependency >
         
         
      < dependency >
          < groupId > log4j groupId >
          < artifactId > log4j artifactId >
          < version > 1.2.17 version >
      dependency >
         
     
      < dependency >
          < groupId > commons-logging groupId >
          < artifactId > commons-logging artifactId >
          < version > 1.2 version >
      dependency >
      < dependency >
          < groupId > commons-io groupId >
          < artifactId > commons-io artifactId >
          < version > 2.4 version >
      dependency >
      < dependency >
          < groupId > commons-fileupload groupId >
          < artifactId > commons-fileupload artifactId >
          < version > 1.3.1 version >
      dependency >
      < dependency >
          < groupId > commons-codec groupId >
          < artifactId > commons-codec artifactId >
          < version > 1.10 version >
      dependency >
      < dependency >
          < groupId > com.fasterxml.jackson.core groupId >
          < artifactId > jackson-core artifactId >
          < version > 2.8.9 version >
      dependency >
      < dependency >
          < groupId > com.fasterxml.jackson.core groupId >
          < artifactId > jackson-databind artifactId >
          < version > 2.8.9 version >
      dependency >
      < dependency >
          < groupId > com.fasterxml.jackson.core groupId >
          < artifactId > jackson-annotations artifactId >
          < version > 2.8.9 version >
      dependency >
   dependencies >  
< build >
     < plugins >
       < plugin >
         < groupId > org.apache.maven.plugins groupId >
         < artifactId > maven-compiler-plugin artifactId >
         < version > 3.5.1 version >
        
          < configuration >
           < source > 1.8 source >
           < target > 1.8 target >
           < encoding > utf-8 encoding >
         configuration >
       plugin >
     plugins >
build >
project >

这里在给出另一种写法(这种写法更方便我们对jar版本的更换)
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/xsd/maven-4.0.0.xsd " >
   < modelVersion > 4.0.0 modelVersion >
   < groupId > com.zxk groupId >
   < artifactId > MavenSSMPrj artifactId >
   < version > 0.0.1-SNAPSHOT version >
   < packaging > war packaging >

      
  
        
    UTF-8
    UTF-8
    
    4.3.8.RELEASE  
    1.2.17  
    5.1.39    
    
    
  junit
  junit
  3.8.1
  test
    mysql
    mysql-connector-java
    ${mysql.version}
      
    org.mybatis
    mybatis
    3.4.1
    org.mybatis
    mybatis-spring
    1.3.1
    org.aspectj
    aspectjweaver
    1.8.9
    org.springframework
    spring-aop
    ${spring.version}
    org.springframework
    spring-core
    ${spring.version}
    org.springframework
    spring-aspects
    ${spring.version}
    org.springframework
    spring-beans
    ${spring.version}
    org.springframework
    spring-context
    ${spring.version}
    org.springframework
    spring-context-support
    ${spring.version}
    org.springframework
    spring-expression
    ${spring.version}
    org.springframework
    spring-instrument
    ${spring.version}
    org.springframework
    spring-instrument-tomcat
    ${spring.version}
    org.springframework
    spring-jdbc
    ${spring.version}
    org.springframework
    spring-jms
    ${spring.version}
    org.springframework
    spring-messaging
    ${spring.version}
    org.springframework
    spring-orm
    ${spring.version}
    org.springframework
    spring-oxm
    ${spring.version}
    org.springframework
    spring-test
    ${spring.version}
    test
    org.springframework
    spring-tx
    ${spring.version}
    org.springframework
    spring-web
    ${spring.version}
    org.springframework
    spring-webmvc
    ${spring.version}
    org.springframework
    spring-webmvc-portlet
    ${spring.version}
    org.springframework
    spring-websocket
    ${spring.version}

      
    taglibs
    standard
    1.1.2
    jstl
    jstl
    1.2

    
    com.mchange
    c3p0
    0.9.5.2
    com.mchange
    mchange-commons-java
    0.2.11
    
    
    log4j
    log4j
    ${log4j.version}
    
    
    commons-logging
    commons-logging
    1.2
    commons-io
    commons-io
    2.4
    commons-fileupload
    commons-fileupload
    1.3.1
    commons-codec
    commons-codec
    1.10
    com.fasterxml.jackson.core
    jackson-core
    2.8.9
    com.fasterxml.jackson.core
    jackson-databind
    2.8.9
    com.fasterxml.jackson.core
    jackson-annotations
    2.8.9
  

< build >
     < plugins >
       < plugin >
         < groupId > org.apache.maven.plugins groupId >
         < artifactId > maven-compiler-plugin artifactId >
         < version > 3.5.1 version >
        
          < configuration >
           < source > 1.8 source >
           < target > 1.8 target >
           < encoding > utf-8 encoding >
         configuration >
       plugin >
     plugins >
build >
project >


如果还需要另外的jar包,我们只需去Maven中央仓库(地址: http://mvnrepository.com/ )去搜索即可,它会给我们提供配置文件我们只需复制相关代码到我们的 pom.xml文件中即可。如:

Eclipse + Maven + SSM框架整合 超详细的讲解_第13张图片


7.接着就开始写我们的相关层次的代码了(我这里直接把我以前整合的ssm代码层次复制上去)

目录结构解释:
Eclipse + Maven + SSM框架整合 超详细的讲解_第14张图片

这里我们会发现一个错误:( The superclass javax.servlet.http.HttpServlet was not found on the Java Build Path ),不用担心很简单,我们只需要给他添加一个Server Runtime运行环境即可;如:

Eclipse + Maven + SSM框架整合 超详细的讲解_第15张图片
Eclipse + Maven + SSM框架整合 超详细的讲解_第16张图片
Eclipse + Maven + SSM框架整合 超详细的讲解_第17张图片
这样错误即可消失

整合后目录结构为:

Eclipse + Maven + SSM框架整合 超详细的讲解_第18张图片

运行测试结果:

Eclipse + Maven + SSM框架整合 超详细的讲解_第19张图片

致此 Maven 整合SSM结束!

你可能感兴趣的:(Maven)