1
+ /*
2
+ The MIT License (MIT)
3
+
4
+ Copyright (c) 2014 abel533@gmail.com
5
+
6
+ Permission is hereby granted, free of charge, to any person obtaining a copy
7
+ of this software and associated documentation files (the "Software"), to deal
8
+ in the Software without restriction, including without limitation the rights
9
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10
+ copies of the Software, and to permit persons to whom the Software is
11
+ furnished to do so, subject to the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be included in
14
+ all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22
+ THE SOFTWARE.
23
+ */
24
+
1
25
package com .isea533 .mybatis .mapperhelper ;
2
26
3
27
import javax .persistence .*;
4
28
import java .lang .reflect .Field ;
29
+ import java .lang .reflect .Modifier ;
5
30
import java .util .*;
6
- import java .util .regex .Matcher ;
7
- import java .util .regex .Pattern ;
8
31
9
32
/**
10
- * 初始化 Entity 结构
33
+ * 实体类工具类
34
+ * <p>项目地址 : <a href="https://github.com/abel533/Mapper" target="_blank">https://github.com/abel533/Mapper</a></p>
11
35
*
12
- * @author huangyong
13
- * @since 1.0
36
+ * @author liuzh
14
37
*/
15
38
public class EntityHelper {
16
39
@@ -114,10 +137,10 @@ public static String getTableName(Class<?> entityClass) {
114
137
String tableName = entityClassTableName .get (entityClass );
115
138
if (tableName == null ) {
116
139
initEntityNameMap (entityClass );
140
+ tableName = entityClassTableName .get (entityClass );
117
141
}
118
- tableName = entityClassTableName .get (entityClass );
119
142
if (tableName == null ) {
120
- throw new RuntimeException ("" );
143
+ throw new RuntimeException ("无法获取实体类" + entityClass . getCanonicalName () + "对应的表名! " );
121
144
}
122
145
return tableName ;
123
146
}
@@ -155,9 +178,10 @@ public static List<EntityColumn> getPKColumns(Class<?> entityClass) {
155
178
public static String getSelectColumns (Class <?> entityClass ) {
156
179
List <EntityColumn > columnList = getColumns (entityClass );
157
180
StringBuilder selectBuilder = new StringBuilder ();
181
+ boolean skipAlias = Map .class .isAssignableFrom (entityClass );
158
182
for (EntityColumn entityColumn : columnList ) {
159
183
selectBuilder .append (entityColumn .getColumn ());
160
- if (!entityColumn .getColumn ().equalsIgnoreCase (entityColumn .getProperty ())) {
184
+ if (!skipAlias && ! entityColumn .getColumn ().equalsIgnoreCase (entityColumn .getProperty ())) {
161
185
selectBuilder .append (" " ).append (entityColumn .getProperty ().toUpperCase ()).append ("," );
162
186
} else {
163
187
selectBuilder .append ("," );
@@ -239,11 +263,10 @@ public static synchronized void initEntityNameMap(Class<?> entityClass) {
239
263
if (field .isAnnotationPresent (SequenceGenerator .class )) {
240
264
SequenceGenerator sequenceGenerator = field .getAnnotation (SequenceGenerator .class );
241
265
if (sequenceGenerator .sequenceName ().equals ("" )) {
242
- throw new RuntimeException (entityClass + "字段" + field .getName ()+ "的注解@SequenceGenerator未指定sequenceName!" );
266
+ throw new RuntimeException (entityClass + "字段" + field .getName () + "的注解@SequenceGenerator未指定sequenceName!" );
243
267
}
244
268
entityColumn .setSequenceName (sequenceGenerator .sequenceName ());
245
- }
246
- else if (field .isAnnotationPresent (GeneratedValue .class )) {
269
+ } else if (field .isAnnotationPresent (GeneratedValue .class )) {
247
270
GeneratedValue generatedValue = field .getAnnotation (GeneratedValue .class );
248
271
if (generatedValue .generator ().equals ("UUID" )) {
249
272
if (field .getType ().equals (String .class )) {
@@ -283,30 +306,67 @@ else if (field.isAnnotationPresent(GeneratedValue.class)) {
283
306
* 将驼峰风格替换为下划线风格
284
307
*/
285
308
public static String camelhumpToUnderline (String str ) {
286
- Matcher matcher = Pattern .compile ("[A-Z]" ).matcher (str );
287
- StringBuilder builder = new StringBuilder (str );
288
- for (int i = 0 ; matcher .find (); i ++) {
289
- builder .replace (matcher .start () + i , matcher .end () + i , "_" + matcher .group ().toLowerCase ());
290
- }
291
- if (builder .charAt (0 ) == '_' ) {
292
- builder .deleteCharAt (0 );
309
+ final int size ;
310
+ final char [] chars ;
311
+ final StringBuilder sb = new StringBuilder (
312
+ (size = (chars = str .toCharArray ()).length ) * 3 / 2 + 1 );
313
+ char c ;
314
+ for (int i = 0 ; i < size ; i ++) {
315
+ c = chars [i ];
316
+ if (isLowercaseAlpha (c )) {
317
+ sb .append (toUpperAscii (c ));
318
+ } else {
319
+ sb .append ('_' ).append (c );
320
+ }
293
321
}
294
- return builder .toString ();
322
+ return sb . charAt ( 0 ) == '_' ? sb . substring ( 1 ): sb .toString ();
295
323
}
296
324
297
325
/**
298
326
* 将下划线风格替换为驼峰风格
299
327
*/
300
- public static String underlineToCamelhump (String str ) {
301
- Matcher matcher = Pattern .compile ("_[a-z]" ).matcher (str );
302
- StringBuilder builder = new StringBuilder (str );
303
- for (int i = 0 ; matcher .find (); i ++) {
304
- builder .replace (matcher .start () - i , matcher .end () - i , matcher .group ().substring (1 ).toUpperCase ());
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
+ } else {
337
+ c = (lastUnderscore && count != 0 ) ? toUpperAscii (c ) : toLowerAscii (c );
338
+ buffer [count ++] = c ;
339
+ lastUnderscore = false ;
340
+ }
305
341
}
306
- if (Character . isUpperCase ( builder . charAt ( 0 )) ) {
307
- builder . replace ( 0 , 1 , String . valueOf ( Character . toLowerCase ( builder . charAt ( 0 ))) );
342
+ if (count != buffer . length ) {
343
+ buffer = subarray ( buffer , 0 , count );
308
344
}
309
- return builder .toString ();
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 ;
352
+ }
353
+
354
+ public static boolean isLowercaseAlpha (char c ) {
355
+ return (c >= 'a' ) && (c <= 'z' );
356
+ }
357
+
358
+ public static char toUpperAscii (char c ) {
359
+ if (isLowercaseAlpha (c )) {
360
+ c -= (char ) 0x20 ;
361
+ }
362
+ return c ;
363
+ }
364
+
365
+ public static char toLowerAscii (char c ) {
366
+ if ((c >= 'A' ) && (c <= 'Z' )) {
367
+ c += (char ) 0x20 ;
368
+ }
369
+ return c ;
310
370
}
311
371
312
372
/**
@@ -324,10 +384,18 @@ private static List<Field> getAllField(Class<?> entityClass, List<Field> fieldLi
324
384
return fieldList ;
325
385
}
326
386
Field [] fields = entityClass .getDeclaredFields ();
327
- fieldList .addAll (Arrays .asList (fields ));
328
- if (entityClass .getSuperclass () != null && !entityClass .getSuperclass ().equals (Object .class )) {
387
+ for (Field field : fields ) {
388
+ //排除静态字段,解决bug#2
389
+ if (!Modifier .isStatic (field .getModifiers ())) {
390
+ fieldList .add (field );
391
+ }
392
+ }
393
+ if (entityClass .getSuperclass () != null
394
+ && !entityClass .getSuperclass ().equals (Object .class )
395
+ && !Map .class .isAssignableFrom (entityClass .getSuperclass ())
396
+ && !Collection .class .isAssignableFrom (entityClass .getSuperclass ())) {
329
397
return getAllField (entityClass .getSuperclass (), fieldList );
330
398
}
331
399
return fieldList ;
332
400
}
333
- }
401
+ }
0 commit comments