Create a parent project

Use the IDE to create functionality

Intellij > File > New > Project > Spring Initializr
Copy the code

Set the project name and create an empty main project using the default options

Modified pom. XML

  1. Adding submodules
<! -- Module description: Btp-common </module> <module> Btp-dao </module> <module>btp-service</module> <module>btp-admin</module> <module>btp-user</module> </modules>Copy the code
  1. Delete Dependencies
  2. Add POM Parent project package cannot be the default JAR, so change to POM
  3. delete

The final POM.xml looks like this

<? xml version="1.0" encoding="UTF-8"? > <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 > < the parent > < groupId > org. Springframework. Boot < / groupId > The < artifactId > spring - the boot - starter - parent < / artifactId > < version > 2.1.3. RELEASE < / version > < relativePath / > <! -- lookup parent from repository --> </parent> <groupId>com.boyuantang</groupId> <artifactId>btp-parent</artifactId> < version > 0.0.1 - the SNAPSHOT < / version > < name > BTP - parent < / name > < description > Demo projectforSpring Boot</description> <packaging>pom</packaging> <! -- Module description: Btp-common </module> <module> Btp-dao </module> <module>btp-service</module> <module>btp-admin</module> <module>btp-user</module> </modules> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> < project. Reporting. OutputEncoding > utf-8 < / project. Reporting. OutputEncoding > < Java version > 1.8 < / Java version > </properties> <dependencies> </dependencies> </project>Copy the code

The role of the parent project

  • The parent project inherits the Spring framework
  • The parent project defines the version number
  • A parent project allows child modules to reference each other

2. Create submodules

In the IDE, click on the parent project and right click

New > Module > Spring Initializr
Copy the code

Set the project name, using the default options, and create an empty submodule

Modified pom. XML

  1. Add parent project

Replaces the default Parent Spring, because parent projects already inherit parent Spring

<! Boyuantang </groupId> <artifactId> btP-parent </artifactId> The < version > 0.0.1 - the SNAPSHOT < / version > < / parent >Copy the code
  1. Add spring dependencies as required, if you are a Web module. If it is a Module module, add dependencies as needed. Submodules can also reference each other, as follows
< the dependency > < groupId > com. Boyuantang < / groupId > < artifactId > BTP - dao < / artifactId > < version > 0.0.1 - the SNAPSHOT < / version > </dependency>Copy the code
  1. If it is not a Web module, delete it

Non-web module, the final POM.xml looks like this

<? xml version="1.0" encoding="UTF-8"? > <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 > <! Boyuantang </groupId> <artifactId> btP-parent </artifactId> < version > 0.0.1 - the SNAPSHOT < / version > < / parent > < groupId > com. Boyuantang < / groupId > < artifactId > BTP - dao < / artifactId > < version > 0.0.1 - the SNAPSHOT < / version > < name > BTP - dao < / name > < description > Demo projectforSpring Boot</description> <properties> <java.version>1.8</java.version> </properties> <dependencies> < the groupId > com. Boyuantang < / groupId > < artifactId > BTP - service < / artifactId > < version > 0.0.1 - the SNAPSHOT < / version > < / dependency > </dependencies> </project>Copy the code

Three, packaging,

Submodules can be packaged separately. The generated JAR works.

4. Configuration problems

  1. Error: xxxDAO does not exist, add@MapperScan
@MapperScan("com.boyuantang.dao") @SpringBootApplication public class BtpAdminApplication { public static void main(String[] args) { SpringApplication.run(BtpAdminApplication.class, args); }}Copy the code
  1. If you encounter a bean that cannot register the service module, add@ComponentScan
@ComponentScan("com.boyuantang") @SpringBootApplication public class BtpAdminApplication { public static void main(String[] args) { SpringApplication.run(BtpAdminApplication.class, args); }}Copy the code
  1. Unable to scan mybatis XML file, added in application.properties
mybatis.mapper-locations=classpath*:mapper/*.xml
Copy the code

V. Dependence between projects

  1. The parent project imports global dependencies so that modules don’t have to import them anymore

    Such aslombok.fastjsonSuch as
  2. The Web module imports the Service module without importing Entity, common, etc., because the Service already contains these submodules