springboot分模組後臺系統開發詳細過程
阿新 • • 發佈:2021-12-05
新建maven專案(父專案)
父專案下面新建模組--》maven模組
父模組設定
1 <packaging>pom</packaging>
父模組引入依賴管理----<dependencyManagement>
注意:這兩個需要加上
<type>pom</type>
<scope>import</scope>
1 <dependencyManagement> 2 <dependencies> 3 <dependency> 4 <groupId>org.springframework.boot</groupId> 5 <artifactId>spring-boot-dependencies</artifactId> 6 <version>2.2.12.RELEASE</version> 7 <!-- 註明是引入父模組 --> 8 <type>pom</type> 9 <!--註明引入父模組的jar包 --> 10 <scope>import</scope> 11 </dependency> 12 </dependencies> 13 </dependencyManagement>
父模組引入公共模組
1 <dependencies> 2 <!--Lombok--> 3 <dependency> 4 <groupId>org.projectlombok</groupId> 5 <artifactId>lombok</artifactId> 6 <scope>provided</scope> 7 </dependency> 8 <!-- swagger2--> 9 <dependency> 10 <groupId>io.springfox</groupId> 11 <artifactId>springfox-swagger2</artifactId> 12 <version>${swagger.version}</version> 13 <exclusions> 14 <exclusion> 15 <groupId>io.swagger</groupId> 16 <artifactId>swagger-annotations</artifactId> 17 </exclusion> 18 <exclusion> 19 <groupId>io.swagger</groupId> 20 <artifactId>swagger-models</artifactId> 21 </exclusion> 22 </exclusions> 23 </dependency> 24 <!--防止進入swagger頁面報型別轉換錯誤,排除2.9.2中的引用,手動增加1.5.21版本--> 25 <dependency> 26 <groupId>io.swagger</groupId> 27 <artifactId>swagger-annotations</artifactId> 28 <version>1.5.21</version> 29 </dependency> 30 31 <dependency> 32 <groupId>io.swagger</groupId> 33 <artifactId>swagger-models</artifactId> 34 <version>1.5.21</version> 35 </dependency> 36 <!-- swagger2-UI--> 37 <dependency> 38 <groupId>io.springfox</groupId> 39 <artifactId>springfox-swagger-ui</artifactId> 40 <version>${swagger.version}</version> 41 </dependency> 42 </dependencies>
父模組將編譯的java版本和專案的java版本設定為一致
1 <build> 2 <plugins> 3 <!-- 將編譯的java版本和專案的java版本設定為一致 --> 4 <plugin> 5 <groupId>org.apache.maven.plugins</groupId> 6 <artifactId>maven-compiler-plugin</artifactId> 7 <version>3.1</version> 8 <configuration> 9 <source>${java.version}</source> 10 <target>${java.version}</target> 11 <encoding>${project.build.sourceEncoding}</encoding> 12 </configuration> 13 </plugin> 14 </plugins> 15 </build>
framework子模組依賴引入
1 <artifactId>framwork</artifactId>
1 <dependencies> 2 <dependency> 3 <groupId>org.springframework.boot</groupId> 4 <artifactId>spring-boot-starter-web</artifactId> 5 </dependency> 6 </dependencies>
admin子模組依賴引入
生命週期範圍--<scope>compile</scope>
1 <parent> 2 <artifactId>fullSpringBootProject</artifactId> 3 <groupId>org.example</groupId> 4 <version>1.0-SNAPSHOT</version> 5 </parent> 6 <modelVersion>4.0.0</modelVersion> 7 <packaging>jar</packaging> 8 <artifactId>admin</artifactId> 9 10 <dependencies> 11 <dependency> 12 <groupId>org.example</groupId> 13 <artifactId>framwork</artifactId> 14 <version>1.0-SNAPSHOT</version> 15 </dependency> 16 <dependency> 17 <groupId>org.example</groupId> 18 <artifactId>common</artifactId> 19 <version>1.0-SNAPSHOT</version> 20 <scope>compile</scope> 21 </dependency> 22 </dependencies>
admin子模組建目錄
admin子模組建立AdminApplication啟動類
1 @ComponentScan(basePackages = {"com.wxadmin.web.controller", 2 "com.wxcommon.core", 3 "com.wxsystem.mapper"}) 4 @MapperScan("com.wxsystem.mapper") 5 6 //@SpringBootApplication(exclude = { DataSourceAutoConfiguration.class }) 7 @SpringBootApplication(exclude = { DataSourceAutoConfiguration.class }) 8 public class AdminApplication { 9 public static void main(String[] args) { 10 SpringApplication.run(AdminApplication.class,args); 11 System.out.println("系統啟動成功!"); 12 } 13 }
admin模組resource目錄下建立application.yml配置檔案
配置swagger2
swagger: enabled: true #pathMapping: /test
建立SwaggerConfig配置類
1 //標誌為一個配置類 2 @Configuration 3 //開啟配置 4 @EnableSwagger2 5 public class SwaggerConfig { 6 7 @Autowired 8 private SysBasicInfoConfig sysBasicInfoConfig; 9 10 /** 是否開啟swagger */ 11 @Value("${swagger.enabled}") 12 private boolean enabled; 13 14 /** 設定請求的統一字首 */ 15 @Value("${swagger.pathMapping}") 16 private String pathMapping; 17 18 @Bean 19 public Docket createRestApi() 20 { 21 return new Docket(DocumentationType.SWAGGER_2) 22 // 是否啟用Swagger 23 .enable(enabled) 24 // 用來建立該API的基本資訊,展示在文件的頁面中(自定義展示的資訊) 25 .apiInfo(apiInfo()) 26 // 設定哪些介面暴露給Swagger展示 27 .select() 28 // 掃描所有有註解的api,用這種方式更靈活 29 .apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class)) 30 // 掃描指定包中的swagger註解 31 // .apis(RequestHandlerSelectors.basePackage("com.joolun.project.tool.swagger")) 32 // 掃描所有 .apis(RequestHandlerSelectors.any()) 33 .paths(PathSelectors.any()) 34 .build(); 35 /* 設定安全模式,swagger可以設定訪問token */ 36 //.securitySchemes(securitySchemes()) 37 //.securityContexts(securityContexts()) 38 //.pathMapping(pathMapping); 39 } 40 /** 41 * 新增摘要資訊 42 */ 43 private ApiInfo apiInfo() 44 { 45 // 用ApiInfoBuilder進行定製 46 return new ApiInfoBuilder() 47 // 設定標題 48 .title("標題:系統介面文件") 49 // 描述 50 .description("描述:用於管理集團旗下公司的人員資訊,具體包括XXX,XXX模組...") 51 // 作者資訊 52 //.contact(new Contact(sysBasicInfoConfig.getName(), null, null)) 53 .contact(new Contact("Husong", null, null)) 54 // 版本 55 //.version("版本號:" + sysBasicInfoConfig.getVersion()) 56 .version("版本號:" + "V1.0") 57 .build(); 58 } 59 }
admin模組建立controller
建立測試swagger
1 //swagger的註解 2 @Api 3 @RestController 4 public class TestController { 5 @ApiOperation(value = "desc of method", notes = "test") 6 @GetMapping("/test") 7 public String Test(){ 8 return "hello"; 9 } 10 }