SpringBoot图文教程16—SpringBoot 多模块开发「web」「打包」

SpringBoot图文教程16—SpringBoot 多模块开发「web」「打包」

有天上飞的概念,就要有落地的实现

概念十遍不如代码一遍,朋友,希望你把文中所有的代码案例都敲一遍

先赞后看,养成习惯

SpringBoot 图文教程系列文章目录

  1. SpringBoot图文教程1—SpringBoot+Mybatis 环境搭建
  2. SpringBoot图文教程2—日志的使用「logback」「log4j」
  3. SpringBoot图文教程3—「‘初恋’情结」集成Jsp
  4. SpringBoot图文教程4—SpringBoot 实现文件上传下载
  5. SpringBoot图文教程5—SpringBoot 中使用Aop
  6. SpringBoot图文教程6—SpringBoot中过滤器的使用
  7. SpringBoot图文教程7—SpringBoot拦截器的使用姿势这都有
  8. SpringBoot图文教程8—SpringBoot集成MBG「代码生成器」
  9. SpringBoot图文教程9—SpringBoot 导入导出 Excel 「Apache Poi」
  10. SpringBoot图文教程10—模板导出|百万数据Excel导出|图片导出「easypoi」
  11. SpringBoot图文教程11—从此不写mapper文件「SpringBoot集成MybatisPlus」
  12. SpringBoot图文教程12—SpringData Jpa的基本使用
  13. SpringBoot图文教程13—SpringBoot+IDEA实现代码热部署
  14. SpringBoot图文教程14—阿里开源EasyExcel「为百万数据读写设计」
  15. SpringBoot图文教程15—项目异常怎么办?「跳转404错误页面」「全局异常捕获」

前言

本文已经收录码云仓库:https://gitee.com/bingqilinpeishenme/Java-Tutorials 本文涉及源码下载地址:https://gitee.com/bingqilinpeishenme/multi-module-demo

SpringBoot图文教程16—SpringBoot 多模块开发「web」「打包」

什么是多模块开发?如图所示,项目中每一个包对应都是一个完整的项目,在IDEA中称之为模块,每一个模块都有完整的项目结构:独立的pom文件,独立的配置文件,独立的编译文件输出模块等等。

那么这样项目结构的项目是如何设计出来的呢?

SpringBoot图文教程16—SpringBoot 多模块开发「web」「打包」

SpringBoot 多模块开发

技术选型:

SpringBoot MybatisPlus MybatisPlus教程见:SpringBoot图文教程11—从此不写mapper文件「SpringBoot集成MybatisPlus」 Mysql 多模块开发效果图如下:

父级工程开发


父级工程可以用来统一管理所有项目的依赖,如图,如果在父级项目中有一个mysql依赖,那么所有继承这个父级项目的子项目中也会继承到mysql的依赖

1.创建一个Project

SpringBoot图文教程16—SpringBoot 多模块开发「web」「打包」

SpringBoot图文教程16—SpringBoot 多模块开发「web」「打包」

SpringBoot图文教程16—SpringBoot 多模块开发「web」「打包」

SpringBoot图文教程16—SpringBoot 多模块开发「web」「打包」

2.对IDEA做一些项目基本的配置

  • 字符编码配置
  • 注解生效激活
  • Java编译版本选择

3.写父级项目的pom文件

pom文件的详细内容见注释

<code>

    4.0.0
 
    
        org.springframework.boot
        spring-boot-starter-parent
        2.1.3.RELEASE
          
    
    com.lby
    multi-module-demo
    1.0-SNAPSHOT
     
    pom

     
    
        1.8
         
        1.18.4
    

    
        
            org.projectlombok
            lombok
             
            ${lombok-version}
            provided
        
    

     
    
        
             
            
                com.baomidou
                mybatis-plus-boot-starter
                3.3.1.tmp
            

             
            
                com.alibaba
                druid
                1.0.19
            

             
            
                mysql
                mysql-connector-java
                5.1.38
            

        
    

/<code>

注意:

  • 父级项目的packing必须设置为 pom
  • dependencies 和 DependencyManagement 的区别 dependencies 在当前项目中引入依赖,如果子项目继承了该项目,也会在子项目中引入依赖 DependencyManagement 只是声明依赖,并不实际引入,因此子项目需要显式声明需要用到的依赖 如果在子项目中声明依赖,是不会从父项目中继承下来的,只有在子项目中写了该依赖项,并且没有执行具体版本,才会从父项目中继承该项,并且version和scope都读取自父pom 如果子项目指定了版本号,那么会使用子项目中指定的jar版本子项目开发

子项目开发的步骤如下:

基于Project创建module 修改pom 写配置,没有可以不写 写代码

1.创建multi-entity

1.基于Project创建module

SpringBoot图文教程16—SpringBoot 多模块开发「web」「打包」

创建完multi-entity后打开pom可以看到

此时打开父级项目的pom 会看到

2.修改pom

完整的multi-entity项目的pom如下:

<code>

     
    
        multi-module-demo
        com.lby
        1.0-SNAPSHOT
    

    4.0.0

    multi-entity

    
        
            com.baomidou
            mybatis-plus-boot-starter
        
    

/<code>

3.在项目中写入实体类

<code>package com.lby.entity;

import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import lombok.Data;

@TableName("cmfz_admin")
@Data
public class Admin {
    /**
     * 主键属性  @TableId
     *
     * value 该属性对应的数据库表中的字段名
     * type 主键自增的类型 AUTO 代表自动递增
     */
    @TableId(value = "id",type = IdType.AUTO)
    private Integer id;
    /**
     * 非主键属性  @TableField
     *  @TableField("username")  参数为该属性对应的数据库表中的字段名
     *
     */
    private String username;

    private String password;

    private String salt;

}
/<code>

2.按照上述步骤创建 multi-dao

对于dao模块而言,不同的地方在于,在multi-dao中需要使用到 multi-entity中的实体类,但是怎么在dao模块中使用到另一个项目中的实体类呢?

将multi-entity像依赖一样导入 multi-dao中

SpringBoot图文教程16—SpringBoot 多模块开发「web」「打包」

multi-dao完整pom文件

<code>

    
        multi-module-demo
        com.lby
        1.0-SNAPSHOT
    
    4.0.0

    multi-dao

    
 
        
            com.lby
            multi-entity
            1.0-SNAPSHOT
        

         
         
        
            mysql
            mysql-connector-java
        

        
            com.alibaba
            druid
        


    

/<code>

在multi-dao写入dao接口

<code>package com.lby.dao;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.lby.entity.Admin;

public interface AdminDao extends BaseMapper {
}
/<code>

3.创建 multi-service 模块

完整pom文件

<code>

    
        multi-module-demo
        com.lby
        1.0-SNAPSHOT
    
    4.0.0

    multi-service

    
 
        
            com.lby
            multi-dao
            1.0-SNAPSHOT
        
    
/<code>

写入业务类

<code>package com.lby.service;

import com.lby.dao.AdminDao;
import com.lby.entity.Admin;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 之所以能够直接使用@Service注解
 * 是因为 multi-service 模块 依赖了 multi-dao
 * multi-dao 依赖了 multi-entity
 * multi-entity 中的 MybatisPlus 依赖在项目中导入了Spring的jar包
 */
@Service
public class AdminService {
    @Autowired
    private AdminDao adminDao;

    public List adminList(){
        return adminDao.selectList(null);
    }

}
/<code>

4.创建 multi-controller 模块

multi-controller 模块是启动类所在的模块 所以我们把配置文件 启动类以及SpringBoot集成maven的插件都放在这个项目中

1.完整的pom文件

<code>

    
        multi-module-demo
        com.lby
        1.0-SNAPSHOT
    
    4.0.0

    multi-controller

    
 
        
            com.lby
            multi-service
            1.0-SNAPSHOT
        

 
        
            org.springframework.boot
            spring-boot-starter-web
        

         
        
            org.springframework.boot
            spring-boot-starter-test
            test
        
    

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

/<code>

2.application配置文件配置

<code>#配置端口号
server:
  port: 8802
#数据源的配置
spring:
  datasource:
    driver-class-name: com.mysql.jdbc.Driver
    url: jdbc:mysql://localhost:3306/cmfz?useUnicode=true&characterEncoding=UTF-8
    username: root
    password: 123456
    type: com.alibaba.druid.pool.DruidDataSource

#mybatis-plus
mybatis-plus:
  mapper-locations: classpath:mapper/*Mapper.xml

# root 全局日志等级 默认是info 可以修改 debug warn error
logging:
  level:
    root: info
    com.baizhi: debug

/<code>

3.启动类代码

<code>package com.lby;

import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@MapperScan("com.lby.dao")
@SpringBootApplication
public class AppRun {

    public static void main(String[] args) {
        SpringApplication.run(AppRun.class,args);
    }
}
/<code>

4.controller代码

<code>package com.lby.controller;

import com.lby.entity.Admin;
import com.lby.service.AdminService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
public class AdminController {
    @Autowired
    private AdminService adminService;

    @RequestMapping("adminList")
    public List adminList(){
        return adminService.adminList();
    }
}
/<code>

5.启动项目

通过启动类启动项目

SpringBoot图文教程16—SpringBoot 多模块开发「web」「打包」

访问地址:http://localhost:8802/adminList 可以看到如下效果

SpringBoot图文教程16—SpringBoot 多模块开发「web」「打包」

如果要使用插件启动 需要先对父项目进行 clean 和 install操作


6.测试

编写测试类

SpringBoot图文教程16—SpringBoot 多模块开发「web」「打包」

运行测试方法 效果如下

SpringBoot图文教程16—SpringBoot 多模块开发「web」「打包」

7.打包项目

注意:启动类在哪个模块,就通过哪个模块打包

通过maven打包项目

SpringBoot图文教程16—SpringBoot 多模块开发「web」「打包」

找到打包好的项目 通过java -jar运行项目

SpringBoot图文教程16—SpringBoot 多模块开发「web」「打包」

访问地址:http://localhost:8802/adminList 可以看到如下效果

SpringBoot图文教程16—SpringBoot 多模块开发「web」「打包」

常见问题处理

1.循环依赖问题

SpringBoot图文教程16—SpringBoot 多模块开发「web」「打包」

错误信息提示:

<code>Error:java: Annotation processing is not supported for module cycles. Please ensure that all modules from cycle [qrcode-common,qrcode-manager-pojo] are excluded from annotation processing
/<code>

原因分析:循环依赖 死循环了

SpringBoot图文教程16—SpringBoot 多模块开发「web」「打包」

解决方案:修改模块中的依赖

2.IDEA修改pom不生效问题

在使用IDEA开发多项目的时候发现这样一个问题:修改pom文件之后,不管怎么刷新都不生效

SpringBoot图文教程16—SpringBoot 多模块开发「web」「打包」

解决方案:重启 IDEA 即可

总结

本文涉及源码下载地址:https://gitee.com/bingqilinpeishenme/multi-module-demo

恭喜你完成了本章的学习,为你鼓掌!如果本文对你有帮助,请帮忙点赞,评论,转发,这对作者很重要,谢谢。

SpringBoot图文教程16—SpringBoot 多模块开发「web」「打包」

让我们再次回顾本文的学习目标

掌握SpringBoot中多模块开发

要掌握SpringBoot更多的用法,请持续关注本系列教程。

求关注,求点赞,求转发

欢迎关注本人公众号:鹿老师的Java笔记,将在长期更新Java技术图文教程和视频教程,Java学习经验,Java面试经验以及Java实战开发经验。

SpringBoot图文教程16—SpringBoot 多模块开发「web」「打包」


分享到:


相關文章: