文章目录
springboot的多模块开发
springboot-high
|–springboot-common 模块 (DTO)
|–springboot-domain 模块 (entity)
|–springboot-service 模块 (业务模块)
|–springboot-web 模块 (页面模块)
1.创建项目springboot-high
父模块的不需要打包,设置内容为pom
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.dyit.springboot</groupId>
<artifactId>springboot-high</artifactId>
<version>1.0-SNAPSHOT</version>
<modules>
<module>springboot-common</module>
<module>springboot-domain</module>
<module>springboot-service</module>
</modules>
<packaging>pom</packaging>
<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
<project.lombok.version>1.18.22</project.lombok.version>
<project.maven.verison>3.1</project.maven.verison>
</properties>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.5.10</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>${project.lombok.version}</version>
</dependency>
</dependencies>
<!--打包插件-->
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>${project.maven.verison}</version>
<configuration>
<encoding>utf-8</encoding>
<source>${java.version}</source>
<target>${java.version}</target>
</configuration>
</plugin>
</plugins>
</build>
</project>
父模块中不需要进行源代码开发,因此我们删除删除src文件夹
idea中不显示某些文件夹或文件
2.在项目中添加模块
1)springboot-common模块
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>springboot-high</artifactId>
<groupId>com.dyit.springboot</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<groupId>com.dyit.springboot.common</groupId>
<artifactId>springboot-common</artifactId>
<description>springboot-high子模块: 定义必备的模块信息</description>
</project>
添加Swagger配置
<dependencies>
<!--swagger3 api测试框架-->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-boot-starter</artifactId>
<version>${project.swagger.version}</version>
</dependency>
</dependencies>
添加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();
}
}
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<Publisher> list = ips.findAll();
return new HttpResp(RespEnum.FIND_ALL_BOOKS, list, new Date());
}
}
2)springboot-domain模块
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>springboot-high</artifactId>
<groupId>com.dyit.springboot</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<groupId>com.dyit.springboot.domain</groupId>
<artifactId>springboot-domain</artifactId>
<description>springboot-high子模块: 定义领域模型</description>
</project>
3)springboot-service模块
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>springboot-high</artifactId>
<groupId>com.dyit.springboot</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<groupId>com.dyit.springboot.service</groupId>
<artifactId>springboot-service</artifactId>
<description>springboot-high子模块: 定义业务模型</description>
</project>
4)springboot-web模块
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>springboot-high</artifactId>
<groupId>com.dyit.springboot</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<groupId>com.dyit.springboot.web</groupId>
<artifactId>springboot-web</artifactId>
<description>springboot-high子模块:静态页面</description>
</project>
多模块开发引入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);
}
}