创建项目并集成流程
- 1:前提准备
- 2:创建springboot项目流程
- 3:集成阿里连接池步骤
- 4:集成swagger方便测试
- 5:书写增删改查进行测试
- 6:项目gitee地址
1:前提准备
-
准备开发工具:idea
-
java环境:jdk1.8
-
安装插件lombook
2:创建springboot项目流程
-
依次选择:File–>New–>Project…
-
选择Spring Initializr,依次填写和修改响应信息。
-
由于官网地址可能因为网络等原因,会有问题,可以修改下镜像
https://start.aliyun.com
-
填写好之后,点击:next
-
选择版本和对应的依赖,确认无误点击:finish
-
创建完成的目录结构
-
修改application.priperties文件的后缀改成yml
-
添加基础的三层架构包,删除demos
控制层:controller
业务逻辑层:service、impl
数据访问层/持久层:mapper
实体:entity
- application.yml 文件添加内容
spring:
application:
# 服务名称
name: fallrain-boot
profiles:
# 选择环境,我这里创建了两个环境文件,dev:开发环境,prod: 正式发布环境
# 正常有这么几个环境
# 1:dev--本地开发环境;
# 2:sit--测试环境;
# 3:uat--准生产环境;
# 4:prod--生产环境;
active: dev
mvc:
pathmatch:
matching-strategy: ant_path_matcher
mybatis:
# 配置mapper.xml位置
mapper-locations: classpath:mapper/**.xml
- application-dev.yml 文件添加内容
server:
# 服务启动端口,可根据情况自定义
port: 8989
spring:
# 数据库连接
datasource:
type: com.alibaba.druid.pool.DruidDataSource
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://127.0.0.1:3306/fallrain_boot?zeroDateTimeBehavior=convertToNull&useUnicode=true&characterEncoding=UTF-8&serverTimezone=Asia/Shanghai&autoReconnect=true&allowMultiQueries=true
username: root
password: admin@123
swagger:
# 是否开启swagger
enable: true
-
yml配置文件截图
-
启动项目
3:集成阿里连接池步骤
- 添加依赖
<!-- 阿里云连接池 -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>${druid.version}</version>
</dependency>
<druid.version>1.2.16</druid.version>
4:集成swagger方便测试
- 引入依赖,放在阿里连接池下面就行
<!-- Swagger API文档 -->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.9.2</version>
<exclusions>
<exclusion>
<groupId>io.swagger</groupId>
<artifactId>swagger-annotations</artifactId>
</exclusion>
<exclusion>
<groupId>io.swagger</groupId>
<artifactId>swagger-models</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.9.2</version>
</dependency>
<dependency>
<groupId>com.github.xiaoymin</groupId>
<artifactId>swagger-bootstrap-ui</artifactId>
<version>1.9.6</version>
</dependency>
<!-- # 增加两个配置解决 NumberFormatException -->
<dependency>
<groupId>io.swagger</groupId>
<artifactId>swagger-annotations</artifactId>
<version>1.5.22</version>
</dependency>
<dependency>
<groupId>io.swagger</groupId>
<artifactId>swagger-models</artifactId>
<version>1.5.22</version>
</dependency>
<!-- Swagger API文档 -->
- 创建配置类:Swagger2Config
package com.fallrain.fallrainspringboot.swagger;
import com.github.xiaoymin.swaggerbootstrapui.annotations.EnableSwaggerBootstrapUI;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;
/**
* @Decription SWAGGER
*/
@Slf4j
@Configuration
@EnableSwagger2
@EnableSwaggerBootstrapUI
@ConditionalOnProperty(name = "swagger.enable", havingValue = "true")
public class Swagger2Config implements WebMvcConfigurer {
/**
*
* 显示swagger-ui.html文档展示页,还必须注入swagger资源:
*
* @param registry
*/
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("swagger-ui.html").addResourceLocations("classpath:/META-INF/resources/");
registry.addResourceHandler("doc.html").addResourceLocations("classpath:/META-INF/resources/");
registry.addResourceHandler("/webjars/**").addResourceLocations("classpath:/META-INF/resources/webjars/");
}
/**
* swagger2的配置文件,这里可以配置swagger2的一些基本的内容,比如扫描的包等等
*
* @return Docket
*/
@Bean
public Docket createRestApi() {
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo())
.select()
//此包路径下的类,才生成接口文档
.apis(RequestHandlerSelectors.basePackage("com.fallrain.fallrainspringboot"))
//加了ApiOperation注解的类,才生成接口文档
.apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class))
.paths(PathSelectors.any())
.build();
// .securitySchemes(Collections.singletonList(securityScheme()));
//.globalOperationParameters(setHeaderToken());
}
/**
* api文档的详细信息函数,注意这里的注解引用的是哪个
*
* @return
*/
private ApiInfo apiInfo() {
return new ApiInfoBuilder()
// //大标题
.title("XX项目-XX管理-API接口文档")
// 版本号
.version("1.0")
// .termsOfServiceUrl("NO terms of service")
// 描述
.description("后台API接口")
// 作者
.contact("XXX团队")
.license("The Apache License, Version 2.0")
.licenseUrl("http://www.apache.org/licenses/LICENSE-2.0.html")
.build();
}
}
- 注意事项:包路径记得修改
.apis(RequestHandlerSelectors.basePackage(“com.fallrain.fallrainspringboot”))
- yml文件配置是否开启swagger
swagger:
# 是否开启swagger
enable: true
5:书写增删改查进行测试
- 引入lombok依赖
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.16</version>
</dependency>
- 书写增删改查
- 测试:http://localhost:8989/doc.html
6:项目gitee地址
https://gitee.com/itliulei/fallrainspringboot.git
数据库脚本:sql/fallrain_boot.sql