Through the maven modularization mechanism, we can separate different types of functions and store them in different modules.

Create Maven Modules

Create Common Module

  • Right click shop (project) directory: New > Module
  • Choose “Maven”
  • Tick “Create from archetype” checkbox option
  • Scroll down a little bit and choose “maven-archetype-quickstart”, click “Next” button
img
  • Fill “common” in ArtifactId, click “Next” buttons till the last step
img
  • Click “Finish” button

Remove “co.dongchen” package in java and test directories of the common module

img

Create Service Module

  • Right click shop (project) directory: New > Module
  • Choose “Maven”
  • Tick “Create from archetype” checkbox option
  • Scroll down a little bit and choose “maven-archetype-quickstart”, click “Next” button
img
  • Fill “service” in ArtifactId, click “Next” buttons till the last step
img
  • Click “Finish” button

Remove “co.dongchen” package in java and test directories of the service module

img

Create Controller Module

  • Right click shop (project) directory: New > Module
  • Choose “Maven”
  • Tick “Create from archetype” checkbox option
  • Scroll down a little bit and choose “maven-archetype-quickstart”, click “Next” button
img
  • Fill “controller” in ArtifactId, click “Next” buttons till the last step
img
  • Click “Finish” button

Remove “co.dongchen” package in java and test directories of the service module

img

Migrate Files to Correspondent Modules

  • Right click common/src/main/java directory: New > Package
  • Fill in “co.dongchen.shop.common.model”
  • Click “Finish” button
img

Drag the Product.java file in from the “shop/src/main/java/co.dongchen.shop.common.model” package, and remove the empty package.

Service Package

  • Right click service/src/main/java directory: New > Package
  • Fill in “co.dongchen.shop.service”
  • Click “Finish” button
img

Drag the ProductService.java file in from the “shop/src/main/java/co.dongchen.shop.service” package, and remove the empty package.

Config Package

  • Right click service/src/main/java directory: New > Package
  • Fill in “co.dongchen.shop.config”
  • Click “Finish” button
img

Drag the DruidConfig.java file in from the “shop/src/main/java/co.dongchen.shop.config” package, and remove the empty package.

Mapper Package

  • Right click service/src/main/java directory: New > Package
  • Fill in “co.dongchen.shop.mapper”
  • Click “Finish” button
img

Drag the ProductMapper.java file in from the “shop/src/main/java/co.dongchen.shop.mapper” package, and remove the empty package.

Resources Directory

  • Right click service/src/main directory: New > Directory
  • Fill in “resources”
  • Click “Finish” button
img

Drag both the mapper and mybatis directories in from the “shop/src/main/resources” directory

Shop Package

  • Right click controller/src/main/java directory: New > Package
  • Fill in “co.dongchen.shop”
  • Click “Finish” button
img

Drag the ShopApplication.java file in from the “shop/src/main/java/co.dongchen.shop” package

Controller Package

  • Right click controller/src/main/java directory: New > Package
  • Fill in “co.dongchen.shop.controller”
  • Click “Finish” button
img

Drag both the FreeMarkerController.java and ProductController.java files in from the “shop/src/main/java/co.dongchen.shop.controller” package, and remove the empty package.

Resources Directory

  • Right click controller/src/main directory: New > Directory
  • Fill in “resources”
  • Click “Finish” button
img

Drag both the templates directory and the application.properties file in from the “shop/src/main/resources” directory.

Move Maven Dependencies into Related Modules

Common Module

Move the following dependencies from “shop/pom.xml” to “common/pom.xml”:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>2.0.1</version>
        </dependency>

        <dependency>
            <groupId>com.google.guava</groupId>
            <artifactId>guava</artifactId>
            <version>27.0-jre</version>
        </dependency>

Service Module

Move the following dependencies from “shop/pom.xml” to “service/pom.xml”:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>8.0.15</version>
        </dependency>

        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid-spring-boot-starter</artifactId>
            <version>1.1.16</version>
        </dependency>

Add the common module dependency into “service/pom.xml”:

1
2
3
4
5
        <dependency>
            <groupId>co.dongchen</groupId>
            <artifactId>common</artifactId>
            <version>0.0.1-SNAPSHOT</version>
        </dependency>

Controller Module

Move the following dependencies from “shop/pom.xml” to “controller/pom.xml”:

1
2
3
4
5
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-freemarker</artifactId>
            <version>2.1.4.RELEASE</version>
        </dependency>

Add the service module dependency into “controller/pom.xml”:

1
2
3
4
5
        <dependency>
            <groupId>co.dongchen</groupId>
            <artifactId>service</artifactId>
            <version>0.0.1-SNAPSHOT</version>
        </dependency>

Verify

Run the App

1
2
// Short Cut for "Run the Program"
Alt + Shift + F10
img

Choose the correspondent option and press enter.

View Home Page in the Browser

1
http://localhost:8080/home
img

Now, our application is modularized.

References Modules

Buy me a coffeeBuy me a coffee