在现代的 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);
}
}
五、测试
对各个功能进行测试
增加数据
查询所有数据
查询单个数据
修改数据
删除数据(删除是只删除在线文档上的,实际的数据库中是没有被删除的)