根据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);}}
相关文章推荐
- 根据建表语句生成实体类和查询用的ResultMap
- 根据表生成添加、修改语句的程序
- 根据表名自动生成I/S/U/D的SQL语句,用于MSSQL2000/2005/2008
- 根据json生成对应属性
- Gson根据json字符串生成map对象
- 根据表或者视图自动生成分页语句的存储过程,适用于Sql Server 2005及以上版本
- 根据json来生成打印java字段(属性),解析方法,类--1
- 关于导出oracle多个表的建表语句DLL,生成.sql语句。
- 几个收藏的根据数据库生成Insert语句的存储过程[修正版]
- 通过YAJL生成json语句
- Tp3.2 RESTFul 根据地区查询生成xml、html、json的Api接口的天气信息
- CodeSimth生成数据库建表语句
- pb根据sql语句生成数据窗口
- 根据表名自动生成INSERT,UPDATE,DELETE,SELECT的SQL语句
- 根据表名自动生成I/S/U/D的SQL语句,用于MSSQL2000/2005/2008
- sql server根据表中数据生成insert语句
- 提供了根据表中的数据自动生成insert语句的数据库包,还提供了一些简单的工具
- sql server 小数据插入,根据表名生成insert 语句,求加固
- 根据DELTA自动生成SQL语句
- 根据JSON数据,自动生成Table