Repository navigation
Expand file tree
/
Copy pathModelTools.java
More file actions
190 lines (169 loc) · 5.73 KB
/
Copy pathModelTools.java
File metadata and controls
190 lines (169 loc) · 5.73 KB
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
package org.ithang.tools;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import org.apache.log4j.Logger;
import org.ithang.Model;
import org.ithang.tools.lang.StrUtils;
import org.ithang.tools.mate.Column;
import org.ithang.tools.mate.Ignore;
import org.ithang.tools.mate.Primary;
import org.ithang.tools.mate.Table;
public final class ModelTools {
private final static Random r=new Random(System.currentTimeMillis());
private final static String[] tableLabels=new String[]{"a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","x","y","z","A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","X","Y","Z"};
private final static Logger logger=Logger.getLogger(ModelTools.class);
/**
* 解析class对象,获得所有字段属性的相关信息
* @param cls
* @return List<Model> 每个字段对应一个Model对象
*/
public static List<Model> parseCls(Class<?> cls){
String tableName=null;
String tableLabel=null;
Model m=null;
Field[] fields=null;
List<Model> mds=null;
try{
tableName=getTableName(cls);
//为表创建一个别名,用于字段前缀
tableLabel=tableLabels[r.nextInt(tableLabels.length)]+String.valueOf(r.nextInt(36));
fields=cls.getDeclaredFields();
if(null==fields||0==fields.length){
logger.error("无效Class对象,得不到任何字段信息!");
return null;
}
mds=new ArrayList<Model>(fields.length);
for(Field field:fields){
//不处理序列化字段
if("serialVersionUID".equals(field.getName())){
continue;
}
//如果存在ignore注解,则表示不处理该字段,该字段不对应表中任何列
if(field.isAnnotationPresent(Ignore.class)){
continue;
}
m=new Model();
m.setTableName(tableName);
m.setFieldName(field.getName());
m.setFieldType(field.getType().getSimpleName());
m.setTableLabel(tableLabel);
//判断是否存在column注解,如果存在则用注解值作为列名
if(field.isAnnotationPresent(Column.class)){
Column column=field.getAnnotation(Column.class);
m.setColumnName(column.value());
if(StrUtils.isNotBlank(column.defaultValue())){
m.setValue(column.defaultValue());
}else{
m.setValue("NULL");
}
}else{
m.setColumnName(StrUtils.addUnderline(field.getName()));
m.setValue("NULL");
}
//判断是不是主键
if(field.isAnnotationPresent(Primary.class)){
Primary pri=field.getAnnotation(Primary.class);
if(null!=pri&&null!=pri.Seq()&&pri.Seq().trim().length()>0){//设置主键sequence
m.setSequence(pri.Seq());
}
m.setPrimary(true);
}else{
m.setPrimary(false);
}
mds.add(m);
}
}catch(Exception e){
e.printStackTrace();
}
return mds;
}
/**
* 得到表名
* @param cls
* @return
*/
public static String getTableName(Class<?> cls){
if(cls.isAnnotationPresent(Table.class)){
Table table=cls.getAnnotation(Table.class);
if(null!=table&&StrUtils.isNotBlank(table.value())){
return table.value();
}
}
return StrUtils.addUnderline(cls.getSimpleName());
}
public static Field getField(Class<?> cls,String fieldName){
Field field=null;
try{
field=cls.getDeclaredField(fieldName);
}catch(Exception e){
e.printStackTrace();
}
return field;
}
public static String getFieldValue(Object bean,String fieldName){
Field field=null;
try{
field=getField(bean.getClass(),fieldName);
Class<?> fieldType=field.getType();
Object v=bean.getClass().getMethod("get"+StrUtils.headUpper(fieldName)).invoke(bean);
if(fieldType==String.class){
return "'"+(v==null?"":v)+"'";
}else{
return v==null?"0":String.valueOf(v);
}
}catch(Exception e){
e.printStackTrace();
}
return null;
}
public static String[] getFieldNames(Class<?> cls){
String[] fieldNames=null;
try{
Field[] fields=cls.getDeclaredFields();
if(null!=fields&&fields.length>0){
fieldNames=new String[fields.length];
int i=0;
for(Field field:fields){
fieldNames[i++]=field.getName();
}
}
}catch(Exception e){
e.printStackTrace();
}
return fieldNames;
}
public static String[] getColumnNames(Class<?> cls){
String[] columnNames=null;
try{
Field[] fields=cls.getDeclaredFields();
if(null!=fields&&fields.length>0){
columnNames=new String[fields.length];
int i=0;
for(Field field:fields){
if(field.isAnnotationPresent(Column.class)){
columnNames[i++]=field.getAnnotation(Column.class).value();
}else{
columnNames[i++]=StrUtils.addUnderline(field.getName());
}
}
}
}catch(Exception e){
e.printStackTrace();
}
return columnNames;
}
/*public static void generBeanFromMySQL(Connection conn,String schema) throws SQLException{
QueryRunner qr=new QueryRunner();
List<String> tables=qr.query(conn,"select table_name from information_schema.columns where table_schema='"+schema+"'", new ColumnListHandler<String>());
if(null!=tables&&tables.size()>0){
for(String table:tables){
List<Column> columns=qr.query(conn, "select table_name,column_name,is_nullable,data_type,column_key,character_maximum_length len from information_schema.columns where table_schema='"+schema+"'", new BeanListHandler<Column>(Column.class));
}
}
}
private static List<Model> parseModelFromTable(List<Column> columns){
return null;
}*/
}