import com.alibaba.fastjson.JSONObject;
import com.alibaba.gts.flm.base.util.MysqlUtil;
import com.alibaba.gts.flm.base.util.StringUtils;

import java.util.Set;

public class Json转表结构 {

    private static JSONObject j;

    public static void main(String[] args) {
        j = JSONObject.parseObject(s);

        Set<String> keys = j.keySet();

        StringBuffer ddl = new StringBuffer();
        ddl.append("CREATE TABLE lkyw_vehicle_info (n" +
                "  `pkid` bigint(20) NOT NULL AUTO_INCREMENT COMMENT '主键id',n" +
                "  `create_time` datetime DEFAULT NULL COMMENT '创建时间',n" +
                "  `modify_time` datetime DEFAULT NULL COMMENT '修改时间',n");
        // 遍历键并获取类型
        for (String key : keys) {
            Class<?> keyType = j.get(key).getClass();
            String xhx = StringUtils.tuoFengToxiaHuaXian(key);
            ddl.append("  `" + xhx + "` " + MysqlUtil.getMysqlType(getJavaObject(keyType.toString())) + " DEFAULT NULL COMMENT '" + "" + "',n");
        }

        ddl.append(
                "  PRIMARY KEY (`pkid`)n" +
                        ") ENGINE=InnoDB DEFAULT CHARSET=utf8mb3 COMMENT='';");
        System.out.println(ddl);
    }

    public static String getJavaObject(String kt) {
        switch (kt) {
            case "java.lang.String":
                return "String";
            case "java.lang.Integer":
            case "java.lang.Long":
                return "Long";
            case "java.lang.Float":
            case "java.lang.Double":
            case "class java.math.BigDecimal":
                return "Double";
            default:
                break;
        }
        return "String";
    }

    public static String s = "{n" +
            "            "age": "6",n" +
            "            "name": "张三",n" +
            "            "speed": 553.8017520000001,n" +
            "            "lng": 103.980244,n" +
            "            "lat": 30.694589n" +
            "        }";
}
MysqlUtil

import java.util.LinkedList;
import java.util.List;

/**
 * @Author: liyue
 * @Date: 2022/08/17/11:39
 * @Description:
 */
public class MysqlUtil {
    public static final List<String> types = new LinkedList<String>() {{
        add("tinyint");
        add("int");
        add("integer");
        add("bigint");
        add("float");
        add("double");
        add("char");
        add("varchar");
        add("datetime");
        add("text");
    }};

    public static String getJavaType(String mysqlType) {
        switch (mysqlType) {
            case "tinyint":
            case "int":
            case "integer":
                return "Integer";
            case "bigint":
                return "Long";
            case "float":
                return "Float";
            case "double":
                return "Double";
            case "text":
            case "char":
            case "varchar":
            case "datetime":
                return "String";
            default:
                throw new RuntimeException("未找到java映射类型");
        }
    }

    public static String getMysqlType(String javaType) {
        switch (javaType) {
            case "String":
                return "varchar(100)";
            case "Long":
                return "bigint";
            case "Double":
                return "double";
            case "double":
                return "Double";
            default:
                throw new RuntimeException("未找到mysql映射类型");
        }
    }
}
StringUtils

Java 常用工具类(22) : 字符串处理_java 字符处理 工具包-CSDN博客

原文地址:https://blog.csdn.net/Lxinccode/article/details/134688491

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任

如若转载,请注明出处:http://www.7code.cn/show_39984.html

如若内容造成侵权/违法违规/事实不符,请联系代码007邮箱suwngjj01@126.com进行投诉反馈,一经查实,立即删除

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注