1、项目架构

1.1、图形表示



demo 
         

           springboot 
         

           springboot-parent 
         

           demo1 
         

           demo2 
         

           mybatis-springboot 
         

           mysql-driver


  • 粗实线的箭头表示继承关系
  • 虚线箭头表示组合关系
  • demo 作为父工程,包含了一些公共依赖
  • demo1 作为子工程1,设定为启动模块,依赖于demo2
  • demo2 作为子工程2,设定为业务模块,包含对数据的增删改查

1.2、代码体现

demo工程的pom文件

<!--父模块-->
<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.5.4</version>
    <relativePath/>
</parent>
<!--当前模块定义-->
<groupId>com.example</groupId>
<artifactId>demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<!--一定要添加,否则会报错-->
<packaging>pom</packaging>
<!--子模块,打包时,会根据依赖关系打包所有子模块-->
<modules>
    <module>demo1</module>
    <module>demo2</module>
</modules>
<!--依赖的模块-->
<dependencies>
    <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>
</dependencies>

demo1工程的pom文件

<!--父模块-->
<parent>
    <groupId>com.example</groupId>
    <artifactId>demo</artifactId>
    <version>0.0.1-SNAPSHOT</version>
</parent>
<!--当前模块定义-->
<groupId>com.example</groupId>
<artifactId>demo1</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>demo1</name>
<!--依赖的模块-->
<dependencies>
    <!--对于同项目下的模块,一定要先clean,否则看不到传递依赖内容-->
    <dependency>
        <groupId>com.example</groupId>
        <artifactId>demo2</artifactId>
        <version>${project.version}</version>
    </dependency>
</dependencies>
<build>
    <!--这个插件表示,可以打包成可执行jar,且所有依赖都会放到jar中-->
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
        </plugin>
    </plugins>
</build>

demo2工程的pom文件

<!--父模块-->
<parent>
    <groupId>com.example</groupId>
    <artifactId>demo</artifactId>
    <version>0.0.1-SNAPSHOT</version>
</parent>
<!--当前模块定义-->
<groupId>com.example</groupId>
<artifactId>demo2</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>demo2</name>
<!--依赖的模块-->
<dependencies>
    <!--mybatis 启动器-->
    <dependency>
        <groupId>org.mybatis.spring.boot</groupId>
        <artifactId>mybatis-spring-boot-starter</artifactId>
        <version>2.1.1</version>
    </dependency>
    <!--mysql数据库驱动-->
    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <version>8.0.18</version>
    </dependency>
</dependencies>
<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
            <!--
                1. 这个表示可执行的jar包的后缀为exex,标准名称的jar为普通打包形式
                之所以这样做,是为了不打包依赖项,多模块情况下很好用
                2. 还有另外一种,等效做法就是安装当前这个插件,这样打的包也同样不包含依赖项
                3. 由于父模块,会被继承,所以父模块不可安装这个插件
			-->
            <configuration>
                <classifier>exec</classifier>
            </configuration>
        </plugin>
    </plugins>
</build>

2、主启动模块

根据当前的架构,主启动模块为demo1

注意事项,一个是如果包的层级不小于等于启动类的层级,需要显示扫描安装的bean

2.1、关于启动类的注意事项

--java
------com.example.demo1
-----------------------dao
-----------------------service
-----------------------controller
-----------------------Demo1Application.java #启动类

--java
------com.example.demo2
-----------------------dao
-----------------------service
-----------------------controller
-----------------------Demo2Application.java

#如果,这是两个人子模块,那么可以理解成这样

--java
------com
---------example
-----------------demo1
-----------------------dao
-----------------------service
-----------------------controller
-----------------------Demo1Application.java #启动类
-----------------demo2
-----------------------dao
-----------------------service
-----------------------controller
-----------------------Demo2Application.java
#自然demo2里面的bean的设定就不能被安装到容器中
#这就需要在demo1的启动类中显示的指定了!!!

2.2、显示指定组件与Mapper

@SpringBootApplication
@ComponentScan("com.example.demo2")
@MapperScan("com.example.demo2.dao")
public class Demo1Application {
    public static void main(String[] args) {
        SpringApplication.run(Demo1Application.class, args);
    }
}

3、配置文件的处理

3.1、配置文件冲突问题

#demo1
--resource
----------application.yml
#demo1
--resource
----------application.yml
#合并之后
--resource
----------application.yml #冲突
----------application.yml #冲突

#为了解决冲突的问题

#demo1
--resource
----------application.yml #导入application-demo1.yml与application-demo2.yml
----------application-demo1.yml
#demo1
--resource
----------application-demo2.yml
#合并之后
--resource
----------application.yml #导入application-demo1.yml与application-demo2.yml
----------application-demo1.yml
----------application-demo2.yml

3.2、处理冲突的代码实现

application.yml

spring:
   profiles:
      active: demo1,demo2

application-demo1.yml

server:
  port: 8082

application-demo2.yml

spring:
  datasource:
    driver-class-name: com.mysql.cj.jdbc.Driver
    url: jdbc:mysql://localhost:3306/readmore?useUnicode=true&characterEncoding=UTF-8&serverTimezone=Asia/Shanghai
    username: root
    password: root
mybatis:
    mapper-locations: classpath:/mapper/*Dao.xml

完整代码地址:demo下载