blog2.0--Springboot+MyBatis+maven+swagger框架搭建 IDEA上

 

然后进行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.version2</groupId>
	<artifactId>blog</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<packaging>jar</packaging>

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

	<!-- spring boot parent节点,引入之后,再去引入spring boot相关的就不需要引入版本了 -->
	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>2.0.2.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>
		<!-- web支持: 1.web mvc 2.restful 3.jackjson支持 4.aop 等等-->
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-data-jpa</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-test</artifactId>
			<scope>test</scope>
		</dependency>

		<!--热部署-->
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-devtools</artifactId>
			<optional>true</optional>
		</dependency>



		<!-- spring-boot整合mybatis -->
		<dependency>
			<groupId>org.mybatis.spring.boot</groupId>
			<artifactId>mybatis-spring-boot-starter</artifactId>
			<version>1.3.2</version>
		</dependency>

		<!-- mysql驱动 -->
		<dependency>
			<groupId>mysql</groupId>
			<artifactId>mysql-connector-java</artifactId>
			<scope>runtime</scope>
		</dependency>


		<!--基本配置-->
		<dependency>
			<groupId>junit</groupId>
			<artifactId>junit</artifactId>
			<version>4.9</version>
			<scope>test</scope>
		</dependency>
		<dependency>
			<groupId>javax.servlet</groupId>
			<artifactId>servlet-api</artifactId>
			<version>2.5</version>
			<scope>provided</scope>
		</dependency>
		<dependency>
			<groupId>javax.servlet</groupId>
			<artifactId>jsp-api</artifactId>
			<version>2.0</version>
			<scope>provided</scope>
		</dependency>
	</dependencies>


	<build>
		<plugins>
			<!-- 打包项目为可执行的jar包 -->
			<plugin>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin</artifactId>
			</plugin>
		</plugins>
	</build>


</project>

  

然后创建如下结构

进行数据库连接,在文件application.properties进行配置

spring.datasource.url=jdbc:mysql://118.24.116.158:3306/blog_V2?useUnicode=true&characterEncoding=UTF-8
spring.datasource.username=root
spring.datasource.password=skyeBlog7!
spring.datasource.driverClassName=com.mysql.jdbc.Driver
spring.datasource.max-active=20
spring.datasource.max-idle=8
spring.datasource.min-idle=8
spring.datasource.initial-size=10

  

补充BlogApplication.java

 

然后就是实体类的创建

mapper接口的创建

Service层

Controller层

 

 java.lang.NoSuchMethodError: javax.servlet.ServletContext.getVirtualServerName()Ljava/lang/String;

 

添加Swgger

添加swagger依赖

 <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger2</artifactId>
            <version>2.7.0</version>
        </dependency>
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger-ui</artifactId>
            <version>2.7.0</version>
        </dependency>

  

在APP同包下添加swagger2的配置类

package com.version2.blog;

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

    @Bean
    public Docket webAPI(){
        return new Docket(DocumentationType.SWAGGER_2)
                .groupName("API 接口文档")
                .apiInfo(apiInfo())
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.version2.blog.controller"))
                .paths(PathSelectors.any())
                .build();
    }


    private ApiInfo apiInfo(){
        return new ApiInfoBuilder()
                .title("测试Swagger")
                .termsOfServiceUrl("http://localhost/")
                .contact("skye")
                .version("1.0")
                .description("部署信息")
                .build();
    }
}

  

然后在需要生成API的接口添加注解

package com.version2.blog.controller;

import com.version2.blog.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * Created by Skye on 2018/5/28.
 */
@Api(tags = "test")
@RestController
public class UserController {

    @Autowired
    UserService userService;

    /*@ApiOperation(value = “接口说明”, httpMethod = “接口请求方式”,
    response = “接口返回参数类型”, notes = “接口发布说明”;其他参数可参考源码;*/
    @ApiOperation(value = "查询用户是否存在", notes = "")
    @RequestMapping("/userName")
    public String findUser(String userName){
        return "Hello";
       /* return userService.findUser( userName );*/
    }
}

  

然后访问http://localhost:8080/swagger-ui.html#/就可以了

 

posted @ 2018-05-28 23:15  SkyeAngel  阅读(973)  评论(0编辑  收藏  举报