栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

springboot的多模块开发

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

springboot的多模块开发

文章目录

springboot的多模块开发

1.创建项目springboot-high2.在项目中添加模块

1)springboot-common模块2)springboot-domain模块3)springboot-service模块4)springboot-web模块

多模块开发引入jpa的实体类问题

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}
                
            
        
    


父模块中不需要进行源代码开发,因此我们删除删除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子模块: 定义必备的模块信息


添加Swagger配置


    
    
        io.springfox
        springfox-boot-starter
        ${project.swagger.version}
    

添加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;


@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();
    }
}

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());
    }
}

2)springboot-domain模块


    
        springboot-high
        com.dyit.springboot
        1.0-SNAPSHOT
    
    4.0.0

    com.dyit.springboot.domain
    springboot-domain
    springboot-high子模块: 定义领域模型


3)springboot-service模块


    
        springboot-high
        com.dyit.springboot
        1.0-SNAPSHOT
    
    4.0.0

    com.dyit.springboot.service
    springboot-service
    springboot-high子模块: 定义业务模型

4)springboot-web模块


    
        springboot-high
        com.dyit.springboot
        1.0-SNAPSHOT
    
    4.0.0

    com.dyit.springboot.web
    springboot-web
    springboot-high子模块:静态页面

多模块开发引入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);
    }
}
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/777679.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号