2019-07-12

Maven讲义

[if !supportLists]1. [endif]Maven概述

[if !supportLists]1.1.  [endif]Maven是什么

Maven是一个由Apache基金会维护的项目构建工具。



我们将项目的代码从源代码具体程序文件的过程称为代码构建。

行为包括:编译、测试、运行、打包、部署的过程


[if !supportLists]1.3.  [endif]Eclipse项目构建

Eclipse构建项目的过程图示:


[if !vml]

[endif]



构建过程如下:

[if !supportLists]1)[endif]创建一个Web项目

[if !supportLists]2)[endif]在项目中编写好源代码和配置文件

[if !supportLists]3)[endif]对源代码编译生成class文件

[if !supportLists]4)[endif]通过Junit对代码单元测试

[if !supportLists]5)[endif]将项目通过Eclipse工具放在Tomcat运行

[if !supportLists]6)[endif]将项目导出war,放在Tomcat运行


[if !supportLists]1.4.  [endif]Maven构建项目

通过Maven构建工具可以一条命令完成上面所有的操作:


[if !vml]

[endif]


[if !supportLists]1.    [endif]*clean,清除命令,作用清除已经编译的class文件和war文件

[if !supportLists]2.    [endif]*compile,编译命令,作用是将java文件编译成class文件

[if !supportLists]3.    [endif]*package,打包命令,作用将class文件打成war包

[if !supportLists]4.    [endif]test,测试命令,作用执行Junit工具(可以忽略)

[if !supportLists]5.    [endif]deploy,部署命令,将war包放在指定的tomcat(可以忽略)

[if !supportLists]6.    [endif]*运行Tomcat,通过命令tomcat7:run (要加入Maven工具的Tomcat7插件)

[if !supportLists]7.    [endif]*install,安装命令,一条命令包括了,clean complile package test


[if !supportLists]1.5.  [endif]使用Maven的好处

[if !supportLists]1.    [endif]使用命令,一键快速编译部署

[if !supportLists]2.    [endif]对项目的构建更加精细化,适合大型项目的构建

[if !supportLists]3.    [endif]Maven支持直接通过配置文件(POM)的配置下载依赖的包

[if !supportLists]4.    [endif]各大开源社区强制使用Maven导包,意味着如果不学它,有很多的开源项目不好入门。


[if !supportLists]2. [endif]Maven的安装

[if !supportLists]2.1.  [endif]Maven说明

下载路径:http://maven.apache.org/download.cgi

目录说说明:

[if !vml]

[endif]


[if !supportLists]2.2.  [endif]环境配置

[if !supportLists]2.2.1.  [endif]第一步:确定JAVA_HOME配置

前提:如果要在CMD命令行运行Maven,必须要配置JAVA_HOME环境变量

通过set命令

[if !vml]

[endif]



如果没有配置JAVA_HOME环境会导致运行获得Java运行环境,异常如下:

[if !vml]

[endif]




[if !supportLists]2.2.2.  [endif]第二步:配置MAVEN_HOME环境变量


[if !vml]

[endif]


[if !supportLists]2.2.3.  [endif]第三步:指定Maven执行命令到当前目录

配置执行目录到PATH环境变量,让命令可以在当前目录可以执行

--变量Path变量

[if !vml]

[endif]

--成功增加结果变量:%MAVEN_HOME%\bin


[if !supportLists]2.2.4.  [endif]第四步:测试配置,在CMD下键入mvn -h

如果返回参数提示如下图,说明配置成功!

[if !vml]

[endif]


[if !supportLists]2.2.5.  [endif]第五步:配置Maven国内源

由于默认Maven使用国外的源,会导致下载jar比较慢。

配置Maven下conf文件夹的settings.xml文件


 




  | Specifies a repository mirror site to use instead of a given repository.  The repository that


  | this mirror serves has an ID that matches the mirrorOf element of  this mirror. IDs are used


  | for inheritance and direct lookup purposes, and must be unique  across the set of mirrors.


  |


 


  mirrorId


  repositoryId


  Human Readable Name for this Mirror.


  http://my.repository.com/repo/path




  -->


 


  alimaven


  aliyun maven

      http://maven.aliyun.com/nexus/content/groups/public/


  central       






[if !vml]

[endif]

[if !supportLists]3. [endif]入门配置

需求:通过命令行,使用Maven项目创建一个Java项目,并且打包。


[if !supportLists]3.1.  [endif]第一步:创建项目

使用

命令创建java项目




[if !vml]

[endif]


[if !supportLists]3.2.  [endif]第二步:设置坐标信息

设置坐标信息,通过以下三个信息确定坐标。标记全球Maven项目的唯一性。

groupId:组织ID

artifactId:项目名

package:包名

[if !vml]

[endif]

--提示,创建成功

[if !vml]

[endif]


[if !supportLists]3.3.  [endif]第三步:编译项目

使用命令 mvn compile

[if !vml]

[endif]

--编译成功,生成target文件夹

[if !vml]

[endif]

[if !supportLists]3.4.  [endif]第四步:打包

通过命令mvn package

[if !vml]

[endif]

--打包成功,在target文件夹下会生成jar包

[if !vml]

[endif]


--通过该入门示例,可以发现,使用Maven创建构建项目,是可以不依赖任何开发工具的。


通过该入门示例,我们看到了我们需要学习的内容包括:Maven的命令、仓库


问题:默认Maven仓库在哪里?

答:C:\Users\YL\.m2,在登录用户的个人文件夹里面的.m2文件夹就是仓库


问题:有什么办法了解更多的mvn的命令呢?

答:mvn下的命令都是一个插件,Maven工具内置的可以使用的插件都在其官方帮助文档找到说明。

https://maven.apache.org/plugins/index.html


[if !supportLists]4. [endif]Eclipse使用Maven

[if !supportLists]4.1.  [endif]配置Maven

[if !supportLists]4.1.1.  [endif]第一步:打开Eclipse首选项

[if !vml]

[endif]

[if !supportLists]4.1.2.  [endif]第二步:配置外部Maven

配置指定外部的Maven

[if !vml]

[endif]

--选择使用配置的外部的Maven

[if !vml]

[endif]

[if !supportLists]4.1.3.  [endif]第三步:【可选】查看默认本机仓库

[if !vml]

[endif]



[if !supportLists]4.2.  [endif]通过Maven创建普通项目


[if !supportLists]4.2.1.  [endif]第一步:创建一个Maven项目

[if !vml]

[endif]


[if !supportLists]4.2.2.  [endif]第二步:创建一个自定义的Maven项目

[if !vml]

[endif]


[if !supportLists]4.2.3.  [endif]第三步:设置项目构建信息

GroupId:组编号

ArtifactId:项目标识符(项目的项目名)

注意:Maven是通过GroupId和ArtifactId来确定项目的唯一性,我们称为坐标。任何项目要发布到Maven的库中,必须要有一个全球唯一的坐标。


Version:发布的版本号

Packaging:打包方式。

[if !supportLists](1)[endif]jar:以jar包方式打包,普通java项目

[if !supportLists](2)[endif]war:以war包方式打包,是web项目

[if !supportLists](3)[endif]pom:不打包,表示该项目是一个聚合项目。在多子项目的项目中,用于管理公用Maven构建属性

Name:【可以忽略】就是一个项目的一个名称,实际实战中,一般跟ArtifactID一致。

Description:【可以忽略】就是项目的描述


[if !vml]

[endif]

--创建成功

[if !vml]

[endif]


[if !supportLists]4.2.4.  [endif]第四步:创建一个简单的HelloWorld类

package cn.zj;

public class HelloWorld {


    public static void main(String[] args) {

        System.out.println("HelloWorld");

    }

}


[if !supportLists]4.2.5.  [endif]第五步:构建项目

注意:

[if !supportLists]1.    [endif]Maven build:用于执行Maven的命令

[if !supportLists]2.    [endif]Maven Clean:等同执行 mvn clean

[if !supportLists]3.    [endif]Maven generate-source:等同mvn build

[if !supportLists]4.    [endif]Maven Intall:等同 mvn install 。同时执行,清除、编译、测试、打包、并将包安装到maven仓库

[if !vml]

[endif]

--构建成功

[if !vml]

[endif]


[if !supportLists]4.3.  [endif]通过Maven创建Web项目

[if !supportLists]4.3.1.  [endif]第一步:创建一个Maven项目

[if !vml]

[endif]

--创建项目后,报错信息。提示没有web.xml

解决方案:

[if !supportLists](1)[endif].通过<failOnMissingWebXml>标签忽略web.xml

[if !supportLists](2)[endif].创建一个web.xml文件

[if !vml]

[endif]



[if !supportLists]4.3.2.  [endif]第二步:创建web.xml

[if !supportLists]1.    [endif]创建一个在src/main/webapp下创建WEB-INF文件夹

[if !vml]

[endif]

[if !supportLists]2.      [endif]在WEB-INF文件下创建一个web.xml文件

--通过xsd规则文件创建

[if !vml]

[endif]

--创建成功后,报错消失。!!!!


选中项目右键

[if !vml]

[endif]


[if !supportLists]4.3.3.  [endif]第三步:创建一个index.jsp

[if !vml]

[endif]

发现,报错。原因是没有加入JSP依赖的ServletAPI以及JSP的类库。


[if !supportLists]4.3.4.  [endif]第四步:通过POM.xml加入依赖的类库

--依赖的类库的jar的坐标取哪里找?

答:可以在一下的公有仓库找。

http://mvnrepository.com/


http://search.maven.org/

注意:以后我们开发出来的功能组件希望让别人通过Maven使用,也可以提交到这两个用于库


<projectxmlns="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>cn.zj</groupId>

    <artifactId>maven-demo-02-web</artifactId>

    <version>0.0.1-SNAPSHOT</version>

    <packaging>war</packaging>


    <!-- 增加web项目依赖的类库 -->

    <dependencies>



        <dependency>

            <groupId>javax.servlet</groupId>

            <artifactId>javax.servlet-api</artifactId>

            <version>3.0.1</version>

              <scope>provided</scope>

        </dependency>




        <dependency>

            <groupId>javax.servlet.jsp</groupId>

            <artifactId>javax.servlet.jsp-api</artifactId>

            <version>2.2.1</version>

              <scope>provided</scope>

        </dependency>

    </dependencies>

</project>

注意事项,创建Maven项目是报异常,可以强制更新项目

--右击项目-->选择Maven-->Update Project

[if !vml]

[endif]

--强制更新

[if !vml]

[endif]





[if !supportLists]4.3.5.  [endif]第五步:通过Maven运行Tomcat启动项目

-1.安装tomcat7的Maven插件


所有的Maven工具内置插件都是有在https://maven.apache.org/plugins/index.html找到。

[if !vml]

[endif]


--安装2.2 版本

[if !vml]

[endif]

在pom.xml 配置配置tomcat插件

<!-- 构建项目的相关环境  -->

    <build>

         <plugins>


             <plugin>

                  <groupId>org.apache.tomcat.maven</groupId>

                  <artifactId>tomcat7-maven-plugin</artifactId>

                  <version>2.2</version>

             </plugin>

         </plugins>

    </build>

[if !supportLists]4.3.6.  [endif]使用Tomcat插件运行Maven项目

点击项目鼠标右键

[if !vml]

[endif]

[if !supportLists]4.3.7.  [endif]Maven下面Tomcat插件 项目细节配置

Tomvat 默认项目上下文路径是项目名称,默认端口是 8080

开发者可以手动配置插件信息

    <build>

         <!-- 插件 -->

         <plugins>


             <plugin>

                  <groupId>org.apache.tomcat.maven</groupId>

                  <artifactId>tomcat7-maven-plugin</artifactId>

                  <version>2.2</version>

                  <!-- 细节配置 -->

                  <configuration>

                      <!-- 上下文路径 -->

                      <path>/maven</path>

                      <!-- 端口配置 -->

                      <port>80</port>

                  </configuration>

             </plugin>

         </plugins>

    </build>

[if !supportLists]5. [endif]修改jre 依赖版本

Maven 默认依赖的jar版本是 1.5,开发者可以配置jre版本,有两种配置方式


[if !supportLists]5.1.  [endif]单独为某一个项目配置

<build>

    <!-- 插件 -->

    <plugins>


         <plugin>

             <groupId>org.apache.tomcat.maven</groupId>

             <artifactId>tomcat7-maven-plugin</artifactId>

             <version>2.2</version>

             <!-- 细节配置 -->

             <configuration>

                  <!-- 上下文路径 -->

                  <path>/maven</path>

                  <!-- 端口配置 -->

                  <port>80</port>

             </configuration>

         </plugin>

         <plugin>

             <groupId>org.apache.maven.plugins</groupId>

             <artifactId>maven-compiler-plugin</artifactId>

             <configuration>

                  <!-- 源代码版本 -->

                  <target>1.8</target>

                  <!-- 编译后版本  -->

                  <source>1.8</source>

             </configuration>

         </plugin>

    </plugins>

</build>


[if !supportLists]5.2.  [endif]修改setting.xml配置文件

可以修改 maven根/confg/setting.xml 在 <profiles> 标签中添加如下代码,使用此种方式全局生效

以后创建的项目全部都是1.8版本

<profile>

 <id>jdk-1.8</id>

 <activation>

  <activeByDefault>true</activeByDefault>

  <jdk>1.8</jdk>

 </activation>

 <properties>

  <maven.compiler.source>1.8</maven.compiler.source>

  <maven.compiler.target>1.8</maven.compiler.target>

  <maven.compiler.compilerVersion>1.8</maven.compiler.compilerVersion>

 </properties>

</profile>



[if !supportLists]6. [endif]通过Maven创建多个工程组成的项目

需求:使用Maven项目创建一个ssm整合的项目。项目的每一层为一个工程。


[if !supportLists]6.1.  [endif]第一步:创建Maven项目

注意:

[if !supportLists]1.    [endif]表示层是WEB项目,其他的都是功能组件,所以使用普通的Jar项目

[if !supportLists]2.    [endif]Parent项目是一个聚合项目,主要用于维护统一的包依赖,统一的插件,统一构建项目(测试、编译、打包)

[if !supportLists]6.1.1.  [endif]创建parent-项目

[if !vml]

[endif]

[if !supportLists]6.1.2.  [endif]创建聚合项目的子模块项目

[if !vml]

[endif]


[if !supportLists]6.1.3.  [endif]最终效果

[if !vml]

[endif]


[if !supportLists]6.2.  [endif]第二步:Eclipse实现项目分组

[if !supportLists](1)   [endif].将项目列表使用Working Sets显示

[if !vml]

[endif]


[if !supportLists](2)   [endif].插件一个项目分组

[if !vml]

[endif]


[if !supportLists](3)[endif]设置项目组信息

[if !vml]

[endif]

--分组后结果

[if !vml]

[endif]


[if !supportLists]6.3.  [endif]第三步:聚合项目后的效果

--聚合所有需要构建的工程,可以实现统一构建项目。所谓的统一构建项目,就是统一的执行清除、测试、编译、打包等操作

--在聚合项目ssm-parent通过 <modules>实现聚合所有的需要统一构建的项目

[if !vml]

[endif]


--代码

<projectxmlns="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>cn.zj</groupId>


  <artifactId>ssm-parent</artifactId>


  <version>0.0.1-SNAPSHOT</version>


  <packaging>pom</packaging>


  <description>这是一个聚合项目,本身至于维护其它子项目的包关系插件等</description>




  <!-- 通过引入模块的形式,将项目的其他模块引入在一起 -->


  <modules>


  <module>ssm-mapper</module>


  <module>ssm-service</module>


  <module>ssm-pojo</module>


  <module>ssm-web</module>


  <module>ssm-util</module>


  </modules>

</project>


[if !supportLists]6.4.  [endif]第四步:项目的继承

注意:所谓的项目继承,就是一个项目已经在pom.xml声明的各种元素,继承的子工程的pom.xml也获得该父工程的pom.xml所有声明的元素。


前提:以前如果每个工程需要使用Log4J和Junit,都要在各自的项目的pom.xml加入依赖。这样做他麻烦了。

需求:在ssm-parent声明一次依赖Log4J和Junit,其他所有的子工程都可以使用。


[if !supportLists](1)[endif]在父包加入了依赖

[if !vml]

[endif]


[if !supportLists](2)[endif]在子项目继承父工程。也获得了父工程声明的元素

[if !vml]

[endif]


[if !supportLists]6.5.  [endif]第五步:依赖包的版本统一管理

需求:实现所有项目依赖的类库的版本统一管理。


答:可以通过属性的设置与版本锁定,使用依赖包的统一管理

[if !supportLists](1)[endif]父项目,设置属性与版本锁定

<projectxmlns="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>cn.zj.parent</groupId>

    <artifactId>ssm-parent</artifactId>

    <version>0.0.1-SNAPSHOT</version>

    <packaging>pom</packaging>

    <description>这是一个聚合项目,本身至于维护其它子项目的包关系插件等</description>


    <!-- 通过属性设置类库版本 -->

    <!-- 在properties标签声明的属性,可以在作用范围内的pom.xml里面使用${标签名}访问 -->

    <properties>

        <!-- 注意:pom里面的属性值,标签名是自定义的,随便写,只有符合xml标签规范 -->

        <log4j.version>1.2.17</log4j.version>

        <junit.version>4.12</junit.version>

        <springmvc.version>4.3.16.RELEASE</springmvc.version>

    </properties>

    <!-- 声明依赖 -->

    <dependencies>

        <dependency>

            <groupId>log4j</groupId>

            <artifactId>log4j</artifactId>

            <version>${log4j.version}</version>

            <!-- 打包的时候需要一起进去 -->

            <scope>compile</scope>

        </dependency>

        <dependency>

            <groupId>junit</groupId>

            <artifactId>junit</artifactId>

            <version>${junit.version}</version>


            <scope>test</scope>

        </dependency>

    </dependencies>


    <!-- 指定可以选择的依赖库的版本 -->

    <!-- 放在dependencyManagement标签里面的依赖库,并没有被使用,只是声明了,可选.版本锁定

     这个意味着,如果在作用范围内(本pom.xml以及子pom.xml)里面使用到dependencyManagement标签

     指定版本的类库,必须在在指定版本

     -->

   

       

           

                org.springframework

                spring-webmvc

                ${springmvc.version}



    </dependencyManagement>

    <!-- 通过模块的方式,将所有统一构建的项目聚合起来 -->

    <modules>

        <module>ssm-web</module>

        <module>ssm-service</module>

        <module>ssm-mapper</module>

        <module>ssm-pojo</module>

        <module>ssm-utils</module>

    </modules>

</project>


[if !supportLists](2)[endif]子项目,不需要再指定版本锁定的类库的版本

<projectxmlns="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>cn.zj.controller</groupId>


  <artifactId>ssm-web</artifactId>



  <packaging>war</packaging>




  <!-- 继承ssm parent项目 -->


  <parent>

    <groupId>cn.zj.parent</groupId>

    <artifactId>ssm-parent</artifactId>

    <!-- 注意:如果一个多工程的项目,继承了统一的父项目,版本号以父项目为准 -->

    <version>0.0.1-SNAPSHOT</version>



  </parent>






  <dependencies>

 

    org.springframework

    spring-webmvc




  </dependencies>



</project>

[if !supportLists]6.5.1.  [endif]依赖<scope>的范围

Scope 声明依赖包在哪个阶段有效

Compile(默认)  spring,mybatis

编译(compile)时需要测试时需要,,运行时需要,打包时需要

Provided  jsp-api.jar   servlet-api.jar

编译(compile)时需要,测试(test)时也需要,运行时不需要,打包时不需要


Runtime   数据库驱动包

编译时不需要,测试时需要,,运行时需要,打包时需要

Test  junit.jar

编译时不需要,测试时需要,运行时不需要,打包也不需要



[if !supportLists]6.6.  [endif]第六步:在ssm-web工程配置SpringMVC框架

[if !supportLists]6.6.1.  [endif]导入包依赖

--在ssm-parent对依赖包版本锁定

<projectxmlns="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>cn.zj.parent</groupId>

    <artifactId>ssm-parent</artifactId>

    <version>0.0.1-SNAPSHOT</version>

    <packaging>pom</packaging>

    <description>这是一个聚合项目,本身至于维护其它子项目的包关系插件等</description>


    <!-- 通过属性设置类库版本 -->

    <!-- 在properties标签声明的属性,可以在作用范围内的pom.xml里面使用${标签名}访问 -->

    <properties>

        <!-- 注意:pom里面的属性值,标签名是自定义的,随便写,只有符合xml标签规范 -->

        <log4j.version>1.2.17</log4j.version>

        <junit.version>4.12</junit.version>

        <springmvc.version>4.3.16.RELEASE</springmvc.version>

        <servlet-api.version>3.0.1</servlet-api.version>

        <jsp.version>2.2.1</jsp.version>

    </properties>



    <!-- 声明依赖 -->


    <dependencies>

        <dependency>

            <groupId>log4j</groupId>

            <artifactId>log4j</artifactId>

            <version>${log4j.version}</version>

            <!-- 打包的时候需要一起进去 -->

            <scope>compile</scope>

        </dependency>



        <dependency>

            <groupId>junit</groupId>

            <artifactId>junit</artifactId>

            <version>${junit.version}</version>


            <scope>test</scope>

        </dependency>



    </dependencies>


    <!-- 指定可以选择的依赖库的版本 -->

    <!-- 放在dependencyManagement标签里面的依赖库,并没有被使用,只是声明了,可选.版本锁定 这个意味着,如果在作用范围内(本pom.xml以及子pom.xml)里面使用到dependencyManagement标签

        指定版本的类库,必须在在指定版本 -->

    <dependencyManagement>

       

           

                org.springframework

                spring-webmvc

                ${springmvc.version}



           

                javax.servlet

                javax.servlet-api

                ${servlet-api.version}

                provided



           

                javax.servlet.jsp

                javax.servlet.jsp-api

                ${jsp.version}

                provided





    </dependencyManagement>


    <!-- 设置构建是的jdk的版本 -->

    <build>

        <!-- 插件就是Maven命令 -->

        <plugins>

            <!-- 修改编译插件的JDK版本 -->

            <plugin>

                <groupId>org.apache.maven.plugins</groupId>

                <artifactId>maven-compiler-plugin</artifactId>

                <version>3.5</version>

                <!-- 配置属性 -->

                <configuration>

                   <!-- 源代码使用JDK1.8 -->

                   <source>1.8</source>

                   <!-- 生成的目标文件.class文件使用JDK1.8 -->

                   <target>1.8</target>

                </configuration>

            </plugin>

        </plugins>

    </build>


    <!-- 通过模块的方式,将所有统一构建的项目聚合起来 -->


    <modules>

        <module>ssm-web</module>

        <module>ssm-service</module>

        <module>ssm-mapper</module>

        <module>ssm-pojo</module>

        <module>ssm-utils</module>


    </modules>

</project>


--在ssm-web的pom.xml导入依赖包

<projectxmlns="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>cn.zj.controller</groupId>

    <artifactId>ssm-web</artifactId>


    <packaging>war</packaging>


    <!-- 继承ssm parent项目 -->

    <parent>

        <groupId>cn.zj.parent</groupId>

        <artifactId>ssm-parent</artifactId>

        <!-- 注意:如果一个多工程的项目,继承了统一的父项目,版本号以父项目为准 -->

        <version>0.0.1-SNAPSHOT</version>

        <!-- 指定父工厂的pom.xml相对本pom.xml的相对路径 -->


    </parent>


    <!-- 增加依赖包 -->


   


       

            org.springframework

            spring-webmvc





       

            javax.servlet

            javax.servlet-api




       

            javax.servlet.jsp

            javax.servlet.jsp-api




    <!-- 运行tocmat7插件 -->

    <build>

        <plugins>


            <plugin>

                <groupId>org.apache.tomcat.maven</groupId>

                <artifactId>tomcat7-maven-plugin</artifactId>

                <version>2.2</version>

            </plugin>

        </plugins>

    </build>


</project>



[if !supportLists]6.6.2.  [endif]创建一个请求页面

<%@pagelanguage="java"contentType="text/html;

  charset=UTF-8"

    pageEncoding="UTF-8"%>

<html>

<head>

<metahttp-equiv="Content-Type"content="text/html;

  charset=UTF-8">

<title>Insert title here</title>

</head>

<body>


   <ahref="${pageContext.request.contextPath

  }/user/add.do">add</a>

</body>

</html>


[if !supportLists]6.6.3.  [endif]编写web.xml

<?xmlversion="1.0"encoding="UTF-8"?>

<web-appxmlns: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_2_5.xsd"

    id="WebApp_ID"version="2.5">


    <!-- 配置SpringMVC的前端控制器(总控) -->

    <servlet>

         <servlet-name>MVC</servlet-name>

         <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>


         <!-- 读取Spring相关配置文件 -->

         <init-param>

             <param-name>contextConfigLocation</param-name>

             <!-- 因为spring相关配置文件可能有多个,所有使用*号通配符全部扫描  -->

             <param-value>classpath:applicationContext*.xml</param-value>

         </init-param>


         <!-- 初始化容器 -->

         <load-on-startup>1</load-on-startup>


    </servlet>

    <servlet-mapping>

         <servlet-name>MVC</servlet-name>

         <url-pattern>*.do</url-pattern>

    </servlet-mapping>


</web-app>


[if !supportLists]6.6.4.  [endif]编写配置文件spring-mvc.xml

 <!-- 配置包扫描 -->

    <context:component-scanbase-package="cn.zj.ssm"/>

<!-- 开启注解驱动 -->

    <mvc:annotation-driven/>

    <!-- 配置视图解析器 -->

    <bean

         class="org.springframework.web.servlet.view.InternalResourceViewResolver">

         <!-- 配置视图跳转的前缀  -->

         <propertyname="prefix"value="/WEB-INF/views/"/>

         <!-- 配置视图跳转的后缀  -->

         <propertyname="suffix"value=".jsp"/>

    </bean>



[if !supportLists]6.6.5.  [endif]编写Controller组件类

package cn.zj.controller;


import org.springframework.context.annotation.Scope;

import org.springframework.stereotype.Controller;

import org.springframework.web.bind.annotation.RequestMapping;


@Controller

@RequestMapping(value="/user")

public class  StudentController {


    @RequestMapping(value="/add")

    public void add(){

        System.out.println("-增加用户-");

    }


}


[if !supportLists]6.7.  [endif]第七步:在ssm-mapper工程配置Mybatis框架

[if !supportLists]6.7.1.  [endif]导入包依赖

--ssm-mapper项目导入依赖

<projectxmlns="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>cn.zj.mapper</groupId>


  <artifactId>ssm-mapper</artifactId>


  <parent>


    <groupId>cn.zj.parent</groupId>


    <artifactId>ssm-parent</artifactId>


    <version>0.0.1-SNAPSHOT</version>




  </parent>




  <dependencies>




     <dependency>

        <groupId>mysql</groupId>

        <artifactId>mysql-connector-java</artifactId>

     </dependency>




     <dependency>

        <groupId>org.mybatis</groupId>

        <artifactId>mybatis</artifactId>

     </dependency>



     <dependency>

        <groupId>cn.zj.pojo</groupId>

        <artifactId>ssm-pojo</artifactId>

        <version>0.0.1-SNAPSHOT</version>

     </dependency>



     <dependency>

        <groupId>com.alibaba</groupId>

        <artifactId>druid</artifactId>

     </dependency>



  </dependencies>




</project>

[if !supportLists]6.7.2.  [endif]创建操作映射注解

--创建一个操作接口,编写一个插入数据的SQL语句

package cn.zj.mapper;


import org.apache.ibatis.annotations.Insert;

import org.apache.ibatis.annotations.Options;


import cn.zj.pojo.User;


public interface StudentMapper

  {


    @Insert(value="INSERT  INTO t_user  (name, phone, email)   VALUES (#{name}, #{phone}, #{email})")

    @Options(useGeneratedKeys=true,keyProperty="id",keyColumn="id")

    int insert(User user);


}


[if !supportLists]6.7.3.  [endif]编写mybatis-config.xml配置文件


在test/resources创建mybatis-config.xml配置文件

[if !vml]

[endif]


--配置文件代码

<?xmlversion="1.0"encoding="UTF-8"?>

<!DOCTYPEconfigurationPUBLIC"-//mybatis.org//DTD

  Config 3.0//EN""mybatis-3-config.dtd">

<configuration>



  <environmentsdefault="mysql">

     <environmentid="mysql">

       <transactionManagertype="JDBC"></transactionManager>

       <dataSourcetype="POOLED">

          <propertyname="driver"value="com.mysql.jdbc.Driver"/>

          <propertyname="url"value="jdbc:mysql://localhost:3306/sms"/>

          <propertyname="username"value="root"/>

          <propertyname="password"value="admin"/>

       </dataSource>

     </environment>


  </environments>


  <!-- 加载映射接口 -->


  <mappers>

      <mapperclass="cn.zj.mapper.UserMapper"/>


  </mappers>

</configuration>



[if !supportLists]6.7.4.  [endif]测试代码

package cn.zj.test;

import java.io.IOException;

import java.io.Reader;


import org.apache.ibatis.io.Resources;

import org.apache.ibatis.session.SqlSession;

import org.apache.ibatis.session.SqlSessionFactory;

import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import org.junit.Test;


import cn.zj.mapper.UserMapper;

import cn.zj.pojo.User;


public class UserMapperTest

  {


    @Test

    public void insert(){



        try {

            //1.获得配置文件

            Readerreader= Resources.getResourceAsReader("mybaits-config.xml");

            //2.通过SqlSessionFactoryBuilder获得SqlSessionFactory对象

            SqlSessionFactoryBuilderbuild=new  SqlSessionFactoryBuilder();

            //获得会话工厂

            SqlSessionFactorysessionFactory = build.build(reader);

            //获得会话

            SqlSessionsession = sessionFactory.openSession();

            //获得操作接口的动态对象

            UserMappermapper = session.getMapper(UserMapper.class);

            Useruser=new User();

            user.setName("王五");

            int count = mapper.insert(user);

            System.out.println(count);

            //提交

            session.commit();

            session.close();

            System.out.println(user.getStuId());

        }catch (IOException e) {

            // TODO Auto-generated catch block

            e.printStackTrace();

        }

    }

}



[if !supportLists]6.8.  [endif]第八步:整合SpringMVC与Mybatis


[if !supportLists]6.8.1.  [endif]配置支持支持${key}获得Properties文件值

<!-- 读取db.properties数据库配置文件 -->

<context:property-placeholderlocation="classpath:db.properties"/>


<!-- 配置Druid连接池 -->

<beanid="dataSource"class="com.alibaba.druid.pool.DruidDataSource"

    init-method="init"destroy-method="close">

    <!-- setter方法注入属性值 -->

    <propertyname="driverClassName"value="${jdbc.driverClassName}"/>

    <propertyname="url"value="${jdbc.url}"/>

    <propertyname="username"value="${jdbc.username}"/>

    <propertyname="password"value="${jdbc.password}"/>

    <!-- 最大连接数 -->

    <propertyname="maxActive"value="${jdbc.maxActive}"/>

</bean>


[if !supportLists]6.8.2.  [endif]整合Mybatis

<!-- 配置MyBatis的 SqlSessionFactory对象 -->

    <beanid="sqlSessionFactory"class="org.mybatis.spring.SqlSessionFactoryBean">

         <!-- 1. 注入数据源:连接池 -->

         <propertyname="dataSource"ref="dataSource"/>


         <!-- 2.使用包扫描配置别名  -->

         <propertyname="typeAliasesPackage"value="cn.zj.ssm.pojo"/>


         <!-- 3.引入MyBatis配置文件mybatis-confg.xml -->

         <propertyname="configLocation"value="classpath:mybatis-config.xml"/>


         <!-- 4.配置映射文件 -->

         <propertyname="mapperLocations"value="classpath:cn/zj/ssm/dao/*Mapper.xml"></property>

    </bean>

    <!-- 使用包扫描配置Mapper接口对应的代理对象

         对应包下面的所有的接口全部会被创建代理对象, 默认都是 接口名的首字母小写 作为  名称 UserMapper->userMapper

     -->

     <beanclass="org.mybatis.spring.mapper.MapperScannerConfigurer">

         <!-- 配置SqlSessionfactory的bean对应的名称(不是对象)

  -->

         <propertyname="sqlSessionFactoryBeanName"value="sqlSessionFactory"/>


         <!-- 配置需要扫描创建代理对象的包  -->

         <propertyname="basePackage"value="cn.zj.ssm.dao"/>

     </bean>


[if !supportLists]6.8.3.  [endif]配置事物管理器

<!-- 配置MyBatis事物管理器 -->

    <beanid="txManager"class="org.springframework.jdbc.datasource.DataSourceTransactionManager">

         <!-- 1. 注入数据源:连接池 -->

         <propertyname="dataSource"ref="dataSource"/>

    </bean>



    <!-- 配置事务 通知(增强)tx-->

    <tx:adviceid="txAdvice"transaction-manager="txManager">

         <!-- 配置事务方法 -->

         <tx:attributes>

             <tx:methodname="find*"read-only="true"/>

             <tx:methodname="get*"read-only="true"/>

             <tx:methodname="query*"read-only="true"/>

             <tx:methodname="select*"read-only="true"/>

             <tx:methodname="list*"read-only="true"/>

             <!-- 需要事务的方法 -->

             <tx:methodname="*"/>

         </tx:attributes>

    </tx:advice>


    <!-- 使用aop切事务 -->

    <aop:config>

         <!-- 配置切入点 -->

         <aop:pointcutexpression="execution

  (* cn.zj.ssm.service..*.*(..))"id="pt"/>


         <!-- 配置切面 = 切入点+ 通知 -->

         <aop:advisoradvice-ref="txAdvice"pointcut-ref="pt"/>

         <!-- 织入: spring完成 -->

    </aop:config>


[if !supportLists]7. [endif]常见问题

问题1:已经下载到本地仓库的jar。Eclipse如何快速加入到Pom.xml文件里面

答:进入POM.xml对应的标签位置,右击选择增加依赖或者插件。

--选择增加的依赖

[if !vml]

[endif]

--选择需要的依赖

[if !vml]

[endif]



问题2:本地明明已经下载了对应的插件或者jar包。但Eclipse工具搜索不出来.

答:原因是缓冲问题。可以清一下仓库索引,操作步骤

--打开Maven仓库管理窗口

[if !vml]

[endif]

--刷新索引缓存

[if !vml]

[endif]


问题3:如何设置默认的JDK版本

    <!-- 设置构建是的jdk的版本 -->

    <build>

        <!-- 插件就是Maven命令-->

        <plugins>

            <!-- 修改编译插件的JDK版本 -->

            <plugin>

                <groupId>org.apache.maven.plugins</groupId>

                <artifactId>maven-compiler-plugin</artifactId>

                <version>3.5</version>

                <!-- 配置属性 -->

                <configuration>

                   <!-- 源代码使用JDK1.8 -->

                   <source>1.8</source>

                   <!-- 生成的目标文件.class文件使用JDK1.8 -->

                   <target>1.8</target>

                </configuration>

            </plugin>

        </plugins>

    </build>


问题4:设置打包安装跳过Junit的测试

        <!-- 跳过测试代码 -->

            <plugin>

                <groupId>org.apache.maven.plugins</groupId>

                <artifactId>maven-surefire-plugin</artifactId>

                <version>2.18.1</version>

                <configuration>

                  <skipTests>true</skipTests>

                </configuration>

            </plugin>



[if !supportLists]8. [endif]小结

Maven是什么?

Maven是一个项目构建工具

[if !supportLists]1,[endif]编译--测试-打包-安装完全使用Maven即可完成

[if !supportLists]2,[endif]Maven完全负责项目依赖包管理

[if !supportLists]3,[endif]Maven内置集成Tomcat插件


Maven相对手动构建项目优势

[if !supportLists]1. [endif]Maven有依赖仓库,基本上拥有市面上所有的jar包

[if !supportLists](1)      [endif]更加方便jar包版本管理

[if !supportLists]2. [endif]互联网各大开源的项目基本都是使用Maven项目构建

[if !supportLists]3. [endif]后面会讲解SpringBoot(必须使用Maven)

©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 195,898评论 5 462
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 82,401评论 2 373
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 143,058评论 0 325
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 52,539评论 1 267
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 61,382评论 5 358
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 46,319评论 1 273
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 36,706评论 3 386
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 35,370评论 0 254
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 39,664评论 1 294
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 34,715评论 2 312
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 36,476评论 1 326
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 32,326评论 3 313
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 37,730评论 3 299
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 29,003评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 30,275评论 1 251
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 41,683评论 2 342
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 40,877评论 2 335

推荐阅读更多精彩内容

  • 对于java中的思考的方向,1必须要看前端的页面,对于前端的页面基本的逻辑,如果能理解最好,不理解也要知道几点。 ...
    神尤鲁道夫阅读 782评论 0 0
  • 第一部分 HTML&CSS整理答案 1. 什么是HTML5? 答:HTML5是最新的HTML标准。 注意:讲述HT...
    kismetajun阅读 27,347评论 1 45
  • 续上:HTML的讲解: 第十三节:HTML5介绍 13.1发展历史图 1超文本标记语言(第一版)——在1993年6...
    袁小胜阅读 342评论 0 0
  • 第三 那天人们又想起了被唐风美食支配的恐惧,而这一切的起始,源于我被巴顿船长的一顿暴击……! (摘录自迈克尔语录)...
    温万春阅读 252评论 0 0
  • 事情并不是我认真我就输了,这是一直以来的一种误解,事实是我认真了你就输了,毕竟认真起来的我连自己都怕,毕竟认真的我...
    Skylt阅读 58评论 0 0