springboot的多模块开发
1.创建项目springboot-high2.在项目中添加模块
1)springboot-common模块2)springboot-domain模块3)springboot-service模块4)springboot-web模块
多模块开发引入jpa的实体类问题
springboot的多模块开发1.创建项目springboot-highspringboot-high
|–springboot-common 模块 (DTO)
|–springboot-domain 模块 (entity)
|–springboot-service 模块 (业务模块)
|–springboot-web 模块 (页面模块)
父模块的不需要打包,设置内容为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中不显示某些文件夹或文件
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模块
3)springboot-service模块springboot-high com.dyit.springboot 1.0-SNAPSHOT 4.0.0 com.dyit.springboot.domain springboot-domain springboot-high子模块: 定义领域模型
4)springboot-web模块springboot-high com.dyit.springboot 1.0-SNAPSHOT 4.0.0 com.dyit.springboot.service springboot-service springboot-high子模块: 定义业务模型
多模块开发引入jpa的实体类问题springboot-high com.dyit.springboot 1.0-SNAPSHOT 4.0.0 com.dyit.springboot.web springboot-web springboot-high子模块:静态页面
需要扫描其他模块实体类位置要加上@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);
}
}



