Mybatis-flex的增、删、改,查,使用swagger (knife4J)

在现代的 Java 开发中,Mybatis - Flex 为数据库操作提供了高效灵活的方式,而 Swagger(Knife4J)则极大地改善了 API 的文档化和测试体验。将两者结合起来,对于开发人员来说是提高效率、增强协作以及保证代码质量的有效手段。本文将详细介绍 Mybatis - Flex 在增删改方面的操作,并阐述如何与 Swagger(Knife4J)协同工作。
针对Mybatis - Flex 的增、删、改、查,前期准备可参考我的第一篇博客,且沿用了前期的配置环境。

一、项目目录结构

新增的包:config
新增类和接口:Knife4jConfig、BuildingController、Building、BuildingMapper、BuildingServiceImpl、BuildingService
在这里插入图片描述

二、创建数据库

CREATE TABLE `tb_building`  (
  `b_id` int(0) NOT NULL AUTO_INCREMENT,
  `b_name` varchar(100) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL,
  `location` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL,
  `construction_year` datetime(0) NULL DEFAULT NULL,
  `building_area` double NULL DEFAULT NULL,
  `building_type` varchar(50) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL,
  `create_time` datetime(0) NULL DEFAULT NULL,
  `update_time` datetime(0) NULL DEFAULT NULL,
  `is_deleted` int(0) NULL DEFAULT 1,
  PRIMARY KEY (`b_id`) USING BTREE
) ENGINE = InnoDB AUTO_INCREMENT = 1 CHARACTER SET = utf8mb4 COLLATE = utf8mb4_general_ci ROW_FORMAT = Dynamic;

三、引入Knife4J依赖包并配置

(1)添加在pom.xml中

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

(2)配置Knife4jConfig.java类
在这里插入图片描述
具体代码:

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("xxx", "地址", "邮箱");
        return new ApiInfo(
            "闽南古厝建筑文档",
            "闽南古厝建筑文档",
            "1.0",
            "",
            author,
            "",
            "",
            new ArrayList()
        );
    }
}

(3)配置yml文件
在这里插入图片描述
具体代码:

  mvc:
    pathmatch:
      matching-strategy: ant_path_matcher

运行测试: http://localhost:8080/doc.html#/home

四、对其进行增、删、改、查

创建所需要的映射文件
(1)创建实体类Building.java
在这里插入图片描述
具体代码:

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)//判断是否是大字段,大字段APT不会生成到 DEFAULT_COLUMNS 里
    private String location;
    @Column(onInsertValue = "now()")//insert 的时候默认值,这个值会直接被拼接到 sql 而不通过参数设置
    private Date construction_year;
    private Double building_area;
    private String building_type;
    @Column(onInsertValue = "now()")
    private Date create_time;
    @Column(onInsertValue = "now()",onUpdateValue = "now()")//update 的时候自动赋值,这个值会直接被拼接到 sql 而不通过参数设置
    private Date update_time;
    @Column(isLogicDelete = true)
    private Boolean isDeleted;
}

(2)创建Service层的接口BuildingService
在这里插入图片描述
具体代码:

package com.mybatisflex.demo.service;

import com.mybatisflex.core.service.IService;
import com.mybatisflex.demo.entity.Building;

public interface BuildingService extends IService<Building> {
}

(3)创建Service的具体类BuildingServiceImpl.java
在这里插入图片描述
具体代码:

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)创建Mapper层上的接口BuildingMapper
在这里插入图片描述
具体代码:

package com.mybatisflex.demo.mapper;

import com.mybatisflex.core.BaseMapper;
import com.mybatisflex.demo.entity.Building;
public interface BuildingMapper extends BaseMapper<Building> {
}

(5)创建controller层上的类BuildingController.java
在这里插入图片描述
具体代码:

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);
    }
}

五、测试

对各个功能进行测试
增加数据
在这里插入图片描述
在这里插入图片描述
查询所以数据
在这里插入图片描述
查询单个数据
在这里插入图片描述

删除数据
在这里插入图片描述

本项目是一个基于SSM(Spring+SpringMVC+MyBatis)框架和Vue.js前端技术的家教平台系统。该系统旨在为家教和学生提供一个便捷、高效的在线交流和预约平台,涵盖了从用户注册登录、个人信息管理、课程发布与搜索、预约与取消预约、评价反馈等一系列功能。 在后台管理方面,系统提供了管理员对用户信息、课程信息、预约记录等进行管理的功能,确保平台的正常运行和数据的准确性。通过Spring框架的依赖注入和AOP特性,实现了业务逻辑的清晰分离和高效处理;SpringMVC则负责处理前端请求和响应,提供友好的用户界面;MyBatis作为ORM框架,简化了数据库操作,提高了数据访问的效率和安全性。 前端部分采用Vue.js框架,结合Vue Router进行页面路由管理,Axios进行HTTP请求,实现了前后端分离的开发模式。Vue.js的组件化开发和响应式数据绑定特性,使得前端页面更加动态和交互性强,提升了用户体验。 数据库设计采用了MySQL,存储了用户信息、课程信息、预约记录等核心数据。通过合理的数据库表结构和索引设计,保证了系统的高效运行和数据的一致性。 该项目不仅适合计算机相关专业的毕设学生参考和学习,也适合Java学习者进行项目实战练习。通过对该项目的深入理解和二次开发,可以实现更多个性化功能,进一步提升技术水平和实践能力。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值