SpringBoot —— 简单多模块构建

前言

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项目)

在这里插入图片描述

3. 新建Module

右击项目,选择新建Module


在这里插入图片描述

新建springboot-common、springboot-dao、springboot-service、springboot-api模块(每个子模块的groupId要建议一样)


在这里插入图片描述

全部建好之后,效果如下:
在这里插入图片描述

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包,以及配置打包。



    4.0.0
    
        org.springframework.boot
        spring-boot-starter-parent
        2.5.5
         
    

    com.local.springboot
    springboot-parent
    0.0.1-SNAPSHOT
    springboot-parent
    Demo project for Spring Boot

    
    pom

    
    
        springboot-common
        springboot-dao
        springboot-service
        springboot-api
    

    
        1.8
    

    
    
        
            net.sf.json-lib
            json-lib
            2.4
            jdk15
        
        
            com.alibaba
            fastjson
            1.2.70
        
        
            commons-lang
            commons-lang
            2.5
        
        
            javax.servlet
            javax.servlet-api
            4.0.0
        
    



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



    4.0.0
    com.local.springboot
    springboot-api
    0.0.1-SNAPSHOT
    springboot-api
    Demo project for Spring Boot

    
    
        com.local.springboot
        springboot-parent
        0.0.1-SNAPSHOT
    

    
        
        
            com.local.springboot
            springboot-service
            0.0.1-SNAPSHOT
        
        
        
            org.springframework.boot
            spring-boot-starter-web
        
        
        
            org.springframework.boot
            spring-boot-starter-test
            test
        
        
        
            io.springfox
            springfox-swagger2
            2.9.2
        
        
            io.springfox
            springfox-swagger-ui
            2.9.2
        
        
        
            com.github.xiaoymin
            swagger-bootstrap-ui
            1.9.6
        
        
            junit
            junit
            test
        
        
            junit
            junit
            test
        
    

    
        
            
                org.springframework.boot
                spring-boot-maven-plugin
            
        
    



其他模块都继承父模块,这里需要注意:由于我们把其他模块的启动类删了,父模块的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



    4.0.0
    com.local.springboot
    springboot-dao
    0.0.1-SNAPSHOT
    springboot-dao
    Demo project for Spring Boot

    
    
        com.local.springboot
        springboot-parent
        0.0.1-SNAPSHOT
    

    
        
        
            com.local.springboot
            springboot-common
            0.0.1-SNAPSHOT
        

        
        
            com.baomidou
            mybatis-plus-boot-starter
            3.4.0
        

        
        
            mysql
            mysql-connector-java
        
    


5. 修改application.yml

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

将以前的代码修改

#内置Tomcat容器配置
server:
  port: 8080
  servlet:
    #应用路径,配置应用路径,可方便进行反向代理
    context-path:
spring:
  # 数据源
  datasource:
    url: jdbc:mysql://localhost:3306/local_develop?serverTimezone=Asia/Shanghai&useUnicode=true&characterEncoding=utf-8
    username: root
    password: root
    driver-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: [email protected]
    #授权码
    password: btdbkkdkrnxqbijh
    # thymeleaf模板格式
    thymeleaf:
      cache: false
      encoding: UTF-8
      mode: HTML
      servlet:
        content-type: text/html
      prefix: classpath:/templates/
      suffix: .html
  # aop
  aop:
    auto: true
    proxy-target-class: true
#日志
logging:
  config: classpath:logback-spring.xml
#mybatis-plus
mybatis-plus:
  global-config:
    db-config:
      #主键策略
      id-type: auto
      field-strategy: not_empty
      #驼峰下划线转换
      column-underline: true
      #逻辑删除配置
      logic-delete-value: 0
      logic-not-delete-value: 1
      db-type: mysql
      #声明全局默认类名的对应的表的前缀
      table-prefix: t_
    refresh: false
  configuration:
    map-underscore-to-camel-case: true
    cache-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. 代码搬家

将以前的代码搬运过来(不完整),大致结构如下:


在这里插入图片描述

二、测试

在这里插入图片描述

启动程序,浏览器输入http://localhost:8080/user/list?pageNumber=1&pageSize=10
在这里插入图片描述

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

« 上一章:SpringBoot —— Filter过滤器的使用

创作不易,关注、点赞就是对作者最大的鼓励,欢迎在下方评论留言
求关注,定期分享Java知识,一起学习,共同成长。

你可能感兴趣的:(SpringBoot —— 简单多模块构建)