数据库字段以及注释生成json格式

本文介绍了如何通过SQL查询和Java代码结合,自动生成数据库表字段及其注释的JSON格式,以简化YApi开发文档的参数备注编写工作。分享了一段SQL查询和Java代码示例,执行后可以得到转换好的JSON内容,适用于快速生成接口文档。
摘要由CSDN通过智能技术生成

使用YApi做开发文档的时候,要手写各种参数备注,十分乏味,我就简单的想了个办法,通过sql和代码结合,简单的将表字段以及注释生成想要的json格式。话不多说,上代码

  1. sql代码如下
SELECT CONCAT
(',"',
column_name,
'": {"type": "', 
CASE `data_type` 
 WHEN 'varchar' THEN 'String' 
 WHEN 'timestamp' THEN 'Data' 
 WHEN 'int' THEN 'integer'
 WHEN 'text' THEN 'String'
 ELSE NULL END,
 '","description": "',
column_comment,
'"}'
)test  FROM information_schema.columns 
WHERE table_schema = '数据库名称' 
AND table_name ='表名称';

填写上自己的数据库名称和表名称之后,执行会生成以下的内容,复制所有行到剪切板
在这里插入图片描述

2. 下面是java代码

public class StringService {
   
    /**
     *执行sql之后将结果赋值过来生成该表的josn格式
     * @param args
     * @throws Exception
     */
    public static void main(String[] args) throws Exception {
   
        String testString= ",\"sr_uid\": {\"type\": \"integer\",\"description\": \"发布人id\"}\n" +
                ",\"title\": {\"type\": \"String\",\"description\": \"拼租标题\"}\n" +
                ",\"address\": {\"type\": \"String\",\"description\": \"期望批租地点\"}\n" +
                ",\"rent_time\": {\"type\": \"Data\",\"description\": \"期望入住时间\"}\n" +
                ",\"rent_budget\": {\"type\": \"String\",\"description\": \"租金预算\"}\n" +
                ",\"sex\": {\"type\": \"integer\",\"description\": \"要求性别 0女生 1男生\"}\n" +
                ",\"age\": {\"type\": \"integer\",\"description\": \"年龄\"}\n" +
                ",\"hometown\": {\"type\": \"String\",\"description\": \"籍贯\"}\n" +
                ",\"educational_form\": {\"type\": \"String\",\"description\": \"教育形式\"}\n" +
                ",\"education\": {\"type\": \"String\",\"description\": \"学历\"}\n" +
                ",\"school_name\": {\&#
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值