@@ -37,6 +37,46 @@ of this software and associated documentation files (the "Software"), to deal
37
37
*/
38
38
public class EntityHelper {
39
39
40
+ /**
41
+ * 实体对应表的配置信息
42
+ */
43
+ public static class EntityTable {
44
+ private String name ;
45
+ private String catalog ;
46
+ private String schema ;
47
+
48
+ public void setTable (Table table ){
49
+ this .name = table .name ();
50
+ this .catalog = table .catalog ();
51
+ this .schema = table .schema ();
52
+ }
53
+
54
+ public String getName () {
55
+ return name ;
56
+ }
57
+
58
+ public String getCatalog () {
59
+ return catalog ;
60
+ }
61
+
62
+ public String getSchema () {
63
+ return schema ;
64
+ }
65
+
66
+ public String getPrefix () {
67
+ if (catalog != null && catalog .length () > 0 ) {
68
+ return catalog ;
69
+ }
70
+ if (schema != null && schema .length () > 0 ) {
71
+ return catalog ;
72
+ }
73
+ return "" ;
74
+ }
75
+ }
76
+
77
+ /**
78
+ * 实体字段对应数据库列的信息
79
+ */
40
80
public static class EntityColumn {
41
81
private String property ;
42
82
private String column ;
@@ -113,9 +153,9 @@ public void setGenerator(String generator) {
113
153
}
114
154
115
155
/**
116
- * 实体类 => 表名
156
+ * 实体类 => 表对象
117
157
*/
118
- private static final Map <Class <?>, String > entityClassTableName = new HashMap <Class <?>, String >();
158
+ private static final Map <Class <?>, EntityTable > entityTableMap = new HashMap <Class <?>, EntityTable >();
119
159
120
160
/**
121
161
* 实体类 => 全部列属性
@@ -128,21 +168,21 @@ public void setGenerator(String generator) {
128
168
private static final Map <Class <?>, List <EntityColumn >> entityClassPKColumns = new HashMap <Class <?>, List <EntityColumn >>();
129
169
130
170
/**
131
- * 获取表名
171
+ * 获取表对象
132
172
*
133
173
* @param entityClass
134
174
* @return
135
175
*/
136
- public static String getTableName (Class <?> entityClass ) {
137
- String tableName = entityClassTableName .get (entityClass );
138
- if (tableName == null ) {
176
+ public static EntityTable getEntityTable (Class <?> entityClass ) {
177
+ EntityTable entityTable = entityTableMap .get (entityClass );
178
+ if (entityTable == null ) {
139
179
initEntityNameMap (entityClass );
140
- tableName = entityClassTableName .get (entityClass );
180
+ entityTable = entityTableMap .get (entityClass );
141
181
}
142
- if (tableName == null ) {
182
+ if (entityTable == null ) {
143
183
throw new RuntimeException ("无法获取实体类" + entityClass .getCanonicalName () + "对应的表名!" );
144
184
}
145
- return tableName ;
185
+ return entityTable ;
146
186
}
147
187
148
188
/**
@@ -153,7 +193,7 @@ public static String getTableName(Class<?> entityClass) {
153
193
*/
154
194
public static List <EntityColumn > getColumns (Class <?> entityClass ) {
155
195
//可以起到初始化的作用
156
- getTableName (entityClass );
196
+ getEntityTable (entityClass );
157
197
return entityClassColumns .get (entityClass );
158
198
}
159
199
@@ -165,7 +205,7 @@ public static List<EntityColumn> getColumns(Class<?> entityClass) {
165
205
*/
166
206
public static List <EntityColumn > getPKColumns (Class <?> entityClass ) {
167
207
//可以起到初始化的作用
168
- getTableName (entityClass );
208
+ getEntityTable (entityClass );
169
209
return entityClassPKColumns .get (entityClass );
170
210
}
171
211
@@ -226,16 +266,23 @@ public static String getPrimaryKeyWhere(Class<?> entityClass) {
226
266
* @param entityClass
227
267
*/
228
268
public static synchronized void initEntityNameMap (Class <?> entityClass ) {
229
- if (entityClassTableName .get (entityClass ) != null ) {
269
+ if (entityTableMap .get (entityClass ) != null ) {
230
270
return ;
231
271
}
232
272
//表名
273
+ EntityTable entityTable = null ;
233
274
if (entityClass .isAnnotationPresent (Table .class )) {
234
275
Table table = entityClass .getAnnotation (Table .class );
235
- entityClassTableName .put (entityClass , table .name ());
236
- } else {
237
- entityClassTableName .put (entityClass , camelhumpToUnderline (entityClass .getSimpleName ()).toUpperCase ());
276
+ if (!table .name ().equals ("" )) {
277
+ entityTable = new EntityTable ();
278
+ entityTable .setTable (table );
279
+ }
280
+ }
281
+ if (entityTable == null ) {
282
+ entityTable = new EntityTable ();
283
+ entityTable .name = camelhumpToUnderline (entityClass .getSimpleName ()).toUpperCase ();
238
284
}
285
+ entityTableMap .put (entityClass , entityTable );
239
286
//列
240
287
List <Field > fieldList = getAllField (entityClass , null );
241
288
List <EntityColumn > columnList = new ArrayList <EntityColumn >();
@@ -302,6 +349,13 @@ public static synchronized void initEntityNameMap(Class<?> entityClass) {
302
349
entityClassPKColumns .put (entityClass , pkColumnList );
303
350
}
304
351
352
+ public static void main (String [] args ) {
353
+ System .out .println (camelhumpToUnderline ("userName" ));
354
+ System .out .println (camelhumpToUnderline ("userPassWord" ));
355
+ System .out .println (camelhumpToUnderline ("ISO9001" ));
356
+ System .out .println (camelhumpToUnderline ("hello_world" ));
357
+ }
358
+
305
359
/**
306
360
* 将驼峰风格替换为下划线风格
307
361
*/
@@ -313,62 +367,26 @@ public static String camelhumpToUnderline(String str) {
313
367
char c ;
314
368
for (int i = 0 ; i < size ; i ++) {
315
369
c = chars [i ];
316
- if (isLowercaseAlpha (c )) {
317
- sb .append (toUpperAscii (c ));
318
- } else {
370
+ if (isUppercaseAlpha (c )) {
319
371
sb .append ('_' ).append (c );
320
- }
321
- }
322
- return sb .charAt (0 ) == '_' ? sb .substring (1 ): sb .toString ();
323
- }
324
-
325
- /**
326
- * 将下划线风格替换为驼峰风格
327
- */
328
- public static String underlineToCamelhump (String name ) {
329
- char [] buffer = name .toCharArray ();
330
- int count = 0 ;
331
- boolean lastUnderscore = false ;
332
- for (int i = 0 ; i < buffer .length ; i ++) {
333
- char c = buffer [i ];
334
- if (c == '_' ) {
335
- lastUnderscore = true ;
336
372
} else {
337
- c = (lastUnderscore && count != 0 ) ? toUpperAscii (c ) : toLowerAscii (c );
338
- buffer [count ++] = c ;
339
- lastUnderscore = false ;
373
+ sb .append (toUpperAscii (c ));
340
374
}
341
375
}
342
- if (count != buffer .length ) {
343
- buffer = subarray (buffer , 0 , count );
344
- }
345
- return new String (buffer );
346
- }
347
-
348
- public static char [] subarray (char [] src , int offset , int len ) {
349
- char [] dest = new char [len ];
350
- System .arraycopy (src , offset , dest , 0 , len );
351
- return dest ;
376
+ return sb .charAt (0 ) == '_' ? sb .substring (1 ): sb .toString ();
352
377
}
353
378
354
- public static boolean isLowercaseAlpha (char c ) {
355
- return (c >= 'a ' ) && (c <= 'z ' );
379
+ public static boolean isUppercaseAlpha (char c ) {
380
+ return (c >= 'A ' ) && (c <= 'Z ' );
356
381
}
357
382
358
383
public static char toUpperAscii (char c ) {
359
- if (isLowercaseAlpha (c )) {
384
+ if (isUppercaseAlpha (c )) {
360
385
c -= (char ) 0x20 ;
361
386
}
362
387
return c ;
363
388
}
364
389
365
- public static char toLowerAscii (char c ) {
366
- if ((c >= 'A' ) && (c <= 'Z' )) {
367
- c += (char ) 0x20 ;
368
- }
369
- return c ;
370
- }
371
-
372
390
/**
373
391
* 获取全部的Field
374
392
*
0 commit comments