JAVA通过实体类生成数据库查询语句(驼峰命名规则)

 

 

import java.io.IOException;
import java.lang.reflect.Field;
import java.util.HashMap;
import java.util.Map;

/***
 * 通过java Class 生成查询语句
 */
public class SelectTable {
    public static Map<String, String> javaProperty2SqlColumnMap = new HashMap<>();

    static {
        javaProperty2SqlColumnMap.put("Integer", "INTEGER");
        javaProperty2SqlColumnMap.put("Short", "tinyint");
        javaProperty2SqlColumnMap.put("Long", "bigint");
        javaProperty2SqlColumnMap.put("BigDecimal", "decimal(19,2)");
        javaProperty2SqlColumnMap.put("Double", "double precision not null");
        javaProperty2SqlColumnMap.put("Float", "float");
        javaProperty2SqlColumnMap.put("Boolean", "tinyint");
        javaProperty2SqlColumnMap.put("Timestamp", "datetime");
        javaProperty2SqlColumnMap.put("String", "VARCHAR(255)");
        javaProperty2SqlColumnMap.put("Date", "datetime");
    }


    /**
     * @param args
     * @throws IOException
     */
    public static void main(String[] args) throws IOException {
        selectTable(Test.class, "g_test");

    }

    public static String selectTable(Class obj, String tableName) throws IOException {


        Field[] fields = null;
        fields = obj.getDeclaredFields();
        String column = null;
        StringBuilder sb = null;
        sb = new StringBuilder(50);


        String tableAlias = getTableAlias(tableName);

        sb.append("select ");
        for (Field f : fields) {
            column = f.getName();
            if (column.equals("serialVersionUID")) {
                continue;
            }

            String columnName = camelToUnderline(column, 1);

            sb.append(tableAlias+"."+columnName + "");
            sb.append(" as ");
            sb.append(column);
            sb.append(" ,");


        }

        sb.deleteCharAt(sb.length() - 1);


        sb.append(" from " + tableName+" "+tableAlias);
        String sql = sb.toString();

        System.out.println(sql);
        System.out.println("#----------------------------------------");

        return sql;


    }


    /**
     * 获取表名的别名 最后一个单词的首字母
     * @param tableName
     * @return
     */
    public static String getTableAlias(String tableName) {
        String[] split = tableName.split("_");
        String table = split[split.length - 1];
        return table.charAt(0) + "";

    }


    /**
     * 驼峰转下划线
     *
     * @param param
     * @param charType
     * @return
     */
    public static String camelToUnderline(String param, Integer charType) {
        if (param == null || "".equals(param.trim())) {
            return "";
        }
        int len = param.length();
        StringBuilder sb = new StringBuilder(len);
        for (int i = 0; i < len; i++) {
            char c = param.charAt(i);
            if (Character.isUpperCase(c)) {
                sb.append("_");
            }
            if (charType == 2) {
                sb.append(Character.toUpperCase(c));  //统一都转大写
            } else {
                sb.append(Character.toLowerCase(c));  //统一都转小写
            }


        }

        String s = sb.toString();
        if (s.startsWith("_")) {
            s = s.substring(1, s.length());
        }

        return s;
    }
}

 

posted @ 2021-08-12 16:08  yvioo  阅读(654)  评论(0编辑  收藏  举报