现代 Java 开发中,Mybatis - Flex 是一个功能强大的 Java 持久层框架,使数据库操作高效灵活,而Swagger(Knife4J)则改善 了API 文档化与测试体验,两者结合能提高效率、增强协作、保证代码质量。本文将详细描述 Mybatis - Flex 增、删、改、查操作及与 Swagger(Knife4J)协同使用,以下均已Culture为例。
一、项目文件组织架构
(1).在com.mybatisflex.demo下新增config软件包
(2).新增Knife4jConfig、CultureController、Culture、CultureServiceImpl类以及CultureMapper、CultureService接口
二、构建数据库
CREATE TABLE `tb_culture` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(100) NOT NULL,
`description` longtext COMMENT '文化艺术描述',
`create_time` datetime DEFAULT NULL,
`update_time` datetime DEFAULT NULL,
`is_deleted` int(11) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8mb4;
三、引入依赖配置knife4J
(1).在pom.xml中引入Knife4j的依赖包,代码如下:
<dependency>
<groupId>com.github.xiaoymin</groupId>
<artifactId>knife4j-spring-boot-starter</artifactId>
<version>2.0.9</version>
</dependency>
(2).创建Swagger配置依赖,代码如下:
@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()
);
}
}
注意:
.apis(RequestHandlerSelectors.basePackage("com.mybatisflex.demo.controlle"))文件路径要与自己项目的路径名保持一致哦!
(3).加入application.yml配置文件
mvc:
pathmatch:
matching-strategy: ant_path_matcher
运行测试: http://localhost:8080/doc.html
四、增、删、改、查操作
(1)构建实体类Culture.java
@Data
@Table("tb_culture")
public class Culture {
@Id(keyType = KeyType.Auto)
private Integer id;
private String name;
@Column(isLarge = true)
private String description;
@Column(onInsertValue = "now()")
private Date createTime;
@Column(onInsertValue = "now()",onUpdateValue = "now()")
private Date updateTime;
@Column(isLogicDelete = true)
private Boolean isDeleted;
}
(2)构建Service层接口CultureService
public interface CultureService extends IService<Culture> {
}
(3)构建Service的具体类CultureServiceImpl.java
@Service
public class CultureServiceImpl extends ServiceImpl<CultureMapper, Culture> implements CultureService {
}
(4)构建Mapper层上的接口CultureMapper
public interface CultureMapper extends BaseMapper<Culture> {
}
(5)构建controller层上的类CultureController.java
@RestController
@RequestMapping("/api/culture")
public class CultureController {
@Autowired
private CultureService cultureService;
//CURD
@PostMapping("/create")
public Boolean create(Culture culture){
return cultureService.save(culture);
}
@PostMapping("/update")
public Boolean update(Culture culture){
return cultureService.updateById(culture);
}
@GetMapping("/delete")
public Boolean delete(Integer id){
return cultureService.removeById(id);
}
@GetMapping("/all")
public List<Culture> all(){
return cultureService.list();
}
@GetMapping("/one")
public Culture one(Integer id){
return cultureService.getById(id);
}
}
五、测试
(1).添加数据
点击发送响应内容返回true表示添加数据成功!
(2).删除数据
注意:这里的删除是只删除在线文档上的,实际的数据库中是没有被删除的哦!
(3).查询数据
1).查询所有数据
2). 查询单个数据
(4).修改数据
标签:Flex,knife4J,return,Culture,private,id,culture,Mybatis,public From: https://blog.csdn.net/2301_80449758/article/details/142874899注意:此处根据id来修改数据哦!