swagger-api实现简单的数据增删改查

时间:2024-03-23 18:14:44

(1)、首先创建springBoot工程

pom.xml代码:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
   <modelVersion>4.0.0</modelVersion>

   <groupId>com.example</groupId>
   <artifactId>demo</artifactId>
   <version>0.0.1-SNAPSHOT</version>
   <packaging>jar</packaging>

   <name>demo</name>
   <description>Demo project for Spring Boot</description>

   <parent>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-parent</artifactId>
      <version>1.5.10.RELEASE</version>
      <relativePath/> <!-- lookup parent from repository -->
   </parent>

   <properties>
      <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
      <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
      <java.version>1.8</java.version>
   </properties>

   <dependencies>
      <dependency>
         <groupId>junit</groupId>
         <artifactId>junit</artifactId>
         <version>4.12</version>
         <scope>test</scope>
      </dependency>

      <dependency>
         <groupId>org.slf4j</groupId>
         <artifactId>slf4j-api</artifactId>
         <version>1.7.5</version>
      </dependency>

      <dependency>
         <groupId>org.slf4j</groupId>
         <artifactId>slf4j-log4j12</artifactId>
         <version>1.7.12</version>
      </dependency>

      <dependency>
         <groupId>org.springframework.boot</groupId>
         <artifactId>spring-boot-starter</artifactId>
         <exclusions>
            <exclusion>
               <groupId>org.springframework.boot</groupId>
               <artifactId>spring-boot-starter-logging</artifactId>
            </exclusion>
         </exclusions>
      </dependency>

      <dependency>
         <groupId>org.springframework.boot</groupId>
         <artifactId>spring-boot-starter-log4j</artifactId>
      </dependency>

      <dependency>
         <groupId>org.springframework.boot</groupId>
         <artifactId>spring-boot-starter-data-jpa</artifactId>
      </dependency>

      <dependency>
         <groupId>mysql</groupId>
         <artifactId>mysql-connector-java</artifactId>
      </dependency>

      <dependency>
         <groupId>org.springframework.boot</groupId>
         <artifactId>spring-boot-starter-web</artifactId>
      </dependency>

      <dependency>
         <groupId>org.springframework.boot</groupId>
         <artifactId>spring-boot-starter-test</artifactId>
         <scope>test</scope>
      </dependency>

      <dependency>
         <groupId>org.apache.tomcat.embed</groupId>
         <artifactId>tomcat-embed-jasper</artifactId>
         <scope>provided</scope>
      </dependency>

      <dependency>
         <groupId>javax.servlet</groupId>
         <artifactId>jstl</artifactId>
         <version>1.2</version>
      </dependency>

      <dependency>
         <groupId>org.apache.maven.plugins</groupId>
         <artifactId>maven-war-plugin</artifactId>
         <version>3.2.0</version>
      </dependency>

      <dependency>
         <groupId>org.springframework.boot</groupId>
         <artifactId>spring-boot-starter-thymeleaf</artifactId>
      </dependency>

      <dependency>
         <groupId>org.mybatis.spring.boot</groupId>
         <artifactId>mybatis-spring-boot-starter</artifactId>
         <version>1.3.1</version>
      </dependency>

      <dependency>
         <groupId>com.alibaba</groupId>
         <artifactId>druid</artifactId>
         <version>1.0.26</version>
      </dependency>

      <!--输出APi-->
      <dependency>
         <groupId>io.springfox</groupId>
         <artifactId>springfox-swagger2</artifactId>
         <version>2.5.0</version>
      </dependency>
      <dependency>
         <groupId>io.springfox</groupId>
         <artifactId>springfox-swagger-ui</artifactId>
         <version>2.5.0</version>
      </dependency>

   </dependencies>

   <build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
        </plugin>
    </plugins>
</build>
</project>

(2)、再创建实体类

package com.example.demo.entity;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;

@Component
@Entity
@Table(name="person")
public class Person {
    @Id
    @GeneratedValue
    private Integer id;
    private String name;
    private Integer age;

    //必须要有构造函数
    public Person() {
    }

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }

    @Override
    public String toString() {
        return "Person [id=" + id + ", name=" + name + ", age=" + age +"]" ;
    }
}

(3)、整合springBoot和mybatis

package com.example.demo.mapper;

import com.example.demo.entity.Person;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface PersonMapper {
   //查询所有信息
    @Select("select * from person")
    public List<Person> getList();

    //添加一条记录
    @Insert("insert into person(id,name,age) values (#{id},#{name},#{age})")
    public void addPerson(Person person);

    //查询单条记录
    @Select("select * from person where id=#{id}")
    public Person getPersonOne(Integer id);

    //修改一条记录
    @Update("update person set name=#{name},age=#{age} where id=#{id}")
    public void updatePerson(Person person);
    //删除一条记录
    @Delete("delete from person where id=#{id}")
    public void deletePersonById(Integer id);
}

(4)、controller类:

package com.example.demo.controller;

import com.example.demo.entity.Person;
import com.example.demo.service.PersonService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
@Api(value = "简单test",description = "简单的测试API")
@RestController
public class PersonController {

    @Autowired
    private PersonService personService;

    //personList
    @ApiOperation("查询全部记录")
    @RequestMapping(value = "/personList",method = RequestMethod.GET)
    public List<Person> getList(){
        List<Person> list = this.personService.getList();
        return list;
    }
    //添加一条数据
    @ApiOperation("添加一条记录")
    @RequestMapping(value = "/addPerson",method = RequestMethod.POST)
    public String addPerson(@ModelAttribute Person person){
        Person p = new Person();
        p.setId(person.getId());
        p.setName(person.getName());
        p.setAge(person.getAge());
        this.personService.addPerson(p);
        return "success";
    }
    //查询单个记录
    @ApiOperation("根据ID查询")
    @RequestMapping(value = "/getPersonOne/{id}",method = RequestMethod.GET)
    public Person getPersonOne(@ApiParam("ID")@PathVariable Integer id){
        Person person = this.personService.getPersonOne(id);
        return person;
    }
    //修改单条记录
    @ApiOperation("根据ID修改记录")
    @RequestMapping(value = "/updatePerson/{id}",method = RequestMethod.PUT)
    public String updatePerson(@ApiParam("被修改的ID")@PathVariable Integer id,@RequestBody Person person){
        Person p = new Person();
        p.setId(id);
        p.setName(person.getName());
        p.setAge(person.getAge());
        this.personService.updatePerson(p);
        return "success";
    }

    //删除一条数据
    @ApiOperation("根据ID删除记录")
    @RequestMapping(value = "/deletePerson/{id}",method = RequestMethod.DELETE)
    public String deletePersonById(@ApiParam("被删除的ID")@PathVariable Integer id){
        this.personService.deletePersonById(id);
        return "success";
    }

}

(5)、swagger类:

package com.example.demo.utils;

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 SwaggerApi {

    @Bean
    public Docket buildDocket(){
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(buildApi())
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.example.demo.controller"))
                .paths(PathSelectors.any())
                .build();
    }
    private ApiInfo buildApi(){
        return new ApiInfoBuilder()
                .title("springBoot test Swagger build document API")
                .contact("sanpiao")
                .version("1.0").build();
    }

}

(6)、运行http://localhost:8080/swagger-ui.html

结果:swagger-api实现简单的数据增删改查