您的位置:首页 > Web前端 > JavaScript

根据json生成建表语句

2017-10-23 16:29 176 查看
package com.shuquanlin.contact;

/**
* Created by sql on 10/23/17.
*/
public class SqlSentence {

public static String INTEGER = "java.lang.Integer";
public static String LONG = "java.lang.Long";
public static String STRING = "java.lang.String";
public static String JSONOBJECT
4000
= "com.alibaba.fastjson.JSONObject";
public static String FLOAT = "java.lang.Float";
public static String DOUBLE = "java.lang.Double";
public static String BIG_DECIMAL = "java.math.BigDecimal";
public static String DATE = "java.util.Date";

}
package com.shuquanlin.domain;import java.util.List;/*** Created by sql on 17-6-22.*/@lombok.Datapublic class JsonMetaNode {private String key;private String valueType;//数据库中的列名private String dbColName;private List<JsonMetaNode> children;public JsonMetaNode() {}public JsonMetaNode(String key, String valueType) {this.key = key;this.valueType = valueType;}}
package com.shuquanlin.util;import com.shuquanlin.contact.SqlSentence;import com.shuquanlin.domain.JsonMetaNode;import java.util.List;/*** Created by sql on 10/23/17.*/public class SqlUtil {/*** 建表语句* @author sql* @date 10/23/17 3:43 PM*/public static String createTable(String tableName , List<JsonMetaNode> jsonMetaNodeList){String sqlCreate = "CREATE TABLE " + tableName + "(\n" + getRowName(jsonMetaNodeList);return sqlCreate;}/*** 获取建表语句的列名* @author sql* @date 10/23/17 3:43 PM*/private static String getRowName(List<JsonMetaNode> jsonMetaNodeList){StringBuffer sqlRowNameBuffer = new StringBuffer();for (JsonMetaNode jsonMetaNode:jsonMetaNodeList) {String key = jsonMetaNode.getKey();String valueType = jsonMetaNode.getValueType();String type = "";if(SqlSentence.INTEGER.equals(valueType)){type = "int(100)";}else if(SqlSentence.LONG.equals(valueType)){type = "bigint(100)";}else if(SqlSentence.STRING.equals(valueType)){type = "varchar(100)";}else if(SqlSentence.BIG_DECIMAL.equals(valueType)){type = "decimal(18,8)";}else if(SqlSentence.FLOAT.equals(valueType)){type = "float(100,10)";}else if(SqlSentence.DOUBLE.equals(valueType)){type = "double(100,10)";}else if(SqlSentence.DATE.equals(valueType)){type = "datetime";}else{type = "varchar(100)";}sqlRowNameBuffer.append(key).append(" ").append(type).append(" ").append("CHARACTER SET utf8 NULL ,");}sqlRowNameBuffer.deleteCharAt(sqlRowNameBuffer.length()-1);sqlRowNameBuffer.append(")");String sqlRowName = sqlRowNameBuffer.toString();return sqlRowName;}
package com.shuquanlin;import com.alibaba.fastjson.JSON;import com.alibaba.fastjson.JSONObject;import com.shuquanlin.domain.JsonMetaNode;import com.shuquanlin.util.SqlUtil;import org.assertj.core.util.Lists;import org.junit.jupiter.api.Test;import org.slf4j.Logger;import org.slf4j.LoggerFactory;import java.io.*;import java.util.Iterator;import java.util.List;import java.util.Set;/*** Created by sql on 10/23/17.*/public class JsonTest {Logger logger = LoggerFactory.getLogger(this.getClass());@Testpublic void test1() throws IOException {List<JsonMetaNode> jsonMetaNodeList = Lists.newArrayList();// 读取文件File file = new File("/home/shuquanlin/json.txt");FileReader fileReader = null;try {fileReader = new FileReader(file);} catch (FileNotFoundException e) {logger.error("文件不存在",e.getMessage(),e);}BufferedReader bufferedReader = new BufferedReader(fileReader);String s = null;StringBuffer stringBuffer = new StringBuffer();// 讲文件所有的内存读取出来while ((s = bufferedReader.readLine()) != null) {stringBuffer.append(s);}// 转换成字符串if (stringBuffer != null) {s = stringBuffer.toString();}// 转换成json对象JSONObject jsonObject = (JSONObject) JSON.parse(s);Set<String> strings = jsonObject.keySet();Iterator<String> iterator = stri9bd0ngs.iterator();// 遍历json对象,根据key获取value并获取value的类型while (iterator.hasNext()) {JsonMetaNode jsonMete = new JsonMetaNode();String next = iterator.next();jsonMete.setKey(next);Object o = jsonObject.get(next);String name = o.getClass().getName();jsonMete.setValueType(name);jsonMetaNodeList.add(jsonMete);}// 调用建表语句的方法String sqlCreateTable = SqlUtil.createTable("sql_test", jsonMetaNodeList);System.out.println(sqlCreateTable);}}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  json 建表 语句