1、项目架构
1.1、图形表示
demo
springboot
springboot-parent
demo1
demo2
mybatis-springboot
mysql-driver
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>
<dependency>
<groupId>com.example</groupId>
<artifactId>demo2</artifactId>
<version>${project.version}</version>
</dependency>
</dependencies>
<build>
<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>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.1.1</version>
</dependency>
<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>
<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
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、配置文件冲突问题
--resource
----------application.yml
--resource
----------application.yml
--resource
----------application.yml
----------application.yml
--resource
----------application.yml
----------application-demo1.yml
--resource
----------application-demo2.yml
--resource
----------application.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下载
|