美文网首页
MySQL表转Java对象【小工具】

MySQL表转Java对象【小工具】

作者: 不存在的bug | 来源:发表于2020-08-28 12:45 被阅读0次

    表:


    image.png

    Model:


    image.png
    1.查询表中所有的列及注释
    SET SESSION group_concat_max_len = 10240; -- 如果表中字段太长显示不全,执行该句。
    
    select GROUP_CONCAT(COLUMN_TYPE,'@!@!',COLUMN_COMMENT,'@!@!',COLUMN_NAME separator '&!&!') from information_schema.COLUMNS
    where table_name = 't_pc_chatroom_message' -- @!@!, &!&!自定义分隔符,防止注释部分破坏分割规则
    

    查询结果:
    bigint(64) unsigned@!@!主键ID@!@!id&!&!varchar(50)@!@!@!@!message_id&!&!varchar(50)@!@!租户id@!@!tenant_id&!&!varchar(50)@!@!群db_id@!@!wechat_chatroom_id&!&!varchar(50)@!@!微信db_id@!@!wechat_account_id ...
    2.代码分割

    public static void main(String[] args) {
           String str = "bigint(64) unsigned@!@!主键ID@!@!id&!&!varchar(50)@!@!@!@!message_id&!&!varchar(50)@!@!租户id@!@!tenant_id&!&!varchar(50)@!@!群db_id@!@!wechat_chatroom_id&!&!varchar(50)@!@!微信db_id@!@!wechat_account_id";
           String[] line = str.split("&!&!");
           StringBuilder sb = new StringBuilder();
           for (int i = 0; i < line.length; i++) {
               String subLine = line[i];
    
               String[] subLineArr = subLine.split("@!@!");
               sb.append("/**\n");
               sb.append("* " + subLineArr[subLineArr.length-2]+"\n");
               sb.append("*/\n");
               sb.append("private");
               sb.append(" ");
               if(subLineArr[0].contains("(")){
                   sb.append(toSqlToJava(subLineArr[0].substring(0,subLineArr[0].indexOf("("))));
               }else{
                   sb.append(toSqlToJava(subLineArr[0]));
               }
    
               sb.append(" ");
               String var = subLineArr[subLineArr.length-1];
               if(var.contains("_")){
                   var = CaseFormat.LOWER_UNDERSCORE.to(CaseFormat.LOWER_CAMEL, var);
               }
               sb.append(var);
               sb.append(";");
               sb.append("\n");
           }
           System.out.println(sb);
    
       }
       //百度找的工具
       public static String toSqlToJava(String sqlType) {
           if( sqlType == null || sqlType.trim().length() == 0 ) return sqlType;
           sqlType = sqlType.toLowerCase();
           switch(sqlType){
               case "nvarchar":return "String";
               case "char":return "String";
               case "varchar":return "String";
               case "text":return "String";
               case "longtext":return "String";
               case "nchar":return "String";
               case "blob":return "byte[]";
               case "integer":return "Long";
               case "tinyint":return "Integer";
               case "smallint":return "Integer";
               case "mediumint":return "Integer";
               case "bit":return "Boolean";
               case "bigint":return "Long";
               case "float":return "Fload";
               case "double":return "Double";
               case "decimal":return "BigDecimal";
               case "boolean":return "Boolean";
               case "id":return "Long";
               case "date":return "LocalDate";
               case "datetime":return "LocalDateTime";
               case "year":return "Date";
               case "time":return "java.sql.Time";
               case "timestamp":return "Date";
               case "numeric":return "BigDecimal";
               case "real":return "BigDecimal";
               case "money":return "Double";
               case "smallmoney":return "Double";
               case "image":return "byte[]";
               case "mediumtext":return "String";
               case "json":return "String";
               case "int":return "Integer";
               default:
                   System.out.println("-----------------》转化失败:未发现的类型"+sqlType);
                   break;
           }
           return sqlType;
       }
    

    3.带下划线字段转驼峰使用guava。

    <dependency>
        <groupId>com.google.guava</groupId>
        <artifactId>guava</artifactId>
        <version>21.0</version>
    </dependency>
    

    Over.

    相关文章

      网友评论

          本文标题:MySQL表转Java对象【小工具】

          本文链接:https://www.haomeiwen.com/subject/gzclsktx.html