根据json生成建表语句

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.Data
public 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);

    }


}

猜你喜欢

转载自blog.csdn.net/slq_jobs/article/details/78320194