Skip to content

Commit 90c13fd

Browse files
committed
更新通用Mapper代码到最新。
1 parent c750c49 commit 90c13fd

File tree

4 files changed

+367
-62
lines changed

4 files changed

+367
-62
lines changed

src/main/java/com/isea533/mybatis/mapperhelper/EntityHelper.java

Lines changed: 98 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,39 @@
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+
125
package com.isea533.mybatis.mapperhelper;
226

327
import javax.persistence.*;
428
import java.lang.reflect.Field;
29+
import java.lang.reflect.Modifier;
530
import java.util.*;
6-
import java.util.regex.Matcher;
7-
import java.util.regex.Pattern;
831

932
/**
10-
* 初始化 Entity 结构
33+
* 实体类工具类
34+
* <p>项目地址 : <a href="https://github.com/abel533/Mapper" target="_blank">https://github.com/abel533/Mapper</a></p>
1135
*
12-
* @author huangyong
13-
* @since 1.0
36+
* @author liuzh
1437
*/
1538
public class EntityHelper {
1639

@@ -114,10 +137,10 @@ public static String getTableName(Class<?> entityClass) {
114137
String tableName = entityClassTableName.get(entityClass);
115138
if (tableName == null) {
116139
initEntityNameMap(entityClass);
140+
tableName = entityClassTableName.get(entityClass);
117141
}
118-
tableName = entityClassTableName.get(entityClass);
119142
if (tableName == null) {
120-
throw new RuntimeException("");
143+
throw new RuntimeException("无法获取实体类" + entityClass.getCanonicalName() + "对应的表名!");
121144
}
122145
return tableName;
123146
}
@@ -155,9 +178,10 @@ public static List<EntityColumn> getPKColumns(Class<?> entityClass) {
155178
public static String getSelectColumns(Class<?> entityClass) {
156179
List<EntityColumn> columnList = getColumns(entityClass);
157180
StringBuilder selectBuilder = new StringBuilder();
181+
boolean skipAlias = Map.class.isAssignableFrom(entityClass);
158182
for (EntityColumn entityColumn : columnList) {
159183
selectBuilder.append(entityColumn.getColumn());
160-
if (!entityColumn.getColumn().equalsIgnoreCase(entityColumn.getProperty())) {
184+
if (!skipAlias && !entityColumn.getColumn().equalsIgnoreCase(entityColumn.getProperty())) {
161185
selectBuilder.append(" ").append(entityColumn.getProperty().toUpperCase()).append(",");
162186
} else {
163187
selectBuilder.append(",");
@@ -239,11 +263,10 @@ public static synchronized void initEntityNameMap(Class<?> entityClass) {
239263
if (field.isAnnotationPresent(SequenceGenerator.class)) {
240264
SequenceGenerator sequenceGenerator = field.getAnnotation(SequenceGenerator.class);
241265
if (sequenceGenerator.sequenceName().equals("")) {
242-
throw new RuntimeException(entityClass+"字段"+field.getName()+"的注解@SequenceGenerator未指定sequenceName!");
266+
throw new RuntimeException(entityClass + "字段" + field.getName() + "的注解@SequenceGenerator未指定sequenceName!");
243267
}
244268
entityColumn.setSequenceName(sequenceGenerator.sequenceName());
245-
}
246-
else if (field.isAnnotationPresent(GeneratedValue.class)) {
269+
} else if (field.isAnnotationPresent(GeneratedValue.class)) {
247270
GeneratedValue generatedValue = field.getAnnotation(GeneratedValue.class);
248271
if (generatedValue.generator().equals("UUID")) {
249272
if (field.getType().equals(String.class)) {
@@ -283,30 +306,67 @@ else if (field.isAnnotationPresent(GeneratedValue.class)) {
283306
* 将驼峰风格替换为下划线风格
284307
*/
285308
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+
}
293321
}
294-
return builder.toString();
322+
return sb.charAt(0) == '_'? sb.substring(1): sb.toString();
295323
}
296324

297325
/**
298326
* 将下划线风格替换为驼峰风格
299327
*/
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+
}
305341
}
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);
308344
}
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;
310370
}
311371

312372
/**
@@ -324,10 +384,18 @@ private static List<Field> getAllField(Class<?> entityClass, List<Field> fieldLi
324384
return fieldList;
325385
}
326386
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())) {
329397
return getAllField(entityClass.getSuperclass(), fieldList);
330398
}
331399
return fieldList;
332400
}
333-
}
401+
}

src/main/java/com/isea533/mybatis/mapperhelper/Mapper.java

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,27 @@
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+
125
package com.isea533.mybatis.mapperhelper;
226

327
import org.apache.ibatis.annotations.DeleteProvider;
@@ -8,7 +32,11 @@
832
import java.util.List;
933

1034
/**
11-
* Created by liuzh on 2014/11/19.
35+
* 通用Mapper接口,其他接口继承该接口即可
36+
* <p>项目地址 : <a href="https://github.com/abel533/Mapper" target="_blank">https://github.com/abel533/Mapper</a></p>
37+
*
38+
* @param <T> 不能为空
39+
* @author liuzh
1240
*/
1341
public interface Mapper<T> {
1442

0 commit comments

Comments
 (0)