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