bean 字段生成工具

bean 字段生成工具

从Mysql的数据库字段,导出成bean的字段

1. 先查询mysql表字段:

 

select DATA_TYPE, COLUMN_NAME 

from information_schema.COLUMNS 

where TABLE_NAME = 'INT_LOGISTICS_INFO'

 

得到结果:

int INT_LOGISTICS_INFO_ID

 

2. 然后运行工具类:

public static void transfromDBColumnToBean(String fileName) {
        try (BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(new File(fileName))))) {
            String str = null;
            while ((str = br.readLine()) != null) {
                if (!"".equals(str.trim())) {
                    StringBuilder sb = new StringBuilder("private ");
                    str = str.toLowerCase();
                    // remove the space between type and field
                    str = str.replaceAll("\\s+", "=");
                    String[] ty_fi = str.split("=");
                    switch (ty_fi[0]) {
                    case "int":
                        sb.append("int ");
                        break;
                    case "varchar":
                        sb.append("String ");
                        break;
                    case "timestamp":
                        // java.util.Date
                        sb.append("Date ");
                        break;
                    }
                    String[] filedStrs = null;
                    if (ty_fi[1].indexOf("_") > 0) {
                        filedStrs = ty_fi[1].split("_");
                        sb.append(filedStrs[0]);
                        for (int i = 1; i < filedStrs.length; i++) {
                            sb.append(Character.toUpperCase(filedStrs[i].charAt(0)));
                            sb.append(filedStrs[i].substring(1));
                        }
                    } else {
                        sb.append(ty_fi[1]);
                    }
                    sb.append(";");
                    System.out.println(sb.toString());
                }
            }
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

 

得到结果:

private int intLogisticsInfoId;

 

目前只支持 int, varchar, timestamp 3种类型, 可以继续完善

 

你可能感兴趣的:(bean)