• 从头开始基于Maven搭建SpringMVC+Mybatis项目(2)


    接上文内容,本节介绍Maven的聚合和继承。

    从头阅读传送门

    互联网时代,软件正在变得越来越复杂,开发人员通常会对软件划分模块,以获得清晰的设计、良好的分工及更高的可重用性。Maven的聚合特性能把多个模块聚合在一起构建,并促进各子模块通过继承父模块的pom配置来保持配置的一致。为了演示这些特性,本文的示例划分为数据持久层petstore-persist和petstore-web两个模块。

    下面来介绍创建项目的过程,首先创建父模块perstore-parent。

    File -> New -> Maven Project,简化一下流程,直接勾选创建简单项目,即跳过archetype的选择

    下一步,填写Group Id和Artifact Id,注意作为父模块,打包类型(Packaging)必须选择pom。

    完成后目录结构如下。

    父模块除了构建文件pom.xml外没有实质内容,所以手动删除src目录,仅保留pom.xml文件,删除后目录结构如下:

    接下来创建petstore-persist模块。在上图中petstore-parent上点击右键,选择New -> Project,在向导对话框中选择Maven Module,如下图:

    下一步,这里仍然选择跳过archetype选项,模块名称填写petstore-persist

    直接点击完成(Finish),完成后目录结构如下:

    接下来创建Web子模块,重复“petstore-parent上点击右键,选择New -> Project,在向导对话框中选择Maven Module”步骤,但是不要勾选跳过archetype选择。填写模块名称后点击下一步。

    archetype选择maven-archetype-webapp。

    完成后目录结构如下:

    在我的开发环境下,Maven的向导创建的Web模块版本是2.3,运行时会有一些兼容性问题,首先手动调整为3.0。

    在项目上点击右键 -> Properties,并选择Project Facets菜单。现在选择3.0版本会提示错误,解决办法是先取消Dynamic Web Module选项,如下图:

    点OK关闭对话框,修改petstore-web/src/main/webapps/WEB-INF/web.xml,如下:

    [html] view plain copy
     
     print?在CODE上查看代码片派生到我的代码片
    1. <?xml version="1.0" encoding="UTF-8"?>  
    2. <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
    3.     xmlns="http://java.sun.com/xml/ns/javaee"  
    4.     xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"  
    5.     version="3.0">  
    6.     <display-name>petstore-web</display-name>  
    7. </web-app>  

    从新打开Project Facets界面,勾选Dynamic Web Module选项,并选择版本号为3.0

    然后点击Apply按钮保存设置。

    然后切换到Java Build Path菜单,发现这里提示另一个错误,如下图:

    解决办法是勾选JRE System Library[JavaSE-1.X],点击OK保存设置并关闭对话框,处理完成后项目中增加了一个WebContent目录,这对Maven项目来说是多余的,直接删除。

    现在来解决Web模块上存在的红叉错误提示,查看详细信息,发现是“The superclass "javax.servlet.http.HttpServlet" was not found on the Java Build Path”,说明是缺少编译的jar引用。在聚合结构中,通常在父模块的pom.xml中添加依赖,由其他子模块继承。这里有两种配置方法,第一种和普通项目相同,如下,子项目自动继承这些依赖。

    petstore-parent/pom.xml

    [html] view plain copy
     
     print?在CODE上查看代码片派生到我的代码片
    1. <dependencies>    
    2.     <dependency>    
    3.         <groupId>junit</groupId>    
    4.         <artifactId>junit</artifactId>    
    5.         <version>3.8.1</version>    
    6.         <scope>test</scope>    
    7.     </dependency>    
    8. </dependencies>  

    但这种方式不够灵活,在中大型项目中,通常有非常多的依赖项,项目中即使一个功能非常简单的基础模块也会继承全部依赖,显然是不合理的。所以这里使用第二种配置方式,即使用dependencyManagement元素,它既可以规范各子模块使用的依赖版本,也能允许各子模块按需添加依赖,不过,配置文件代码会更多一些。

    petstore-parent/pom.xml

    [html] view plain copy
     
     print?在CODE上查看代码片派生到我的代码片
    1. <?xml version="1.0" encoding="UTF-8"?>  
    2. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
    3.     xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">  
    4.     <modelVersion>4.0.0</modelVersion>  
    5.     <groupId>com.example</groupId>  
    6.     <artifactId>petstore-parent</artifactId>  
    7.     <version>0.0.1-SNAPSHOT</version>  
    8.     <packaging>pom</packaging>  
    9.     <modules>  
    10.         <module>petstore-persist</module>  
    11.         <module>petstore-web</module>  
    12.     </modules>  
    13.     <properties>  
    14.         <springframework.version>4.2.6.RELEASE</springframework.version>  
    15.     </properties>  
    16.     <dependencyManagement>  
    17.         <dependencies>  
    18.             <dependency>  
    19.                 <groupId>junit</groupId>  
    20.                 <artifactId>junit</artifactId>  
    21.                 <version>4.12</version>  
    22.                 <scope>test</scope>  
    23.             </dependency>  
    24.             <dependency>  
    25.                 <groupId>javax</groupId>  
    26.                 <artifactId>javaee-web-api</artifactId>  
    27.                 <version>7.0</version>  
    28.                 <scope>provided</scope>  
    29.             </dependency>  
    30.             <dependency>  
    31.                 <groupId>log4j</groupId>  
    32.                 <artifactId>log4j</artifactId>  
    33.                 <version>1.2.17</version>  
    34.             </dependency>  
    35.             <dependency>  
    36.                 <groupId>javax.servlet</groupId>  
    37.                 <artifactId>jstl</artifactId>  
    38.                 <version>1.2</version>  
    39.             </dependency>  
    40.             <dependency>  
    41.                 <groupId>org.apache.commons</groupId>  
    42.                 <artifactId>commons-lang3</artifactId>  
    43.                 <version>3.1</version>  
    44.             </dependency>  
    45.             <dependency>  
    46.                 <groupId>org.springframework</groupId>  
    47.                 <artifactId>spring-core</artifactId>  
    48.                 <version>${springframework.version}</version>  
    49.             </dependency>  
    50.             <dependency>  
    51.                 <groupId>org.springframework</groupId>  
    52.                 <artifactId>spring-context</artifactId>  
    53.                 <version>${springframework.version}</version>  
    54.             </dependency>  
    55.             <dependency>  
    56.                 <groupId>org.springframework</groupId>  
    57.                 <artifactId>spring-test</artifactId>  
    58.                 <version>${springframework.version}</version>  
    59.             </dependency>  
    60.             <dependency>  
    61.                 <groupId>org.springframework</groupId>  
    62.                 <artifactId>spring-jdbc</artifactId>  
    63.                 <version>${springframework.version}</version>  
    64.             </dependency>  
    65.             <dependency>  
    66.                 <groupId>org.springframework</groupId>  
    67.                 <artifactId>spring-web</artifactId>  
    68.                 <version>${springframework.version}</version>  
    69.             </dependency>  
    70.             <dependency>  
    71.                 <groupId>org.springframework</groupId>  
    72.                 <artifactId>spring-webmvc</artifactId>  
    73.                 <version>${springframework.version}</version>  
    74.             </dependency>  
    75.             <dependency>  
    76.                 <groupId>org.springframework</groupId>  
    77.                 <artifactId>spring-beans</artifactId>  
    78.                 <version>${springframework.version}</version>  
    79.             </dependency>  
    80.             <dependency>  
    81.                 <groupId>org.hibernate</groupId>  
    82.                 <artifactId>hibernate-validator</artifactId>  
    83.                 <version>5.2.4.Final</version>  
    84.             </dependency>  
    85.             <dependency>  
    86.                 <groupId>com.fasterxml.jackson.core</groupId>  
    87.                 <artifactId>jackson-core</artifactId>  
    88.                 <version>2.7.3</version>  
    89.             </dependency>  
    90.             <dependency>  
    91.                 <groupId>com.fasterxml.jackson.core</groupId>  
    92.                 <artifactId>jackson-databind</artifactId>  
    93.                 <version>2.7.3</version>  
    94.             </dependency>  
    95.             <dependency>  
    96.                 <groupId>c3p0</groupId>  
    97.                 <artifactId>c3p0</artifactId>  
    98.                 <version>0.9.1.2</version>  
    99.             </dependency>  
    100.             <dependency>  
    101.                 <groupId>mysql</groupId>  
    102.                 <artifactId>mysql-connector-java</artifactId>  
    103.                 <version>5.1.18</version>  
    104.             </dependency>  
    105.             <dependency>  
    106.                 <groupId>org.mybatis</groupId>  
    107.                 <artifactId>mybatis</artifactId>  
    108.                 <version>3.4.1</version>  
    109.             </dependency>  
    110.             <dependency>  
    111.                 <groupId>org.mybatis</groupId>  
    112.                 <artifactId>mybatis-spring</artifactId>  
    113.                 <version>1.3.0</version>  
    114.             </dependency>  
    115.         </dependencies>  
    116.     </dependencyManagement>  
    117. </project>  

    petstore-persist/pom.xml

    [html] view plain copy
     
     print?在CODE上查看代码片派生到我的代码片
    1. <?xml version="1.0" encoding="UTF-8"?>  
    2. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
    3.     xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">  
    4.     <modelVersion>4.0.0</modelVersion>  
    5.     <parent>  
    6.         <groupId>com.example</groupId>  
    7.         <artifactId>petstore-parent</artifactId>  
    8.         <version>0.0.1-SNAPSHOT</version>  
    9.     </parent>  
    10.     <artifactId>petstore-persist</artifactId>  
    11.     <dependencies>  
    12.         <dependency>  
    13.             <groupId>junit</groupId>  
    14.             <artifactId>junit</artifactId>  
    15.         </dependency>  
    16.         <dependency>  
    17.             <groupId>org.springframework</groupId>  
    18.             <artifactId>spring-test</artifactId>  
    19.         </dependency>  
    20.         <dependency>  
    21.             <groupId>org.springframework</groupId>  
    22.             <artifactId>spring-core</artifactId>  
    23.         </dependency>  
    24.         <dependency>  
    25.             <groupId>org.springframework</groupId>  
    26.             <artifactId>spring-context</artifactId>  
    27.         </dependency>  
    28.         <dependency>  
    29.             <groupId>org.springframework</groupId>  
    30.             <artifactId>spring-beans</artifactId>  
    31.         </dependency>  
    32.         <dependency>  
    33.             <groupId>org.springframework</groupId>  
    34.             <artifactId>spring-jdbc</artifactId>  
    35.         </dependency>  
    36.         <dependency>  
    37.             <groupId>c3p0</groupId>  
    38.             <artifactId>c3p0</artifactId>  
    39.         </dependency>  
    40.         <dependency>  
    41.             <groupId>org.mybatis</groupId>  
    42.             <artifactId>mybatis</artifactId>  
    43.         </dependency>  
    44.         <dependency>  
    45.             <groupId>org.mybatis</groupId>  
    46.             <artifactId>mybatis-spring</artifactId>  
    47.         </dependency>  
    48.         <dependency>  
    49.             <groupId>mysql</groupId>  
    50.             <artifactId>mysql-connector-java</artifactId>  
    51.         </dependency>  
    52.     </dependencies>  
    53. </project>  

    petstore-web/pom.xml

    [html] view plain copy
     
     print?在CODE上查看代码片派生到我的代码片
    1. <?xml version="1.0"?>  
    2. <project  
    3.     xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"  
    4.     xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">  
    5.     <modelVersion>4.0.0</modelVersion>  
    6.     <parent>  
    7.         <groupId>com.example</groupId>  
    8.         <artifactId>petstore-parent</artifactId>  
    9.         <version>0.0.1-SNAPSHOT</version>  
    10.     </parent>  
    11.     <artifactId>petstore-web</artifactId>  
    12.     <packaging>war</packaging>  
    13.     <name>petstore-web Maven Webapp</name>  
    14.     <url>http://maven.apache.org</url>  
    15.     <dependencies>  
    16.         <dependency>  
    17.             <groupId>junit</groupId>  
    18.             <artifactId>junit</artifactId>  
    19.         </dependency>  
    20.         <dependency>  
    21.             <groupId>javax</groupId>  
    22.             <artifactId>javaee-web-api</artifactId>  
    23.         </dependency>  
    24.         <dependency>  
    25.             <groupId>log4j</groupId>  
    26.             <artifactId>log4j</artifactId>  
    27.         </dependency>  
    28.         <dependency>  
    29.             <groupId>javax.servlet</groupId>  
    30.             <artifactId>jstl</artifactId>  
    31.         </dependency>  
    32.         <dependency>  
    33.             <groupId>org.apache.commons</groupId>  
    34.             <artifactId>commons-lang3</artifactId>  
    35.         </dependency>  
    36.         <dependency>  
    37.             <groupId>org.springframework</groupId>  
    38.             <artifactId>spring-core</artifactId>  
    39.         </dependency>  
    40.         <dependency>  
    41.             <groupId>org.springframework</groupId>  
    42.             <artifactId>spring-context</artifactId>  
    43.         </dependency>  
    44.         <dependency>  
    45.             <groupId>org.springframework</groupId>  
    46.             <artifactId>spring-test</artifactId>  
    47.         </dependency>  
    48.         <dependency>  
    49.             <groupId>org.springframework</groupId>  
    50.             <artifactId>spring-jdbc</artifactId>  
    51.         </dependency>  
    52.         <dependency>  
    53.             <groupId>org.springframework</groupId>  
    54.             <artifactId>spring-web</artifactId>  
    55.         </dependency>  
    56.         <dependency>  
    57.             <groupId>org.springframework</groupId>  
    58.             <artifactId>spring-webmvc</artifactId>  
    59.         </dependency>  
    60.         <dependency>  
    61.             <groupId>org.hibernate</groupId>  
    62.             <artifactId>hibernate-validator</artifactId>  
    63.         </dependency>  
    64.         <dependency>  
    65.             <groupId>com.fasterxml.jackson.core</groupId>  
    66.             <artifactId>jackson-core</artifactId>  
    67.         </dependency>  
    68.         <dependency>  
    69.             <groupId>com.fasterxml.jackson.core</groupId>  
    70.             <artifactId>jackson-databind</artifactId>  
    71.         </dependency>  
    72.         <dependency>  
    73.             <groupId>c3p0</groupId>  
    74.             <artifactId>c3p0</artifactId>  
    75.         </dependency>  
    76.         <dependency>  
    77.             <groupId>mysql</groupId>  
    78.             <artifactId>mysql-connector-java</artifactId>  
    79.         </dependency>  
    80.         <dependency>  
    81.             <groupId>org.mybatis</groupId>  
    82.             <artifactId>mybatis</artifactId>  
    83.         </dependency>  
    84.         <dependency>  
    85.             <groupId>org.mybatis</groupId>  
    86.             <artifactId>mybatis-spring</artifactId>  
    87.         </dependency>  
    88.         <dependency>  
    89.             <groupId>com.example</groupId>  
    90.             <artifactId>petstore-persist</artifactId>  
    91.             <version>0.0.1-SNAPSHOT</version>  
    92.         </dependency>  
    93.     </dependencies>  
    94.     <build>  
    95.         <finalName>petstore-web</finalName>  
    96.     </build>  
    97. </project>  

    在上面的配置中,子模块中的依赖不需要在添加<version>元素,与父模块中的设置保持一致。
    另外,注意主pom中junit和javaee-web-api的依赖定义中分别有一个<scope>test</scope>和一个<scope>provided</scope>,<scope>定义了依赖的作用范围,共有5种:

    compile  编译、测试、运行都有效
    test     测试依赖范围, 只对于测试有效, 编译主代码和运行项目时无法使用此类依赖
    provided 对于编译和测试有效, 运行时无效
    runtime  测试和运行时有效, 编译主代码无效, 如JDBC驱动
    system   与provided范围一致, 通过systemPath元素关联本机文件路径, 用于Maven库外的jar包

    回到项目中,添加依赖后红色警告消失,但这里还有一个步骤要做,否则Web模块可能无法运行。在petstore-web上点击右键 -> Maven -> Update Project...。

    到这里,项目就全部搭建完成了。我们来编写一些测试代码,来检查项目是否可以正常运行。最终目录结构如下图,红色方框圈中的是添加或修改了代码的文件,各文件的源码打包提供,就不再一一粘贴了。

    运行结果如下:

    本节代码下载

    总结

    本节中介绍了通过Maven的聚合和继承特性创建复杂的多模块项目,虽然看起来步骤有些繁琐,但掌握后操作并不复杂。下一节开始进入SpringMVC和Mybatis的功能和整合介绍,包括增删改查等功能。

  • 相关阅读:
    Cefsharp 75 设置代理
    C# Winform 执行JS脚本
    Cefsharp 75 为每个实例单独设置缓存目录
    QQ邮箱发送邮件,出现mail from address must be same as authorization user错误
    软件卸载后自动重装,服务器中了流氓软件解决方法
    win版宝塔更新后,宝塔无法启动(修复办法)
    C的typedef
    为Arch Linux安装桌面
    在Simplicity Studio下创建适用于EFR32的工程项目
    在VMWare上安装Arch Linux
  • 原文地址:https://www.cnblogs.com/sa-dan/p/6836965.html
Copyright © 2020-2023  润新知