根据json生成建表语句
来源:互联网 发布:java考试系统源代码 编辑:程序博客网 时间:2024/04/30 14:10
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 = "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()); @Test public 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 = strings.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); }}
阅读全文
0 0
- 根据json生成建表语句
- 在MySQL中执行脚本根据MySQL表生成hive建表语句
- Excel自动生成sql建表语句
- pdm工具批量生成建表语句
- 使用查询脚本生成建表语句
- PowerDesigner生成sql建表语句
- python:sql建表语句转化为json
- oracle建表语句
- 建表语句:t_number
- mysql建表语句
- SQL建表语句
- mysql建表语句
- Oracle建表语句
- 建表语句示例
- AndroidSQLite建表语句
- oracle建表语句
- MySQL建表语句
- Oracle建表语句
- Flask Web中的 url_for 函数
- TOP100summit:【分享实录】Twitter 新一代实时计算平台Heron
- python使用pip安装SciPy、SymPy、matplotlib
- leetcode 355. Design Twitter
- Redis安装及主从配置
- 根据json生成建表语句
- Leetcode #6 ZigZag Conversion
- Android Studio 默认keystore 以及自定义keystore
- mybatis #{} 以及 ${}
- 32、Selenium + Python 实现 UI 自动化测试-正片4
- ubuntu安装sublime并且解决输入中文问题
- IT培训学费2万值得去吗?看老司机怎么说
- Spring的@Transactional注解的rollbackFor属性
- Android系统启动之BOOT_COMPLETED广播