昨天使用了springboot整合springdatajpa写了一套简单的增删改查功能,今天想实现前后端分离来写一套简单的增删改查demo,于是就有了今天这篇文章。
后端开发还是用的springboot整合springdatajpa:
但是为了使用swagger,我们需要加一点东西,先是在pom文件中加入swagger的依赖。
依赖我是网上找的,如果大家有不同版本,也不必跟我的用同一版。依赖如下:
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.2.2</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.2.2</version>
</dependency>
然后我们在启动类上加入注解:
@EnableSwagger2
然后需要配置一个叫config的文件夹、是用来配置swagger的config文件的。配置一个叫SwaggerConfig的类,类的代码如下:
package com.zerol.jap_vue.config;
import org.springframework.context.annotation.Bean;
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.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;
@Configuration
@EnableSwagger2
public class SwaggerConfig {
@Bean
public Docket createRestApi() {
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo())
.select()
//选择controller包
//这里我们需要改的就是自己控制层所在的位置,其他东西不想改可以不改
.apis(RequestHandlerSelectors.basePackage("com.zerol.jap_vue.controller"))
.paths(PathSelectors.any())
.build();
}
private ApiInfo apiInfo() {
return new ApiInfoBuilder()
//自定义信息可按需求填写
.title("Spring Data Jpa中使用Swagger2构建前后端分离开发")
.description("demo")
//这里大家可以放自己的博客名
.termsOfServiceUrl("https://blog.csdn.net/ZEROl_")
//这个是作者名
.contact("ZEROl")
.version("1.0")
.build();
}
}
然后可以写增删改查了,由于昨天已经发过一篇jpa的博客了,所以今天就不列出关于jpa的代码了
然后我们启动项目,成功启动后进入到http://localhost:8080/swagger-ui.html#/ 访问这个地址即可
使用swagger来操作我们的的后端的 jpa demo了
这些方法点进入就可以执行相应的功能,后端就暂时告一段落。
本人比较懒 就只建立一个简单的表格来进行增删改查。前段代码如下:
<!DOCTYPE html>
<html>
<