SPring boot2创建一个空文件及其相关配置
注1:用于个人速记,非教程
注2:同时有swagger2的简单用法(hello)
前提是你的maven需要下载安装好,并且配置了最新的阿里云网站镜像(有可能阿里云换了网址也说不定)
几点注意事项:
第一,创建项目时,package name不要写长了 一个com.xxx就够了
第二、选择依赖时,要选Spring web;lombok;Thymeleaf;Mybatis ;mysql driver。但是如果要使用swagger的话,需要手动自己添加两个swagger的依赖
第三、用easycode生成代码时,不要选“统一配置”,记得package要选成com.xxx
第四、需要在application的main文件里添加一个mapscan(com.xxx.dao)保证dao层能被扫描上
第五、配置文件里的mybatis.type-aliases-package记得改一下,改成自己的entity层
1.选择JDK路径,JAVA8版本,如果server url出错,请访问:
点击访问
这里有个点需要注意:第一行的Server URL默认是spring官网的地址,这个的效果等同于去官网创建,如下:
官网快速创建项目的网址:https://spring.io/projects/spring-boot
一般国内的话会把这个替换成阿里云的地址:https://start.aliyun.com
2.选择Spring web;lombok;Thymeleaf;Mybatis Framwork;mysql driver(供5个依赖)
maven依赖有哪些:在这个网址里查 https://mvnrepository.com/
Thymeleaf 模板引擎:一篇就够了
Lombok:以简单的注解形式来简化java代码
MyBatis 是一款优秀的持久层框架,它支持自定义 SQL、存储过程以及高级映射
springframework 是spring 里面的一个基础开源框架,主要用于javaee的企业开发
3.在file-setting里选择maven路径
4.把左边不需要的东西删掉,只剩如下的
5.增加mysql的相关依赖,以及Swagger的相关遗依赖(图忘截图了)
<!--Mybatis starter-->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.2.0</version>
</dependency>
<!--mysql数据库驱动-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.25</version>
</dependency>
<!--SWagger依赖-->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.9.2</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.9.2</version>
</dependency>
6.为连接mysql设置配置文件
spring.datasource.url=jdbc:mysql://localhost:3306/keshe?useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&allowMultiQueries=true&sessionVariables=sql_mode='NO_ENGINE_SUBSTITUTION'
spring.datasource.username=root
spring.datasource.password=2333
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
注意:
1、这里localhost:3306是本地数据库连接的端口号
2、keshe是我的数据库连接下面的一个数据库名
3、username=root,password=2333 是我的账号密码
7.如图建包、建类SwaggerConfig用于配置Swagger
8.写一下这个类,即写文档的基本信息
@Configuration
@EnableSwagger2
public class SwaggerConfig {
@Bean
public Docket docket() {//将默认的配置信息给覆盖掉
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo())
.select()
.apis(RequestHandlerSelectors.basePackage("com.example.day7_9test1.Controller"))
.paths(PathSelectors.any())
.build();
}
private ApiInfo apiInfo() {
return new ApiInfoBuilder()
.title("ywy的swagger文档标题")
.description("接口说明")
.version("1.0.0")
.build();
}
}
下面这个swaggerConfig是带注释版本的:
@Configuration
@EnableSwagger2
public class SwaggerConfig {
@Bean
public Docket createRestApi() {
ParameterBuilder tokenPar = new ParameterBuilder();
List<Parameter> pars = new ArrayList<Parameter>();
tokenPar.name("token").description("令牌").modelRef(new ModelRef("string")).parameterType("header").required(false).build();
pars.add(tokenPar.build());
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo())
//是否开启 (true 开启 false隐藏。生产环境建议隐藏)
//.enable(false)
.select()
//扫描的路径包,设置basePackage会将包下的所有被@Api标记类的所有方法作为api
.apis(RequestHandlerSelectors.basePackage("com.lailedahuo.inv_control_system.controller"))
//指定路径处理PathSelectors.any()代表所有的路径
.paths(PathSelectors.any())
.build()
.globalOperationParameters(pars);
}
private ApiInfo apiInfo() {
return new ApiInfoBuilder()
//设置文档标题(API名称)
.title("SpringBoot中使用Swagger2接口规范")
//文档描述
.description("接口说明")
//版本号
.version("1.0.0")
.build();
}
}
9.写一个hellocontroller测试一下
注意:对于项目的Application文件需要加一个注解扫描dao层
@MapperScan("com.example.dentures2.dao")
如:
@SpringBootApplication
@MapperScan("com.example.dentures2.dao")//自动生成的文件没有这一行
public class Dentures2Application {
public static void main(String[] args) {
SpringApplication.run(Dentures2Application.class, args);
}
}
10.结尾
输入这个地址就能看到自己的swagger文档
http://localhost:8080/swagger-ui.html
//附一个引入mybatis后的配置文件
# 应用服务端口
server.port=8084
# 数据库配置
spring.datasource.url=jdbc:mysql://localhost:3306/denture_model?useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&allowMultiQueries=true&sessionVariables=sql_mode='NO_ENGINE_SUBSTITUTION'
spring.datasource.username=root
spring.datasource.password=123456
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
# 当前使用环境
spring.profiles.active=dev
# mybatis的 Mapping.xml的位置
mybatis.mapper-locations=classpath:mapper/*.xml
# 指定POJO扫描包来让mybatis自动扫描到自定义的POJO,如果配置了该配置则不需要再ResultMap中指定全类名了
mybatis.type-aliases-package=com.ywy.entity