Mybatis-flex的增、删、改,查,使用swagger 文档增强工具knife4j使用详解

使用原生的swagger作为接口文档,功能不够强大,并且默认的ui比较简陋,不符合大众审美。所以实际开发中推荐使用knife4j对swagger进行增强,本文将详细介绍 Mybatis - Flex 在增删改方面的操作,并阐述如何与使用swagger 文档增强工具knife4j进行协同工作。本文沿用之前的配置环境。

 一、创建数据库

二、导入Knife4J依赖包并配置 

1.添加到pom.xml中

<dependency>
    <groupId>com.github.xiaoymin</groupId>
    <artifactId>knife4j-spring-boot-starter</artifactId>
    <version>2.0.9</version>
</dependency>

2.创建Knife4jConfig类 

在config包创建Knife4jConfig类

package com.mybatisflex.demo.config;

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.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2WebMvc;

import java.util.ArrayList;

@Configuration
@EnableSwagger2WebMvc
public class Knife4jConfig {
    @Bean
    public Docket docket(){
        return  new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.mybatisflex.demo.controlle"))
                .paths(PathSelectors.any())
                .build();

    }

    private ApiInfo apiInfo() {
        Contact author = new Contact("xx", "地址", "邮箱");
        return new ApiInfo(
                "闽南厝文档",
                "闽南厝文档",
                "1.0",
                "",
                author,
                "",
                "",
                new ArrayList()
        );
    }

}

3.配置yml文件

在spring里添加代码

  mvc:
    pathmatch:
      matching-strategy: ant_path_matcher

 4.运行测试

http://localhost:8080/doc.html

 三、编写增删改查

1.创建实体类Building

package com.mybatisflex.demo.entity;
import com.mybatisflex.annotation.Column;
import com.mybatisflex.annotation.Id;
import com.mybatisflex.annotation.KeyType;
import com.mybatisflex.annotation.Table;
import lombok.Data;

import java.util.Date;

@Data
@Table("tb_building")//数据库表名
public class Building {
    @Id(keyType = KeyType.Auto)
    private Integer b_id;
    private String b_name;
    @Column(isLarge = true)
    private String location;
    @Column(onInsertValue = "now()")
    private Date construction_year;
    private Double building_area;
    private String building_type;
    @Column(onInsertValue = "now()")
    private Date create_time;
    @Column(onInsertValue = "now()",onUpdateValue = "now()")
    private Date update_time;
    @Column(isLogicDelete = true)
    private Boolean isDeleted;
}

2.创建接口BuildingService

3.创建具体类BuildingServiceImpl

package com.mybatisflex.demo.service.impl;

import com.mybatisflex.demo.entity.Building;
import com.mybatisflex.demo.mapper.BuildingMapper;
import com.mybatisflex.demo.service.BuildingService;
import com.mybatisflex.spring.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

@Service
public class BuildingServiceImpl extends ServiceImpl<BuildingMapper, Building> implements BuildingService {
}

 4.创建接口BuildingMapper

5.创建controller层上的类BuildingController

package com.mybatisflex.demo.controller;

import com.mybatisflex.demo.entity.Building;
import com.mybatisflex.demo.service.BuildingService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/api/building")
public class BuildingController {
    @Autowired
    private BuildingService buildingService;
    @PostMapping("/create")
    public Boolean create(Building building){
        return buildingService.save(building);
    }
    @PostMapping("/update")
    public Boolean update(Building building){
        return  buildingService.updateById(building);
    }
    @GetMapping("/delete")
    public Boolean delete(Integer b_id){
        return buildingService.removeById(b_id);
    }
    @GetMapping("/all")
    public List<Building> all(){
        return buildingService.list();
    }
    @GetMapping("/one")
    public Building one(Integer b_id){
        return buildingService.getById(b_id);
    }
}

四、测试

1.添加数据,点击发送显示true

2.查询所有数据

3.查询单个数据

 4.更新数据

5.删除数据 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值