Convert java entity class to table structure Sql statement and attribute name camel case naming conversion

Purpose: used to pull git code, but the project does not provide the table structure corresponding to the entity class to create sql statements, and convert the java entity class attribute names to camel case naming

package com.mypro.testmodule.entity;

import com.mypro.common.domain.Stu;
import com.mypro.testmodule.entity.testen.Equip;
import io.swagger.annotations.ApiModelProperty;
import java.lang.reflect.Field;
import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Objects;

/**
 * @Author: lsx
 * @Date: 2023/10/17/17:34
 * @Description: The sql statement or attribute name used to generate the corresponding table creation table for java entity class is converted into camel case.
 */
public class EntityTr {
    public static void main(String[] args) {
        List<Object> list=new ArrayList<>();
        Stu stu = new Stu();
        Equip equip = new Equip();
        list.add(stu);
        list.add(equip);
        //Generate camel case named static attributes corresponding to each attribute name of the entity class tableName >> public static String TABLE_NAME = "table_name";
        testReflect(list);
        //Generate SQL table creation statement for entity class CREATE TABLE `DefaultName` (id int(1) DEFAULT NULL ,na_me varchar(50) DEFAULT NULL ,age int(1) DEFAULT NULL )ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
        transferToSql(list);
    }

    //java entity class generates static camel case named value fields for all attributes tableName 》 public static String TABLE_NAME = "table_name";
    public static void testReflect(List<Object> entityList) {
        //Loop through the entity class collection that needs to generate static camel case attribute named values.
        for (Object entity:entityList){
            StringBuilder builder = new StringBuilder();
            for (Field field : entity.getClass().getDeclaredFields()) {
                field.setAccessible(true);
                //Convert field name
                String fieldName = transToUnderScore(field.getName());
                builder.append("public static String " + fieldName.toUpperCase() + " = " + """ + fieldName.toLowerCase() + "";\
");
            }
            //Print output value
            System.out.println(builder.toString());
            //Static camel case naming separator for different entity class attributes
            System.out.println("========================");
        }
    }


    public static void transferToSql(List<Object>entityList){
       //space character
        String em=" ";
        //line break
        String en="\
";
        StringBuilder builder = new StringBuilder();
        //Loop through the collection
        for (Object entity:entityList){
            //Number of entity class attributes
            int length = entity.getClass().getDeclaredFields().length;
            int i=1;
            for (Field field : entity.getClass().getDeclaredFields()) {
                field.setAccessible(true);
                if (i==1){
                    String tableName="entity_defaultName";
                    if ("tableName".equals(field.getName())){
                        try {
                            tableName = field.get(entity).toString();
                        } catch (IllegalAccessException e) {
                            System.out.println("Table name field type is wrong");
                            break;
                        }
                    }
                    builder.append("CREATE TABLE `" + tableName + "` (");
                    if (!"entity_defaultName".equals(tableName)){
                        //The first attribute of the entity class is the table name field, so the corresponding field sql will not be generated for this field.
                        i + + ;
                        continue;
                    }
                }
                //Get the ApiModelProperty annotation of each attribute of the java entity class, which is used to add the remark information of the corresponding field when creating the sql table.
                ApiModelProperty annotation = field.getAnnotation(ApiModelProperty.class);
                builder.append(transToUnderScore(field.getName()).toLowerCase()).append(em);
                String sqlClass = javaClassToSql(field);
                if (sqlClass.startsWith("false")){
                    System.out.println(field.getName() + "Entity class attributes, mysql database does not have corresponding types, please modify or manually add type mapping");
                    break;
                }
                builder.append(" " + sqlClass);
                builder.append(" DEFAULT NULL ");
                //Whether it is the last attribute
                boolean end=i==length?false:true;
                if (!Objects.isNull(annotation)){
                    builder.append("COMMENT '").append(annotation.value()).append("'");
                }
                if (end){
                    builder.append(",");
                }
                i + + ;
            }
            builder.append(" )ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 ;");
            //Different entity class sql statement separator
            builder.append(en + "#========================" + en);

        }
        System.out.println(builder.toString());

    }

  /**
   *Field name camel case naming conversion javaName->java_name
   */
    public static String transToUnderScore(String fieldName){
        char[] chars = fieldName.toCharArray();
        //Create a list collection for storing uppercase letters present in the field
        List<Character> characters = new ArrayList<>();
        for (char c:chars){
            int a=(int)c;
            if (a>=65 & amp; & amp;a<=90){
                //Determine whether the char is an uppercase letter A-Z. A corresponds to 65 and Z corresponds to 90.
                characters.add(Character.valueOf(c));
            }
        }
        for (Character a:characters){
            //Replace the corresponding uppercase letters in the field name with _letter format javaName >> java_Name
            fieldName = fieldName.replace(a.toString(), "_" + a.toString());
        }
        return fieldName;
    }

    /**
     *Sql attributes corresponding to the entity attribute value type. Users can modify the field length and other information of each attribute by themselves.
     */
    public static String javaClassToSql(Field field) {
        Class<?> type = field.getType();
        String restr="false";
       if (type ==Integer.class||type==int.class){
           restr="int(1) ";
       }
       if (type == Long.class||type==long.class){
           restr="bigint(10)";
       }
       if (type == BigDecimal.class){
           restr="decimal(16,2)";
       }
       if (type == Double.class||type==double.class){
           restr="double(14,2)";
       }
       if (type == Float.class||type==float.class){
           restr="float(14.2)";
       }
       if (type == Date.class||type == LocalDateTime.class){
           restr="datetime";
       }
       if (type == String.class){
           restr="varchar(50)";
       }
       if (type ==Character.class||type==char.class){
           restr="char(20)";
       }
       return restr;
    }


}

The knowledge points of the article match the official knowledge files, and you can further learn related knowledge. Java skill treeSelect structural statementif conditional statement 137920 people are learning the system