Using IDEA to Build Multi-Module Aggregation Engineering Based on Maven (Spring mvc+spring+mybatis integration)

Keywords: Maven Apache Spring Mybatis

If you like to read Wechat, you can also pay attention to my Wechat public number: learn java well and get high quality learning resources.

Finally, I have time to do java. Today, I use IDEA to build a multi-module aggregation project based on maven. After many attempts, I finally succeed!

Note: ** This is an improved version based on the original author. There are problems with the pom dependence of the original author's version. Therefore, the dependence here is all my own dependence. There is no problem with the picture according to this!

I. Engineering catalogue

Here's the directory after the build

Let's look at the directory relationship first.

taotao-parent (version of parent project management jar package)

taotao-common

taotao-manager

  |-taotao-manager-pojo

  |-taotao-manager-dao

  |-taotao-manager-service

  |-taotao-manager-web(war Package)

taotao-parent is the parent project of the project to manage jar packages and versions

taotao-common is a generic project, inheriting the parent project is to package the generic tool classes.

taotao-manager inherits the parent project as well as the development project

Among them, taotao-manager-pojo, taotao-manager-dao, taotao-manager-service and taotao-manager-web are all sub-modules of development engineering, which depend on Development Engineering taotao-manager.

The first three sub-modules are typed into jar package and taotao-manager-web into war package.

II. Creating Engineering

1. First, create an empty project. The steps are as follows: File——>new——>project

Select Empty - > next

Fill in the name and location of the project and click finnish to complete the creation of the empty project.

2. Create the parent project taotao-parent, as follows:

File——>New——>Module

Point maven, check archetype, select quickstart, and Next takes the next step

Fill in GroupId, usually the name of the organization; Fill in ArtifactId, the name of the project

Select the local maven directory and maven configuration file

Fill in the name and location of the project, click Finish, and the parent project is created.

Waiting for the POM file to be generated, modify the packaging method to be pom, and the POM file to be:

<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.chinapost</groupId>
  <artifactId>post-parent</artifactId>
  <version>1.0-SNAPSHOT</version>
  <packaging>pom</packaging>

  <!-- Centralized Definition of Dependent Version Numbers -->
  <properties>
    <junit.version>4.10</junit.version>
    <spring.version>4.1.3.RELEASE</spring.version>
    <mybatis.version>3.2.8</mybatis.version>
    <mybatis.spring.version>1.2.2</mybatis.spring.version>
    <mybatis.paginator.version>1.2.15</mybatis.paginator.version>
    <mysql.version>5.1.32</mysql.version>
    <slf4j.version>1.6.4</slf4j.version>
    <jackson.version>2.4.2</jackson.version>
    <druid.version>1.0.9</druid.version>
    <httpclient.version>4.3.5</httpclient.version>
    <jstl.version>1.2</jstl.version>
    <servlet-api.version>2.5</servlet-api.version>
    <jsp-api.version>2.0</jsp-api.version>
    <joda-time.version>2.5</joda-time.version>
    <commons-lang3.version>3.3.2</commons-lang3.version>
    <commons-io.version>1.3.2</commons-io.version>
  </properties>

  <dependencyManagement>
    <dependencies>
      <!-- unit testing -->
      <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>${junit.version}</version>
        <scope>test</scope>
      </dependency>

      <!-- Spring -->
      <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-context</artifactId>
        <version>${spring.version}</version>
      </dependency>
      <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-beans</artifactId>
        <version>${spring.version}</version>
      </dependency>
      <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-webmvc</artifactId>
        <version>${spring.version}</version>
      </dependency>
      <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-jdbc</artifactId>
        <version>${spring.version}</version>
      </dependency>
      <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-aspects</artifactId>
        <version>${spring.version}</version>
      </dependency>

      <!-- Mybatis -->
      <dependency>
        <groupId>org.mybatis</groupId>
        <artifactId>mybatis</artifactId>
        <version>${mybatis.version}</version>
      </dependency>
      <dependency>
        <groupId>org.mybatis</groupId>
        <artifactId>mybatis-spring</artifactId>
        <version>${mybatis.spring.version}</version>
      </dependency>
      <!-- Paging assistant -->
      <dependency>
        <groupId>com.github.pagehelper</groupId>
        <artifactId>pagehelper</artifactId>
        <version>3.7.5</version>
      </dependency>
      <dependency>
        <groupId>com.github.jsqlparser</groupId>
        <artifactId>jsqlparser</artifactId>
        <version>0.9.1</version>
      </dependency>
      <!-- currency Mapper -->
      <dependency>
        <groupId>com.github.abel533</groupId>
        <artifactId>mapper</artifactId>
        <version>2.3.4</version>
      </dependency>

      <!-- MySql -->
      <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <version>${mysql.version}</version>
      </dependency>

      <dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>slf4j-log4j12</artifactId>
        <version>${slf4j.version}</version>
      </dependency>

      <!-- Jackson Json Processing Toolkit -->
      <dependency>
        <groupId>com.fasterxml.jackson.core</groupId>
        <artifactId>jackson-databind</artifactId>
        <version>${jackson.version}</version>
      </dependency>

      <!-- Connection pool -->
      <dependency>
        <groupId>com.jolbox</groupId>
        <artifactId>bonecp-spring</artifactId>
        <version>0.8.0.RELEASE</version>
      </dependency>

      <!-- httpclient -->
      <dependency>
        <groupId>org.apache.httpcomponents</groupId>
        <artifactId>httpclient</artifactId>
        <version>${httpclient.version}</version>
      </dependency>

      <!-- JSP Relevant -->
      <dependency>
        <groupId>jstl</groupId>
        <artifactId>jstl</artifactId>
        <version>${jstl.version}</version>
      </dependency>
      <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>servlet-api</artifactId>
        <version>${servlet-api.version}</version>
        <scope>provided</scope>
      </dependency>
      <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>jsp-api</artifactId>
        <version>${jsp-api.version}</version>
        <scope>provided</scope>
      </dependency>

      <!-- Time Operating Component -->
      <dependency>
        <groupId>joda-time</groupId>
        <artifactId>joda-time</artifactId>
        <version>${joda-time.version}</version>
      </dependency>

      <!-- Apache Tool components -->
      <dependency>
        <groupId>org.apache.commons</groupId>
        <artifactId>commons-lang3</artifactId>
        <version>${commons-lang3.version}</version>
      </dependency>
      <dependency>
        <groupId>org.apache.commons</groupId>
        <artifactId>commons-io</artifactId>
        <version>${commons-io.version}</version>
      </dependency>

    </dependencies>
  </dependencyManagement>

  <build>
    <finalName>${project.artifactId}</finalName>
    <plugins>
      <!-- Resource File Copy Plug-in -->
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-resources-plugin</artifactId>
        <version>2.7</version>
        <configuration>
          <encoding>UTF-8</encoding>
        </configuration>
      </plugin>
      <!-- java Compiler plug-in -->
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>3.2</version>
        <configuration>
          <source>1.7</source>
          <target>1.7</target>
          <encoding>UTF-8</encoding>
        </configuration>
      </plugin>
    </plugins>
    <pluginManagement>
      <plugins>
        <!-- To configure Tomcat Plug-in unit -->
        <plugin>
          <groupId>org.apache.tomcat.maven</groupId>
          <artifactId>tomcat7-maven-plugin</artifactId>
          <version>2.2</version>
        </plugin>
      </plugins>
    </pluginManagement>
  </build>
</project>

3. Creating a General Engineering taotao-common

File——>New——>Module

next step directly after point maven

Inherit taotao-parent, fill in ArtifactId, and next step

Fill in the generic module name, click finish, and create the generic module.

Development module taotao-manager is consistent with general module creation

taotao-common's 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>

    <parent>
        <groupId>com.chinapost</groupId>
        <artifactId>post-parent</artifactId>
        <version>1.0-SNAPSHOT</version>
    </parent>

    <groupId>com.chinapost</groupId>
    <artifactId>post-common</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>jar</packaging>

    <dependencies>
        <!-- Jackson Json Processing Toolkit -->
        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-databind</artifactId>
            <version>${jackson.version}</version>
        </dependency>
        <!-- Time Operating Component -->
        <dependency>
            <groupId>joda-time</groupId>
            <artifactId>joda-time</artifactId>
            <version>${joda-time.version}</version>
        </dependency>

        <!-- Apache Tool components -->
        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-lang3</artifactId>
            <version>${commons-lang3.version}</version>
        </dependency>
    </dependencies>
</project>

taotao-manager's 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>

    <parent>
        <groupId>com.chinapost</groupId>
        <artifactId>post-parent</artifactId>
        <version>1.0-SNAPSHOT</version>
    </parent>

    <groupId>com.chinapost</groupId>
    <artifactId>post-manager</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>pom</packaging>

    <dependencies>
        <dependency>
            <groupId>com.chinapost</groupId>
            <artifactId>post-common</artifactId>
            <version>1.0-SNAPSHOT</version>
        </dependency>
        <!-- unit testing -->
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-log4j12</artifactId>
        </dependency>
        <!-- Apache Tool components -->
        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-lang3</artifactId>
            <version>${commons-lang3.version}</version>
        </dependency>
        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-io</artifactId>
            <version>${commons-io.version}</version>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <!-- To configure Tomcat Plug-in unit -->
            <plugin>
                <groupId>org.apache.tomcat.maven</groupId>
                <artifactId>tomcat7-maven-plugin</artifactId>
                <configuration>
                    <port>8999</port>
                    <path>/</path>
                </configuration>
            </plugin>
        </plugins>
    </build>

    <modules>
        <module>post-manager-pojo</module>
        <module>post-manager-dao</module>
        <module>post-manager-service</module>
        <module>post-manager-web</module>
    </modules>
</project>

4. Create sub-modules under the development module. There are two kinds of introduction: taotao-manager-pojo, taotao-manager-dao and taotao-manager-service. The creation of taotao-manager-web is a little different.

1) Take taotao-manager-pojo as an example

File——>new——>module

Select maven - > check archetype - > select quickstart - > click next

Inherit taotao-manager and fill in ArtifactId

Note here that the rewrite path, point finish, a sub-module creation completed

taotao-manager-pojo's 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>

    <parent>
        <groupId>com.chinapost</groupId>
        <artifactId>post-manager</artifactId>
        <version>1.0-SNAPSHOT</version>
    </parent>

    <groupId>com.chinapost</groupId>
    <artifactId>post-manager-pojo</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>jar</packaging>

    <dependencies>
        <dependency>
            <groupId>com.chinapost</groupId>
            <artifactId>post-common</artifactId>
            <version>1.0-SNAPSHOT</version>
        </dependency>
        <dependency>
            <groupId>javax.persistence</groupId>
            <artifactId>persistence-api</artifactId>
            <version>1.0</version>
        </dependency>
    </dependencies>
</project>

taotao-manager-dao's 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">
    <parent>
        <artifactId>post-manager</artifactId>
        <groupId>com.chinapost</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>post-manager-dao</artifactId>
    <packaging>jar</packaging>

    <name>post-manager-dao</name>
    <url>http://maven.apache.org</url>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>

    <dependencies>
        <dependency>
            <groupId>com.chinapost</groupId>
            <artifactId>post-manager-pojo</artifactId>
            <version>1.0-SNAPSHOT</version>
        </dependency>
        <!-- Mybatis -->
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis</artifactId>
        </dependency>
        <!-- currency Mapper -->
        <dependency>
            <groupId>com.github.abel533</groupId>
            <artifactId>mapper</artifactId>
        </dependency>
    </dependencies>
</project>  

taotao-manager-service 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">
    <parent>
        <artifactId>post-manager</artifactId>
        <groupId>com.chinapost</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>post-manager-service</artifactId>
    <packaging>jar</packaging>

    <name>post-manager-service</name>
    <url>http://maven.apache.org</url>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>

    <dependencies>
        <dependency>
            <groupId>com.chinapost</groupId>
            <artifactId>post-manager-dao</artifactId>
            <version>1.0-SNAPSHOT</version>
        </dependency>
        <!-- Spring -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-beans</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
        </dependency>
        <!-- Paging assistant -->
        <dependency>
            <groupId>com.github.pagehelper</groupId>
            <artifactId>pagehelper</artifactId>
        </dependency>
        <dependency>
            <groupId>com.github.jsqlparser</groupId>
            <artifactId>jsqlparser</artifactId>
        </dependency>
    </dependencies>
</project>  

2) taotao-manager-web creation

Similarly File - > New - > Module

Just choose webapp here (the only difference, similar elsewhere) and click Next next.

Also inherit taotao-manager

You also need to rewrite the path and click finish to create the taotao-manager-web sub-module. taotao-manager-web 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">
    <parent>
        <artifactId>post-manager</artifactId>
        <groupId>com.chinapost</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>
    <artifactId>post-manager-web</artifactId>
    <packaging>war</packaging>
    <name>post-manager-web</name>
    <url>http://maven.apache.org</url>
    <!-- Add dependency -->
    <dependencies>
        <dependency>
            <groupId>com.chinapost</groupId>
            <artifactId>post-manager-service</artifactId>
            <version>1.0-SNAPSHOT</version>
        </dependency>
        <dependency>
            <groupId>commons-fileupload</groupId>
            <artifactId>commons-fileupload</artifactId>
            <version>1.3.1</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-jdbc</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-aspects</artifactId>
        </dependency>
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis-spring</artifactId>
        </dependency>
        <!-- MySql -->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
        </dependency>
        <!-- Jackson Json Processing Toolkit -->
        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-databind</artifactId>
        </dependency>
        <!-- Connection pool -->
        <dependency>
            <groupId>com.jolbox</groupId>
            <artifactId>bonecp-spring</artifactId>
        </dependency>
        <!-- JSP Relevant -->
        <dependency>
            <groupId>jstl</groupId>
            <artifactId>jstl</artifactId>
        </dependency>
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>servlet-api</artifactId>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>jsp-api</artifactId>
            <scope>provided</scope>
        </dependency>
        <!-- Time Operating Component -->
        <dependency>
            <groupId>joda-time</groupId>
            <artifactId>joda-time</artifactId>
        </dependency>
    </dependencies>
    <build>
        <finalName>post-manger-web</finalName>
    </build>
</project>

5. Next, run the project using Tomcat 7:run

Edit Configurations in the upper right corner Select maven

It is necessary to install taotao-parent and other installations into the local warehouse.

The final run, as shown in the following figure, shows the run!

Conclusion:

Before reading other people's blogs, there are options for portlet items, their own death can not run.

taotao-parent,taotao-manager-pojo,taotao-manager-dao,taotao-manager-service choose quickstart;

taotao-common, taotao-manager choose nothing;

taotao-manager-web selects the webapp item.

Pay attention to packaging, jar, pom, war

Reference material

Posted by skaforey on Mon, 31 Dec 2018 13:21:08 -0800