当前位置: 代码迷 >> 综合 >> SpringBoot —— 简单多模块构建
  详细解决方案

SpringBoot —— 简单多模块构建

热度:57   发布时间:2023-09-18 12:37:39.0

前言

SpringBoot系列到现在虽然代码不多,但是感觉结构很乱,随着项目的复杂性提高,代码会越来越臃肿,耦合性高。

所以SpringBoot多模块很有必要,简单来说就是由以前按包分模块变为jar包分模块。在多模块jar模式下可以将某个jar拿出来对外共用,能大大提高代码复用率与开发效率。(后续SpringCloud就是将jar升级成war或者多个集合jar,也就是常说的微服务。)

一、模块划分

1.整体流程

(1)新建springboot项目;
(2)在新建后的springboot项目中新建多个module;
(3)修改pom文件以及删除多余的文件及文件夹;
(4)将原项目的代码放进去;

2. 新建springboot项目

通过Spring Initializr新建一个普通的spring boot项目(快速搭建springboot项目)
SpringBoot —— 简单多模块构建

3. 新建Module

右击项目,选择新建Module
SpringBoot —— 简单多模块构建
新建springboot-common、springboot-dao、springboot-service、springboot-api模块(每个子模块的groupId要建议一样)
SpringBoot —— 简单多模块构建
全部建好之后,效果如下:
SpringBoot —— 简单多模块构建

4. 删除多余的文件

1.springboot项目
父模块springboot-parent中将src文件和多余的文件删除;

2.module模块
将springboot-service和springboot-dao下面的application启动类和对应配置文件application.yml,一起删除了,springboot-api模块的不动。

4. 修改pom文件

1.修改父模块pom.xml:将打包方式改为pom新建modules标签
父模块pom.xm用于加载一些全局的或者公共的jar包,以及配置打包。

<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>2.5.5</version><relativePath/> <!-- lookup parent from repository --></parent><groupId>com.local.springboot</groupId><artifactId>springboot-parent</artifactId><version>0.0.1-SNAPSHOT</version><name>springboot-parent</name><description>Demo project for Spring Boot</description><!--打包方式改为pom--><packaging>pom</packaging><!--子模块信息--><modules><module>springboot-common</module><module>springboot-dao</module><module>springboot-service</module><module>springboot-api</module></modules><properties><java.version>1.8</java.version></properties><!--子模块继承父模块依赖--><dependencies><dependency><groupId>net.sf.json-lib</groupId><artifactId>json-lib</artifactId><version>2.4</version><classifier>jdk15</classifier></dependency><dependency><groupId>com.alibaba</groupId><artifactId>fastjson</artifactId><version>1.2.70</version></dependency><dependency><groupId>commons-lang</groupId><artifactId>commons-lang</artifactId><version>2.5</version></dependency><dependency><groupId>javax.servlet</groupId><artifactId>javax.servlet-api</artifactId><version>4.0.0</version></dependency></dependencies></project>

2.修改api模块中的pom.xml

<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><groupId>com.local.springboot</groupId><artifactId>springboot-api</artifactId><version>0.0.1-SNAPSHOT</version><name>springboot-api</name><description>Demo project for Spring Boot</description><!--继承信息--><parent><groupId>com.local.springboot</groupId><artifactId>springboot-parent</artifactId><version>0.0.1-SNAPSHOT</version></parent><dependencies><!--依赖其他子模块--><dependency><groupId>com.local.springboot</groupId><artifactId>springboot-service</artifactId><version>0.0.1-SNAPSHOT</version></dependency><!--spring boot web--><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><!--spring boot test --><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope></dependency><!-- Swagger UI API接口--><dependency><groupId>io.springfox</groupId><artifactId>springfox-swagger2</artifactId><version>2.9.2</version></dependency><dependency><groupId>io.springfox</groupId><artifactId>springfox-swagger-ui</artifactId><version>2.9.2</version></dependency><!-- swagger-bootstrap-ui--><dependency><groupId>com.github.xiaoymin</groupId><artifactId>swagger-bootstrap-ui</artifactId><version>1.9.6</version></dependency><dependency><groupId>junit</groupId><artifactId>junit</artifactId><scope>test</scope></dependency><dependency><groupId>junit</groupId><artifactId>junit</artifactId><scope>test</scope></dependency></dependencies><build><plugins><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId></plugin></plugins></build></project>

其他模块都继承父模块,这里需要注意:由于我们把其他模块的启动类删了,父模块的spring-boot-maven-plugin插件也需要删掉,然后添加到需要的模块中去,因为springboot这个插件必须要启动类,否则则会出现错误:

Failed to execute goal org.springframework.boot:
spring-boot-maven-plugin:2.5.5:repackage (repackage) on project springboot-common: 
Execution repackage of goal org.springframework.boot:spring-boot-maven-
plugin:2.5.5:repackage failed: Unable to find main class

3.其他模块修改类似,springboot-dao模块存放数据库相关处理逻辑,需要加入mybatis依赖
springboot-dao模块pom.xml

<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><groupId>com.local.springboot</groupId><artifactId>springboot-dao</artifactId><version>0.0.1-SNAPSHOT</version><name>springboot-dao</name><description>Demo project for Spring Boot</description><!--继承信息--><parent><groupId>com.local.springboot</groupId><artifactId>springboot-parent</artifactId><version>0.0.1-SNAPSHOT</version></parent><dependencies><!--依赖其他子模块--><dependency><groupId>com.local.springboot</groupId><artifactId>springboot-common</artifactId><version>0.0.1-SNAPSHOT</version></dependency><!--添加MyBatis-Plus依赖 --><dependency><groupId>com.baomidou</groupId><artifactId>mybatis-plus-boot-starter</artifactId><version>3.4.0</version></dependency><!--mysql--><dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId></dependency></dependencies>
</project>

5. 修改application.yml

所有的xml配置和yml配置只能放在最外层的模块,application.yml只能放在springboot-api模块下

将以前的代码修改

#内置Tomcat容器配置
server:port: 8080servlet:#应用路径,配置应用路径,可方便进行反向代理context-path:
spring:# 数据源datasource:url: jdbc:mysql://localhost:3306/local_develop?serverTimezone=Asia/Shanghai&useUnicode=true&characterEncoding=utf-8username: rootpassword: rootdriver-class-name: com.mysql.cj.jdbc.Driver#profiles:#active: @spring.profiles.active@#i18n国际化路径
#  messages:
#    basename: i18n/messages# 邮件mail:default-encoding: utf-8# 配置 SMTP 服务器地址host: smtp.qq.com#发送方邮件名username: 158111920@qq.com#授权码password: btdbkkdkrnxqbijh# thymeleaf模板格式thymeleaf:cache: falseencoding: UTF-8mode: HTMLservlet:content-type: text/htmlprefix: classpath:/templates/suffix: .html# aopaop:auto: trueproxy-target-class: true
#日志
logging:config: classpath:logback-spring.xml
#mybatis-plus
mybatis-plus:global-config:db-config:#主键策略id-type: autofield-strategy: not_empty#驼峰下划线转换column-underline: true#逻辑删除配置logic-delete-value: 0logic-not-delete-value: 1db-type: mysql#声明全局默认类名的对应的表的前缀table-prefix: t_refresh: falseconfiguration:map-underscore-to-camel-case: truecache-enabled: false#打印出sql语句log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
# # mapper-locations: classpath*:com/local/springboot/springbootdao/mapper/**/*Mapper.xml
#  mapper-locations: classpath*:com/local/springboot/springbootdao/mapper/**/*Mapper.xml

SpringbootApiApplication.java启动类修改

package com.local.springboot.springbootapi;import org.mapstruct.Mapper;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.ServletComponentScan;
import springfox.documentation.swagger2.annotations.EnableSwagger2;//@ServletComponentScan
@EnableSwagger2
@SpringBootApplication(scanBasePackages = "com.local.springboot")
@MapperScan(basePackages = "com.local.springboot.*.mapper")
public class SpringbootApiApplication {
    public static void main(String[] args) {
    try {
    SpringApplication.run(SpringbootApiApplication.class, args);}catch (Exception e){
    e.printStackTrace();}}}

6. 代码搬家

将以前的代码搬运过来(不完整),大致结构如下:
SpringBoot —— 简单多模块构建

二、测试

SpringBoot —— 简单多模块构建
启动程序,浏览器输入http://localhost:8080/user/list?pageNumber=1&pageSize=10
SpringBoot —— 简单多模块构建

SpringBoot-简单多模块构建就到这里,还有很多没完善的地方,以后慢慢完善。

? 上一章:SpringBoot —— Filter过滤器的使用
? 下一章:SpringBoot —— 整合RabbitMQ常见问题及解决方案