Article directory
-
- 1 sub-module development and design
- 2 sub-module development and implementation
-
- 2.1 Environment preparation
- 2.2 Extract domain layer
-
-
- Step 1: Create a new module
- Step 2: Create a domain package in the project
- Step 3: Delete the domain package in the original project
- Step 4: Build dependencies
- Step 5: Compile the `maven_02_ssm` project
- Step 6: Install the project in the local warehouse
-
- 2.3 Extract Dao layer
-
-
- Step 1: Create a new module
- Step 2: Create a dao package in the project
- Step 3: Delete the dao package in the original project
- Step 4: Install the project to the local repository
-
- 2.4 Run the test and summarize
1 Sub-module development and design
(1) Split according to function
Our current projects are all in one module, such as the previous SSM integration development. Although the functions have been realized in this way, there are still some problems. Let’s take the bank project as an example to talk about this matter.
- When the network is not so developed, we need to go to the bank counter or cash machine for business operations
- With the development of the Internet, after we have a computer, we can log in to the bank website on the webpage and use the U-shield to conduct business operations
- Furthermore, with the popularization of smart phones, we only need to use mobile phones to log in to the APP to conduct business operations
The above three scenarios appear at different times. If you have to put the module codes of the three scenarios into a project, then when there is a problem with one of the module codes, it will cause the entire project to fail to start normally, resulting in bank failures. Many of the businesses are unable to work normally. So we will split the project according to function.
(2) Split by module
For example, in an e-commerce project, there are two modules: order and product. The order needs to contain the detailed information of the product, so the model class of the product is required, and the product module will also use the model class of the product. At this time, if both modules have When writing model classes, there will be repeated codes, and the later maintenance costs will be relatively high. We wonder if we can extract their common parts into an independent module. If other modules want to use them, we can use our own extracted modules like adding third-party jar package dependencies, which solves the problem of code duplication. The splitting method is what we call split according to modules.
After analyzing two cases, we know that:
- The original module is divided into several sub-modules according to the function, so as to facilitate mutual calling and interface sharing between modules.
Just now we said that the domain layer can be split. In addition to the domain layer, we can also split other layers into opposing modules, such as:
In this way, each layer in the project can be maintained independently, and can also be easily used by others. We have finished talking about the significance of sub-module development. After talking about so many benefits, how to realize it?
2 sub-module development and implementation
We have completed the SSM integration before. Next, we will split the project based on the SSM integration project.
2.1 Environmental preparation
Masked5 / heima_maven_codes GitCode
Deploy the maven_02_ssm Code to IDEA and quickly prepare the environment. After successful deployment, the format of the project is as follows:
2.2 Extract domain layer
Step 1: Create a new module
Create a jar project named maven_03_pojo
, why the project name is created from 02 to 03, the reason we will mention later, the name of this piece can be arbitrary.
Step 2: Create a domain package in the project
Create a com.iheima.domain
package in the maven_03_pojo
project, and copy the Book class in maven_02_ssm
to the package
Step 3: Delete the domain package in the original project
After deletion, there will be a red prompt in the class that uses Book
in the maven_02_ssm
project, as follows:
**Description:**The reason for the error is that the Book class has been deleted in maven_02_ssm
, so the project cannot find the Book class, so an error is reported
To solve the above problems, we need to add the dependency of maven_03_pojo
in maven_02_ssm
.
Step 4: Build dependencies
Add the dependency of maven_03_pojo
to the pom.xml of the maven_02_ssm
project
<dependency> <groupId>com.itheima</groupId> <artifactId>maven_03_pojo</artifactId> <version>1.0-SNAPSHOT</version> </dependency>
Because the dependency is added, the Book class can already be found in maven_02_ssm
, so the red prompt just now will disappear.
Step 5: Compile the maven_02_ssm
project
Compile maven_02_ssm
and you will see the following error in the console
The error message is: the dependency problem of the maven_02_ssm
project cannot be resolved, and the maven_03_pojo
jar package cannot be found.
Why can’t I find it?
The reason is that Maven will find the corresponding jar package from the local warehouse, but the jar package does not exist in the local warehouse, so an error will be reported.
There is a maven_03_pojo
project in IDEA, so we only need to install the maven_03_pojo
project to the local warehouse.
Step 6: Install the project to the local warehouse
Use maven’s install command to install the dependent project maven_03_pojo
into Maven’s local warehouse.
After the installation is successful, you can see the installed jar package in the corresponding path
**Description:** The specific installation location is related to the location of Maven’s local warehouse configuration on your own computer.
After executing the compile command of maven_02_ssm
again, it can be compiled successfully.
2.3 Extract Dao layer
Step 1: Create a new module
Create a jar project named maven_04_dao
Step 2: Create a dao package in the project
Create a com.iheima.dao
package in the maven_04_dao
project, and copy the BookDao class in maven_02_ssm
to the package
In maven_04_dao
, there will be the following problems to be solved:
-
The Book class in the BookDao interface of the project
maven_04_dao
cannot find an error-
Solution Add the
maven_03_pojo
project to the pom.xml of themaven_04_dao
project<dependencies> <dependency> <groupId>com.itheima</groupId> <artifactId>maven_03_pojo</artifactId> <version>1.0-SNAPSHOT</version> </dependency> </dependencies>
-
-
In the BookDao interface of the project
maven_04_dao
, Mybatis adds, deletes, changes, checks, and comments report an error-
Solution Add the relevant dependencies of
mybatis
in the pom.xml of themaven_04_dao
project<dependencies> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>3.5.6</version> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.47</version> </dependency> </dependencies>
-
Step 3: Delete the dao package in the original project
After deleting the Dao package, because the BookServiceImpl class in maven_02_ssm
uses Dao content, you need to add maven_04_dao
in the pom.xml of maven_02_ssm
dependence on
<dependency> <groupId>com.itheima</groupId> <artifactId>maven_04_dao</artifactId> <version>1.0-SNAPSHOT</version> </dependency>
At this time, the maven_03_pojo
and maven_04_dao
packages have been added to the maven_02_ssm
project
Compile the maven_02_ssm
project again, and an error will be reported, as follows:
The cause of the error is the same as just now, maven did not find maven_04_dao
in the warehouse, so we only need to install maven_04_dao
to Maven’s local warehouse at this time.
Step 4: Install the project to the local warehouse
Use maven’s install command to install the dependent project maven_04_dao
into Maven’s local warehouse.
After the installation is successful, you can see the corresponding jar package installed in the corresponding path
After executing the compile command of maven_02_ssm
again, it can be compiled successfully.
2.4 Run the test and summarize
Run the extracted project, and the function of adding, deleting, modifying and checking before the test can still be used.
So for the split of the project, there are roughly the following steps:
(1) Create a Maven module
(2) Write module code
Sub-module development needs to be designed for the module function first, and then coded. The project will not be developed first and then split. The split method can be split according to function or according to module.
(3) Install the module to the local warehouse through the maven command (install command)
The internal development of the team needs to release the module function to the warehouse that can be shared within the team (private server), which we will explain later.