• springboot的多模块开发


    文章目录

    springboot的多模块开发

    springboot-high

    |–springboot-common 模块 (DTO)

    |–springboot-domain 模块 (entity)

    |–springboot-service 模块 (业务模块)

    |–springboot-web 模块 (页面模块)

    1.创建项目springboot-high

    父模块的不需要打包,设置内容为pom

    
    
        4.0.0
    
        com.dyit.springboot
        springboot-high
        1.0-SNAPSHOT
        
            springboot-common
            springboot-domain
            springboot-service
        
        pom
    
        
            8
            8
            1.18.22
            3.1
        
    
        
            org.springframework.boot
            spring-boot-starter-parent
            2.5.10
        
    
        
            
                org.springframework.boot
                spring-boot-starter-web
            
            
                org.projectlombok
                lombok
                ${project.lombok.version}
            
        
    
        
        
            
                
                    org.apache.maven.plugins
                    maven-compiler-plugin
                    ${project.maven.verison}
                    
                        utf-8
                        ${java.version}
                        ${java.version}
                    
                
            
        
    
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58

    父模块中不需要进行源代码开发,因此我们删除删除src文件夹

    idea中不显示某些文件夹或文件
    在这里插入图片描述

    2.在项目中添加模块

    在这里插入图片描述

    在这里插入图片描述

    1)springboot-common模块

    
    
        
            springboot-high
            com.dyit.springboot
            1.0-SNAPSHOT
        
        4.0.0
    
        com.dyit.springboot.common
        springboot-common
        springboot-high子模块: 定义必备的模块信息
    
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16

    添加Swagger配置

    
        
        
            io.springfox
            springfox-boot-starter
            ${project.swagger.version}
        
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    添加Swagger配置类

    package com.dyit.springboot.common.config;
    
    import io.swagger.v3.oas.annotations.Operation;
    import org.springframework.context.annotation.Configuration;
    import springfox.documentation.builders.ApiInfoBuilder;
    import springfox.documentation.builders.PathSelectors;
    import springfox.documentation.builders.RequestHandlerSelectors;
    import springfox.documentation.service.ApiInfo;
    import springfox.documentation.service.Contact;
    import springfox.documentation.spi.DocumentationType;
    import springfox.documentation.spring.web.plugins.Docket;
    
    /**
     * Swagger配置类
     */
    @Configuration
    public class SwaggerConfiguration {
    
        public Docket createRestApi() {
            return new Docket(DocumentationType.OAS_30)
                    .apiInfo(apiInfo())
                    .select()
                    .apis(RequestHandlerSelectors.withMethodAnnotation(Operation.class))
                    .paths(PathSelectors.any())
                    .build();
        }
    
    
        private ApiInfo apiInfo() {
            return new ApiInfoBuilder()
                    .title("springboot整合swagger3的接口文档")
                    .description("描述图书管理的接口文档")
                    .contact(new Contact("非凡boot", "http://www.diyt.com", "dyit@dyit.com"))
                    .version("1.0")
                    .build();
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37

    DTO注解

    @Schema(description = "DTO")
    @Data
    @NoArgsConstructor
    @AllArgsConstructor
    public class HttpResp {
        @Schema(description = "代码和描述")
        RespEnum resp;
        @Schema(description = "返回结果")
        private Object results;
        @Schema(description = "DTO对象创建的时间")
        private Date date;
    }
    
    
    package com.dyit.springboot.book.controller;
    
    import com.dyit.springboot.book.service.IPublisherSevice;
    import com.dyit.springboot.common.dto.HttpResp;
    import com.dyit.springboot.common.dto.RespEnum;
    import com.dyit.springboot.domain.entity.Publisher;
    
    import io.swagger.v3.oas.annotations.Operation;
    import io.swagger.v3.oas.annotations.tags.Tag;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.web.bind.annotation.GetMapping;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RestController;
    
    import java.util.Date;
    import java.util.List;
    @Tag(name ="出版社模块")
    @RestController
    @RequestMapping("/api/publisher")
    
    public class PublisherController {
        @Autowired
        private IPublisherSevice ips;
    
        @Operation(summary = "获取所以出版社信息方法")
        @GetMapping("/findAll")
        public HttpResp findAll() {
            List list = ips.findAll();
          
            return new HttpResp(RespEnum.FIND_ALL_BOOKS, list, new Date());
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46

    2)springboot-domain模块

    
    
        
            springboot-high
            com.dyit.springboot
            1.0-SNAPSHOT
        
        4.0.0
    
        com.dyit.springboot.domain
        springboot-domain
        springboot-high子模块: 定义领域模型
    
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16

    3)springboot-service模块

    
    
        
            springboot-high
            com.dyit.springboot
            1.0-SNAPSHOT
        
        4.0.0
    
        com.dyit.springboot.service
        springboot-service
        springboot-high子模块: 定义业务模型
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15

    4)springboot-web模块

    
    
        
            springboot-high
            com.dyit.springboot
            1.0-SNAPSHOT
        
        4.0.0
    
        com.dyit.springboot.web
        springboot-web
        springboot-high子模块:静态页面
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    多模块开发引入jpa的实体类问题

    需要扫描其他模块实体类位置要加上@EntityScan,不然会报错,因为默认是只扫描本模块@EntityScan(basePackages = “其它模块entity包的位置”)

    @SpringBootApplication
    @EntityScan(basePackages = "com.dyit.springboot.domain.entity")
    public class ServiceApp {
    
        public static void main(String[] args) {
            SpringApplication.run(ServiceApp.class);
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
  • 相关阅读:
    参与开源社区还有证书拿?
    CSS入门
    全自动深腔键合机DSP+FPGA控制器解决方案
    细讲Java连接Kafka构建生产者和消费者
    Unity学习之Shader
    Linux 桌面修改文件mime类型图标
    「Java开发指南」如何用MyEclipse搭建Spring MVC应用程序?(一)
    配置元数据存储为MySql&再次启动测试---大数据之Hive工作笔记0008
    企业微信管理客户如何管理?
    uni-app 超详细教程(从菜鸟到大佬)
  • 原文地址:https://blog.csdn.net/m0_67392931/article/details/126515322