网创优客建站品牌官网
为成都网站建设公司企业提供高品质网站建设
热线:028-86922220
成都专业网站建设公司

定制建站费用3500元

符合中小企业对网站设计、功能常规化式的企业展示型网站建设

成都品牌网站建设

品牌网站建设费用6000元

本套餐主要针对企业品牌型网站、中高端设计、前端互动体验...

成都商城网站建设

商城网站建设费用8000元

商城网站建设因基本功能的需求不同费用上面也有很大的差别...

成都微信网站建设

手机微信网站建站3000元

手机微信网站开发、微信官网、微信商城网站...

建站知识

当前位置:首页 > 建站知识

springBoot+MybatisPlus项目-创新互联

springBoot+MybatisPlus项目 一、springBoot项目 1.1 使用maven项目

pom.xml文件中加入依赖

成都创新互联从2013年成立,是专业互联网技术服务公司,拥有项目成都做网站、网站制作、成都外贸网站建设网站策划,项目实施与项目整合能力。我们以让每一个梦想脱颖而出为使命,1280元镇巴做网站,已为上家服务,为镇巴各地企业和个人服务,联系电话:18982081108
org.springframework.bootspring-boot-starter-parent2.6.4org.springframework.bootspring-boot-starter-web

搜索依赖网址为:https://mvnrepository.com/
创建controller

@RestController     // 包含Controller ResponseBody
public class indexController {@GetMapping("/index")
   public Object index(){   return "hello spring boot";
   }
}

创建启动类

@SpringBootApplication
public class AppServer {public static void main(String[] args) {SpringApplication.run(AppServer.class,args);
    }
}
1.2 使用spring initializr创建

选择URL为https://start.spring.io创建项目
在这里插入图片描述
创建maven项目
在这里插入图片描述

1.3 使用spring initializr阿里云

Custom选择阿里云镜像https://start.aliyun.com/创建项目

1.4 本章小结

所有的springboot项目要继承parent,可以避免中间件版本冲突,阿里云生成的spring项目在spring-boot-dependencies规定了第三方依赖的版本号

二、主启动类

主启动类@SpringBootApplication注解,类似于对该层文件做了xml注释,能够扫描在该层文件夹下的文件

主启动类位置为:
在这里插入图片描述

三、配置文件

配置文件需要在resources.application文件进行配置
推荐使用application.yml文件进行配置,在使用yml时要注意空格

四、以jar的形式运行程序 4.1打包

在打包前需要在pom.xml文件中添加以下依赖:

org.springframework.bootspring-boot-maven-plugin

然后可以使用 mvn package 命令进行打包
在idea运行package
在这里插入图片描述

4.2 运行jar

使用java -jar xxx.jar命令运行jar

五、springboot整合swagger3 5.1 为什么使用swagger3

1.前后端分离开发,后端程序员开发接口,实时更新接口操作数据,前端取后端提供出来的接口数据,app/web

5.2 整合步骤

步骤1:添加springfox-swaage依赖

io.springfoxspringfox-boot-starter3.0.0

步骤2:编写swagger的配置类

package com.bobo.project_spring.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;

//配置文件
//@Configuration
public class SwaggerConfig {//写法固定
    @Bean
    public Docket apiConfig(){return new Docket(DocumentationType.OAS_30)
                .apiInfo(apiInfo())
                .select()
                .apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class))
                //设置通过什么方式定位生成文档的接口
                //这里定位了方法上的apiOperaion
                .paths(PathSelectors.any())
                //any表示全部的路径
                //regex() 正则匹配
                .build();
    }

    private ApiInfo apiInfo(){return new ApiInfoBuilder()
                .title("项目")
                .description("项目信息")
                .contact(new Contact("1","www.baidu.com","911*****@qq.com"))
                .version("1.0")
                .build();
    }
}

在启动时报错: documentationPluginsBootstrapper
使用swagger3需要在application.yml中配置,swagger则不需要该配置

spring:
    mvc:
      pathmatch: 
        matching-strategy: ant_path_matcher

步骤3:添加相关注解
@EnableOpenApi //识别swagger,允许swagger执行
该注解添加在启动类,表示允许swagger运行
@Api(tags = {“学生管理”}) //api在类上说明 tags说明类的作用
该注解添加在控制器类
@ApiOperation(value = “显示全部学生数据”) //表明当前方法
该注解添加在方法上
步骤4:打开swagger,http://localhost:8080/swagger-ui/index.html
注:通过@ApiImplicitParams和@ApiImplicitParam注解可以对参数进行描述

@GetMapping("/{id}")   //根据get或者delectmap可以识别相同的路径
    @ApiOperation(value = "id")
    @ApiImplicitParams(
            @ApiImplicitParam(
                    name = "id",value = "学生id",required = true,dataType = "Integer",dataTypeClass = Integer.class
            )
    )
    public Object selectById(@PathVariable int id){return new Student(id,"张三","男",14);
    }

还可以在方法参数中加入@Apiparas()注解,来说明参数
如果使用post方式提交请求,可以在模型层添加@ApiModle来标注对象中的属性

import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

@Data
@AllArgsConstructor
@NoArgsConstructor
@ApiModel(value = "学生对象",description = "对应数据库student")
public class Student {@ApiModelProperty(value = "学生id",required = true,example = "1")
    private int id;
    @ApiModelProperty(value = "学生名字",required = true,example = "张三")
    private String name;
    @ApiModelProperty(value = "学生性别",required = true,example = "男")
    private String genbder;
    @ApiModelProperty(value = "学生年龄",required = true,example = "12")
    private int age;

  public Student(int id, String name, String genbder, int age) {this.id = id;
        this.name = name;
        this.genbder = genbder;
        this.age = age;
    }
}

知识点:
restful
/student 查询 get
/student 新增 put
/student 更改 save
/student 查询 delect

你是否还在寻找稳定的海外服务器提供商?创新互联www.cdcxhl.cn海外机房具备T级流量清洗系统配攻击溯源,准确流量调度确保服务器高可用性,企业级服务器适合批量采购,新人活动首月15元起,快前往官网查看详情吧


当前题目:springBoot+MybatisPlus项目-创新互联
文章链接:http://bjjierui.cn/article/cdcoci.html

其他资讯