搭建springboot+mybatis+mysql+swagger

创建springboot项目

  1. 配置maven
    搭建springboot+mybatis+mysql+swagger_第1张图片

  2. 创建springboot项目
    搭建springboot+mybatis+mysql+swagger_第2张图片
    搭建springboot+mybatis+mysql+swagger_第3张图片
    搭建springboot+mybatis+mysql+swagger_第4张图片
    一直next就可以穿件完成了

  3. 创建包
    搭建springboot+mybatis+mysql+swagger_第5张图片

  4. 配置application.yml

spring:
  datasource:
    url: jdbc:mysql://127.0.0.1:3306/ssm?useUnicode=true&characterEncoding=utf8&serverTimezone=GMT%2B8
    username: root
    password: root
    driver-class-name: com.mysql.cj.jdbc.Driver
mybatis:
  #配置扫描xml文件
  mapper-locations: classpath*:mapper/*Mapper.xml
  type-aliases-package: com.myself.mybatis.entity
  #配置打印sql
  configuration:
    log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
  1. 引入swagger相关的jar包依赖
	<!-- Swagger API文档 -->
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger-ui</artifactId>
            <version>2.9.2</version>
        </dependency>
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger2</artifactId>
            <version>2.9.2</version>
        </dependency>
  1. 创建config包,里面放的是swagger接口测试框架用到的方法
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
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 api() {
        return new Docket(DocumentationType.SWAGGER_2)
                .select()
                .apis(RequestHandlerSelectors.any())
                .paths(PathSelectors.any())
                .build();
    }
}

搭建springboot+mybatis+mysql+swagger_第6张图片

7.配置yml文件

#开启swagger接口测试
swagger:
 show: true

8.创建测试用的controller


import com.it.springboot.model.Student;
import com.it.springboot.service.StudentService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;
import java.util.UUID;
import java.util.function.Function;

/**
 * springboot+mybatis+mysql
 * 集成swagger接口测试框架
 * 使用java8,lamdba表达式,函数式接口,方法引用
 */
@Controller
@RequestMapping(value = "/studentController")
@Api(tags = "studentController", description = "学生操作api接口测试")
public class StudentConroller {

    @Autowired
    private StudentService studentService;

    /**
     * 查询学生全部信息
     * 根据姓名查询学生信息
     * @param name
     * @return
     */
    @ResponseBody
    @PostMapping("/index")
    @ApiOperation(value = "根据姓名查询学生信息 - 模糊查询", notes = "查询学生姓名", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "name", dataType = "String", required = true, value = "学生姓名", paramType = "query")
    })
    public List<Student> index(String name){
        Function<String, List<Student>> fun = (msg) -> {
          return studentService.selectStudentAll(msg);
        };
        List<Student> list = fun.apply(name);
        return list;
    }
  }

搭建springboot+mybatis+mysql+swagger_第7张图片
运行测试即可(localhost:8080/swagger-ui.html)
最后配上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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.3.0.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.it</groupId>
    <artifactId>springboot</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>springboot</name>
    <description>Demo project for Spring Boot</description>
    <!--jdk版本-->
    <properties>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
    	<!--web项目-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <!--集成mybatis-->
        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>2.1.2</version>
        </dependency>
        <!--集成mysql-->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <scope>runtime</scope>
        </dependency>
        <!--集成Lombok插件-->
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        <!--单元测试Junit Test-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
            <exclusions>
                <exclusion>
                    <groupId>org.junit.vintage</groupId>
                    <artifactId>junit-vintage-engine</artifactId>
                </exclusion>
            </exclusions>
        </dependency>

        <!-- Swagger API文档 -->
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger-ui</artifactId>
            <version>2.9.2</version>
        </dependency>
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger2</artifactId>
            <version>2.9.2</version>
        </dependency>

    </dependencies>

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

</project>

你可能感兴趣的:(mybatis,mysql,maven,spring,boot)