谷粒 | 01 前后端分离项目环境搭建
搭建环境
创建父工程(管理依赖版本)
1、创建一个空的springboot项目,不需要添加任何依赖
2、删除src目录
3、配置pom.xml
1)先选定springboot版本
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.1.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
2)
<artifactId>guli_parent</artifactId>
<packaging>pom</packaging>
3)在pom.xml中添加依赖的版本
<!-- 删除原有的dependencies依赖,以下内容删除 -->
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
4)添加
<properties>
<java.version>1.8</java.version>
<guli.version>0.0.1-SNAPSHOT</guli.version>
<mybatis-plus.version>3.0.5</mybatis-plus.version>
<velocity.version>2.0</velocity.version>
<swagger.version>2.7.0</swagger.version>
<aliyun.oss.version>2.8.3</aliyun.oss.version>
<jodatime.version>2.10.1</jodatime.version>
<poi.version>3.17</poi.version>
<commons-fileupload.version>1.3.1</commons-fileupload.version>
<commons-io.version>2.6</commons-io.version>
<httpclient.version>4.5.1</httpclient.version>
<jwt.version>0.7.0</jwt.version>
<aliyun-java-sdk-core.version>4.3.3</aliyun-java-sdk-core.version>
<aliyun-sdk-oss.version>3.1.0</aliyun-sdk-oss.version>
<aliyun-java-sdk-vod.version>2.15.2</aliyun-java-sdk-vod.version>
<aliyun-java-vod-upload.version>1.4.11</aliyun-java-vod-upload.version>
<aliyun-sdk-vod-upload.version>1.4.11</aliyun-sdk-vod-upload.version>
<fastjson.version>1.2.28</fastjson.version>
<gson.version>2.8.2</gson.version>
<json.version>20170516</json.version>
<commons-dbutils.version>1.7</commons-dbutils.version>
<canal.client.version>1.1.0</canal.client.version>
<docker.image.prefix>zx</docker.image.prefix>
<cloud-alibaba.version>0.2.2.RELEASE</cloud-alibaba.version>
</properties>
5)配置
部份依赖示例
<dependencyManagement>
<dependencies>
<!--Spring Cloud-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Hoxton.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-alibaba-dependencies</artifactId>
<version>${cloud-alibaba.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<!--mybatis-plus 持久层-->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>${mybatis-plus.version}</version>
</dependency>
<!-- velocity 模板引擎, Mybatis Plus 代码生成器需要 -->
<dependency>
<groupId>org.apache.velocity</groupId>
<artifactId>velocity-engine-core</artifactId>
<version>${velocity.version}</version>
</dependency>
<!--swagger-->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>${swagger.version}</version>
</dependency>
<!--swagger ui-->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>${swagger.version}</version>
</dependency>
</dependencies>
</dependencyManagement>
创建子工程(实际业务)
子工程是Maven项目,不是springboot项目
若子工程中还也子工程也需要添加 <packaging>pom</packaging>
子工程中添加依赖,不需要版本号,父工程中已限制版本号,示例如下
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-ribbon</artifactId>
</dependency>
<!--hystrix依赖,主要是用 @HystrixCommand -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
</dependency>
<!--服务注册-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>
<!--服务调用-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
</dependencies>
创建启动类、配置类
子工程虽然创建的是maven工程,但本质上还是springboot工程,需要创建启动类
注意:启动类要直接创建在项目模块下,即与各层级例controller、entity等同级
![](搭建环境.assets/2196407-20201230154103692-1411192833.png)
@SpringBootApplication
public class EduApplication {
public static void main(String[] args) {
SpringApplication.run(EduApplication.class, args);
}
}
项目配置最好是配置在单独创建的配置类上,不要写在启动类上,一个注解搞定 : @Configuration
@Configuration
@MapperScan("com.birdy.eduservice.mapper")
public class EduConfig {
}
项目环境搭建完毕!!
配置swagger接口测试
1、在父工程下创建公共子模块common,在common模块中引入项目依赖,
因为common属于公共模块不写代码,所以需要将相关依赖抽取出来,再创建一个子模块,在子模块中进行相关配置
2、在common中创建子模块service_base,整合某一个功能,以整合swagger为例
在service_base中创建swagger配置类,目录结构同业务模块
@Configuration
@EnableSwagger2 //使用swagger注解
public class Swaggerconfig {
@Bean
public Docket webApiConfig(){
return new Docket(DocumentationType.SWAGGER_2)
.groupName("webApi") //自定义分组
.apiInfo(webApiInfo())
.select()
.paths(Predicates.not(PathSelectors.regex("/admin/.*"))) //设置不显示哪些信息
.paths(Predicates.not(PathSelectors.regex("/error.*")))
.build();
}
/*
在线文档信息配置,见名知意
*/
private ApiInfo webApiInfo(){
return new ApiInfoBuilder()
.title("网站-课程中心API文档") //在线文档标题
.description("本文档描述了课程中心微服务接口定义")
.version("1.0")
.contact(new Contact("birdy", "https://www.cnblogs.com/birdyBlob/", "123131@qq.com"))
.build();
}
}
3、在业务模块中引入抽取出来的公共模块
<dependency>
<groupId>com.birdy</groupId>
<artifactId>service_base</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
4、设置启动类扫描公共模块swagger配置类@componentScan
@SpringBootApplication
@ComponentScan("com.birdy")
public class EduApplication {
public static void main(String[] args) {
SpringApplication.run(EduApplication.class, args);
}
}
@componentScan("com.birdy")
会扫描所有模块中的com.birdy包下的文件,不加注解只会扫描当前模块文件swagger配置类也放在包com.birdy下,所有模块只要有代码都按照统一的路径存放各类型文件
5、访问在线文档
http://localhost:8001/swagger-ui.html
6、增加备注信息
-
控制器注解
@Api(description="讲师管理")
-
方法注解
ApiOperation(value = "查询所有讲师列表")
-
参数注解
public boolean deleteTeacher(
@ApiParam(name = "id", value = "讲师id", required = true) @PathVariable("id") String id)