spring boot集成 spring + spring MVC + mybatis + swagger + maven

spring boot集成 spring + spring MVC + mybatis + swagger + maven

第一步:选择Spring Initializr搭建spring boot
spring boot集成 spring + spring MVC + mybatis + swagger + maven_第1张图片
第二步:配置项目信息
spring boot集成 spring + spring MVC + mybatis + swagger + maven_第2张图片
第三步:选择项目需要的jar包
web必选的
spring boot集成 spring + spring MVC + mybatis + swagger + maven_第3张图片
我用的Mysql数据库选择MySQL,Mybatis
spring boot集成 spring + spring MVC + mybatis + swagger + maven_第4张图片
第四步:配置项目名和项目地址

spring boot集成 spring + spring MVC + mybatis + swagger + maven_第5张图片
第五步:spring boot完整项目结构
spring boot集成 spring + spring MVC + mybatis + swagger + maven_第6张图片

项目实例结构

spring boot集成 spring + spring MVC + mybatis + swagger + maven_第7张图片

第一步:创建User实体类

package com.lbl.springbootdemo.domain;

/**
 * @Author: libl
 * @Date: 2018/9/4 16:37
 * @Description: User实体类
 */
public class User {
     

    private int id;
    private String name;
    private int age;

    public int getId() {
        return id;
    }

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

    public String getName() {
        return name;
    }

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

    public int getAge() {
        return age;
    }

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

    @Override
    public String toString() {
        return "User{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", age=" + age +
                '}';
    }
}

第二步:创建UserDao接口


package com.lbl.springbootdemo.dao;

import com.lbl.springbootdemo.domain.User;
import org.apache.ibatis.annotations.Mapper;
import org.springframework.stereotype.Repository;

/**
 * @Author: libl
 * @Date: 2018/9/4 16:49
 * @Description:
 */
@Mapper
@Repository
public interface UserDao {
     

    public User findUserById(int id);
}

第三步:创建UserMapper.xml



<mapper namespace="com.lbl.springbootdemo.dao.UserDao">

    <select id="findUserById" resultType="com.lbl.springbootdemo.domain.User">
        SELECT * FROM user WHERE id = #{id}
    select>

mapper>

第四步:创建UserService接口

package com.lbl.springbootdemo.service;

import com.lbl.springbootdemo.domain.User;

/**
 * @Author: libl
 * @Date: 2018/9/4 16:49
 * @Description:
 */
public interface UserService {
     
    public User findUserById(int id);
}

第五步:创建UserService类实现UserService接口

package com.lbl.springbootdemo.service.impl;

import com.lbl.springbootdemo.dao.UserDao;
import com.lbl.springbootdemo.domain.User;
import com.lbl.springbootdemo.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @Author: libl
 * @Date: 2018/9/4 16:50
 * @Description:
 */
@Service
public class UserServiceImpl implements UserService {
     

    @Autowired
    UserDao userDao;

    @Override
    public User findUserById(int id) {
        return userDao.findUserById(id);
    }
}

第六步:创建Swagger2类
需要先在pom.xml加入2个Swagger依赖

    
        <dependency>
            <groupId>io.springfoxgroupId>
            <artifactId>springfox-swagger2artifactId>
            <version>2.6.1version>
        dependency>
        <dependency>
            <groupId>io.springfoxgroupId>
            <artifactId>springfox-swagger-uiartifactId>
            <version>2.6.1version>
        dependency>
        
package com.lbl.springbootdemo.config;

import io.swagger.annotations.ApiOperation;
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.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

/**
 * @Author: libl
 * @Date: 2018/9/4 16:51
 * @Description: Restful API 访问路径:http://IP:port/{context-path}/swagger-ui.html
 */

@Configuration //用@Configuration注解该类,等价于XML中配置beans;用@Bean标注方法等价于XML中配置bean。
@EnableSwagger2  //@EnableSwagger2注解来启用Swagger2。
public class Swagger2 {
     

    /**
     * swagger2的配置文件,这里可以配置swagger2的一些基本的内容,比如扫描的包等等
     *
     * @return
     */
    @Bean
    protected Docket createRestApi() {
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .select()
                .apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class)) //相当于apis(RequestHandlerSelectors.basePackage("com.lbl.springbootdemo.controller"))
                .paths(PathSelectors.any())
                .build();
    }

    /**
     * 构建 api文档的详细信息函数,这些函数不是必须的,但我都列了出来。
     *
     * @return
     */
    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                //页面标题
                .title("title页面标题:用户管理客户端")
                //描述
                .description("description描述:用户的增删改查")
                //版本号
                .version("version版本号:1.0")
                // 许可人url
                .termsOfServiceUrl("termsOfServiceUrl许可人url:https://blog.csdn.net/weixin_39819880")
                //创建人姓名,url,email
                .contact(new Contact("创建人姓名:libl", "url:https://blog.csdn.net/weixin_39819880", "email:[email protected]"))
                // 许可人,许可证
                .license("license许可人,许可证:libl")
                // 许可url
                .licenseUrl("licenseUrl许可url:https://blog.csdn.net/weixin_39819880")
                .build();
    }

}

第七步:创建UserController类

package com.lbl.springbootdemo.controller;

import com.lbl.springbootdemo.domain.User;
import com.lbl.springbootdemo.service.impl.UserServiceImpl;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

/**
 * @Author: libl
 * @Date: 2018/9/4 16:50
 * @Description:
 */
@RestController//相当于@Controller与@ResponseBody
@Api(value = "user", description = "用户管理", tags = "用户管理")
@RequestMapping("/user")
public class UserController {
     

    @Autowired
    UserServiceImpl userService;

    @ApiOperation(value = "查询用户信息")
    @ApiImplicitParam(name = "id", value = "用户id", required = true, dataType = "Integer", paramType = "query")
    @RequestMapping(value = "/find", method = RequestMethod.POST)
    public User findUserById(int id) {
        User user = userService.findUserById(id);
        return user;
    }
}

第七步:配置application.properties

#项目的根路径
server.servlet.context-path=/springboot-demo
#设置UTF-8编码
spring.http.encoding.enabled=true
spring.http.encoding.charset=UTF-8
server.tomcat.uri-encoding=UTF-8
#tomcat端口
server.port=8022
#数据连接
spring.datasource.driverClassName=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/user?useUnicode=true&characterEncoding=utf8&useSSL=false
spring.datasource.username=root
spring.datasource.password=root
#Mybatis扫描
mybatis.mapper-locations=classpath*:mapper/*.xml

pom.xml文件


<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.0modelVersion>

    <groupId>com.lblgroupId>
    <artifactId>springboot-demoartifactId>
    <version>0.0.1-SNAPSHOTversion>
    <packaging>jarpackaging>

    <name>springboot-demoname>
    <description>Demo project for Spring Bootdescription>

    <parent>
        <groupId>org.springframework.bootgroupId>
        <artifactId>spring-boot-starter-parentartifactId>
        <version>2.0.4.RELEASEversion>
        <relativePath/> 
    parent>

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

    <dependencies>
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-webartifactId>
        dependency>
        <dependency>
            <groupId>org.mybatis.spring.bootgroupId>
            <artifactId>mybatis-spring-boot-starterartifactId>
            <version>1.3.2version>
        dependency>

        <dependency>
            <groupId>mysqlgroupId>
            <artifactId>mysql-connector-javaartifactId>
            <scope>runtimescope>
        dependency>
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-testartifactId>
            <scope>testscope>
        dependency>

        
        <dependency>
            <groupId>io.springfoxgroupId>
            <artifactId>springfox-swagger2artifactId>
            <version>2.6.1version>
        dependency>

        <dependency>
            <groupId>io.springfoxgroupId>
            <artifactId>springfox-swagger-uiartifactId>
            <version>2.6.1version>
        dependency>
        
    dependencies>

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

project>

数据库
spring boot集成 spring + spring MVC + mybatis + swagger + maven_第8张图片
访问路径:http://IP:port/{context-path}/swagger-ui.html
访问http://127.0.0.1:8022/springboot-demo/swagger-ui.html

spring boot集成 spring + spring MVC + mybatis + swagger + maven_第9张图片
先点击 Show/Hide 再点击 Expand Operations

spring boot集成 spring + spring MVC + mybatis + swagger + maven_第10张图片
输入参数id 点击Try it out!

spring boot集成 spring + spring MVC + mybatis + swagger + maven_第11张图片

Swagger笔记

-----------------------------------------------------------------------------------------------------------------------------------
Swagger注解说明:
@Api:用在类上,说明该类的作用
@ApiOperation:用在方法上,说明方法的作用
@ApiImplicitParams:用在方法上包含一组参数说明
@ApiImplicitParam:用在@ApiImplicitParams注解中,指定一个请求参数的各个方面
paramType:参数放在哪个地方
header-->请求参数的获取:@RequestHeader
query-->请求参数的获取:@RequestParam
path(用于restful接口)-->请求参数的获取:@PathVariable
body(不常用)
form(不常用)
name:参数名
dataType:参数类型
required:参数是否必须传
value:参数的意思
defaultValue:参数的默认值
@ApiResponses:用于表示一组响应
@ApiResponse:用在@ApiResponses中,一般用于表达一个错误的响应信息
code:数字,例如400
message:信息,例如"请求参数没填好"
response:抛出异常的类
@ApiModel:描述一个Model的信息(这种一般用在post创建的时候,使用@RequestBody这样的场景,请求参数无法使用@ApiImplicitParam注解进行描述的时候)
@ApiModelProperty:描述一个model的属性
-----------------------------------------------------------------------------------------------------------------------------------

你可能感兴趣的:(java项目环境搭建)