Skip to content

Commit 6b2ede7

Browse files
committed
update: 通过map创建表结构新增方法:支持设置customtype、表注释、字段注释
1 parent 17c2dbc commit 6b2ede7

File tree

1 file changed

+32
-15
lines changed

1 file changed

+32
-15
lines changed

src/org/nutz/dao/impl/entity/MapEntityMaker.java

Lines changed: 32 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,5 @@
11
package org.nutz.dao.impl.entity;
22

3-
import java.sql.Connection;
4-
import java.sql.SQLException;
5-
import java.util.Map;
6-
import java.util.Map.Entry;
7-
8-
import javax.sql.DataSource;
9-
103
import org.nutz.dao.entity.Entity;
114
import org.nutz.dao.entity.annotation.ColType;
125
import org.nutz.dao.impl.EntityHolder;
@@ -21,6 +14,12 @@
2114
import org.nutz.log.Log;
2215
import org.nutz.log.Logs;
2316

17+
import javax.sql.DataSource;
18+
import java.sql.Connection;
19+
import java.sql.SQLException;
20+
import java.util.Map;
21+
import java.util.Map.Entry;
22+
2423
public class MapEntityMaker {
2524

2625
private static final Log log = Logs.get();
@@ -31,9 +30,18 @@ public class MapEntityMaker {
3130

3231
@SuppressWarnings({"unchecked", "rawtypes"})
3332
public <T extends Map<String, ?>> Entity<T> make(String tableName, T map) {
33+
return this.make(tableName, map, false, false, null);
34+
}
35+
36+
public <T extends Map<String, ?>> Entity<T> make(String tableName, T map, boolean hasColumnComment, boolean hasTableComment, String tableComment) {
3437
final NutEntity<T> en = new NutEntity(map.getClass());
3538
en.setTableName(tableName);
3639
en.setViewName(tableName);
40+
en.setHasColumnComment(hasColumnComment);
41+
en.setHasTableComment(hasTableComment);
42+
if (tableComment != null) {
43+
en.setTableComment(tableComment);
44+
}
3745
boolean check = false;
3846
for (Entry<String, ?> entry : map.entrySet()) {
3947
String key = entry.getKey();
@@ -51,7 +59,7 @@ else if (key.startsWith(".")) {
5159
Object value = entry.getValue();
5260
Mirror<?> mirror = Mirror.me(value);
5361
NutMappingField ef = new NutMappingField(en);
54-
62+
ef.setHasColumnComment(hasColumnComment);
5563
while (true) {
5664
if (key.startsWith("+")) {
5765
ef.setAsAutoIncreasement();
@@ -113,13 +121,24 @@ else if (key.startsWith(".")) {
113121
ef.setWidth((Integer) w);
114122
}
115123
}
124+
125+
// 字段备注
126+
if (map.containsKey("." + key + ".comment")) {
127+
ef.setColumnComment((String) map.get("." + key + ".comment"));
128+
}
129+
130+
// 自定义字段类型
131+
if (map.containsKey("." + key + ".customtype")) {
132+
ef.setCustomDbType((String) map.get("." + key + ".customtype"));
133+
}
134+
116135
ef.setInjecting(new InjectToMap(key)); // 这里比较纠结,回设的时候应该用什么呢?
117136
ef.setEjecting(new EjectFromMap(entry.getKey()));
118137

119138
if (ef.isAutoIncreasement()
120-
&& ef.isId()
121-
&& expert.isSupportAutoIncrement()
122-
&& !expert.isSupportGeneratedKeys()) {
139+
&& ef.isId()
140+
&& expert.isSupportAutoIncrement()
141+
&& !expert.isSupportGeneratedKeys()) {
123142
en.addAfterInsertMacro(expert.fetchPojoId(en, ef));
124143
}
125144

@@ -137,13 +156,11 @@ else if (key.startsWith(".")) {
137156
try {
138157
conn = dataSource.getConnection();
139158
expert.setupEntityField(conn, en);
140-
}
141-
finally {
159+
} finally {
142160
if (conn != null)
143161
conn.close();
144162
}
145-
}
146-
catch (SQLException e) {
163+
} catch (SQLException e) {
147164
log.debug(e.getMessage(), e);
148165
}
149166
}

0 commit comments

Comments
 (0)