Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit eead399

Browse files
committed
更新到最新版的通用mapper-0.3.0
1 parent cf2e752 commit eead399

File tree

6 files changed

+322
-289
lines changed

6 files changed

+322
-289
lines changed

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

+75-57
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,46 @@ of this software and associated documentation files (the "Software"), to deal
3737
*/
3838
public class EntityHelper {
3939

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+
*/
4080
public static class EntityColumn {
4181
private String property;
4282
private String column;
@@ -113,9 +153,9 @@ public void setGenerator(String generator) {
113153
}
114154

115155
/**
116-
* 实体类 => 表名
156+
* 实体类 => 表对象
117157
*/
118-
private static final Map<Class<?>, String> entityClassTableName = new HashMap<Class<?>, String>();
158+
private static final Map<Class<?>, EntityTable> entityTableMap = new HashMap<Class<?>, EntityTable>();
119159

120160
/**
121161
* 实体类 => 全部列属性
@@ -128,21 +168,21 @@ public void setGenerator(String generator) {
128168
private static final Map<Class<?>, List<EntityColumn>> entityClassPKColumns = new HashMap<Class<?>, List<EntityColumn>>();
129169

130170
/**
131-
* 获取表名
171+
* 获取表对象
132172
*
133173
* @param entityClass
134174
* @return
135175
*/
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) {
139179
initEntityNameMap(entityClass);
140-
tableName = entityClassTableName.get(entityClass);
180+
entityTable = entityTableMap.get(entityClass);
141181
}
142-
if (tableName == null) {
182+
if (entityTable == null) {
143183
throw new RuntimeException("无法获取实体类" + entityClass.getCanonicalName() + "对应的表名!");
144184
}
145-
return tableName;
185+
return entityTable;
146186
}
147187

148188
/**
@@ -153,7 +193,7 @@ public static String getTableName(Class<?> entityClass) {
153193
*/
154194
public static List<EntityColumn> getColumns(Class<?> entityClass) {
155195
//可以起到初始化的作用
156-
getTableName(entityClass);
196+
getEntityTable(entityClass);
157197
return entityClassColumns.get(entityClass);
158198
}
159199

@@ -165,7 +205,7 @@ public static List<EntityColumn> getColumns(Class<?> entityClass) {
165205
*/
166206
public static List<EntityColumn> getPKColumns(Class<?> entityClass) {
167207
//可以起到初始化的作用
168-
getTableName(entityClass);
208+
getEntityTable(entityClass);
169209
return entityClassPKColumns.get(entityClass);
170210
}
171211

@@ -226,16 +266,23 @@ public static String getPrimaryKeyWhere(Class<?> entityClass) {
226266
* @param entityClass
227267
*/
228268
public static synchronized void initEntityNameMap(Class<?> entityClass) {
229-
if (entityClassTableName.get(entityClass) != null) {
269+
if (entityTableMap.get(entityClass) != null) {
230270
return;
231271
}
232272
//表名
273+
EntityTable entityTable = null;
233274
if (entityClass.isAnnotationPresent(Table.class)) {
234275
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();
238284
}
285+
entityTableMap.put(entityClass, entityTable);
239286
//列
240287
List<Field> fieldList = getAllField(entityClass, null);
241288
List<EntityColumn> columnList = new ArrayList<EntityColumn>();
@@ -302,6 +349,13 @@ public static synchronized void initEntityNameMap(Class<?> entityClass) {
302349
entityClassPKColumns.put(entityClass, pkColumnList);
303350
}
304351

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+
305359
/**
306360
* 将驼峰风格替换为下划线风格
307361
*/
@@ -313,62 +367,26 @@ public static String camelhumpToUnderline(String str) {
313367
char c;
314368
for (int i = 0; i < size; i++) {
315369
c = chars[i];
316-
if (isLowercaseAlpha(c)) {
317-
sb.append(toUpperAscii(c));
318-
} else {
370+
if (isUppercaseAlpha(c)) {
319371
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;
336372
} else {
337-
c = (lastUnderscore && count != 0) ? toUpperAscii(c) : toLowerAscii(c);
338-
buffer[count++] = c;
339-
lastUnderscore = false;
373+
sb.append(toUpperAscii(c));
340374
}
341375
}
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();
352377
}
353378

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');
356381
}
357382

358383
public static char toUpperAscii(char c) {
359-
if (isLowercaseAlpha(c)) {
384+
if (isUppercaseAlpha(c)) {
360385
c -= (char) 0x20;
361386
}
362387
return c;
363388
}
364389

365-
public static char toLowerAscii(char c) {
366-
if ((c >= 'A') && (c <= 'Z')) {
367-
c += (char) 0x20;
368-
}
369-
return c;
370-
}
371-
372390
/**
373391
* 获取全部的Field
374392
*

0 commit comments

Comments
 (0)