Helius
2021-02-26 de04085526eda992155716eda98af621ad681e4e
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
package com.matrix.codeGeneration.convert;
 
/**
 * 默认的名称实现的转换器
 * 
 * @author Administrator
 *
 */
public class DefaultNameConvert implements NameConvert {
 
    @Override
    public String propertyToColumn(String property) {
        StringBuffer columnName = new StringBuffer(property);
        for (int i = 0; i < columnName.length(); i++) {
            if (columnName.charAt(i) >= 'A' && columnName.charAt(i) <= 'Z') {
                String upcase = "_" + (columnName.charAt(i) + "").toLowerCase();
                columnName.replace(i, i + 1, upcase);
            }
        }
        return columnName.toString();
    }
 
    /**
     * 
     */
    @Override
    public String columnToProperty(String column) {
        // 如果数据库字段为大写则使用这里的方法
        // StringBuffer property = new
        // StringBuffer(tableNameToClassName(column.toLowerCase()));
 
        StringBuffer property = new StringBuffer(tableNameToClassName(column));
        String tempF = (property.charAt(0) + "").toLowerCase();
        property.replace(0, 1, tempF);
        return property.toString();
    }
 
    @Override
    public String classNameToTableName(String calssName) {
        return propertyToColumn(calssName).substring(1, propertyToColumn(calssName).length());
    }
 
    /**
     * 
     * 首字母改为大写 去除_ 下划线后一个字符变为大写字符
     */
    @Override
    public String tableNameToClassName(String tableName) {
        StringBuffer calssName = new StringBuffer(tableName);
        String tempF = (calssName.charAt(0) + "").toUpperCase();
        calssName.replace(0, 1, tempF);
        for (int i = 0; i < calssName.length(); i++) {
            if (calssName.charAt(i) == '_' && i + 1 < tableName.length()) {
                String upcase = (calssName.charAt(i + 1) + "").toUpperCase();
                calssName.replace(i, i + 2, upcase);
            }
        }
        return calssName.toString();
    }
 
    /**
     * 第一个字母大写
     */
    @Override
    public String propertyToMethod(String property) {
        StringBuffer method = new StringBuffer(property);
        String tempF = (method.charAt(0) + "").toUpperCase();
        method.replace(0, 1, tempF);
        return method.toString();
    }
 
    /**
     * 第一个字母小写
     */
    @Override
    public String classNameToVariableName(String className) {
        StringBuffer variableName = new StringBuffer(className);
        String tempF = (variableName.charAt(0) + "").toLowerCase();
        variableName.replace(0, 1, tempF);
        return variableName.toString();
    }
 
    public static void main(String[] args) {
        DefaultNameConvert convert = new DefaultNameConvert();
        System.out.println(convert.propertyToColumn("userName"));
 
    }
}