Skip to content

Commit dab6ad4

Browse files
authored
Merge pull request nutzam#1347 from happyday517/master
add: 增加自动生成Table/View名称功能,补充文档
2 parents 652cc33 + c153f38 commit dab6ad4

File tree

5 files changed

+43
-6
lines changed

5 files changed

+43
-6
lines changed

doc/manual/dao/entity.man

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,8 @@
4343
public class PetBean {
4444
...
4545
}}}
46-
* 将对应到数据表 "pet_bean"
46+
* 将对应到数据表 "pet_bean" (since 1.r.63)
47+
* (since 1.r.63) 你也可以通过Daos.setTableNameMaker来设置你自己的Table名称生成器,同理,setViewNameMaker设置View名称生成器。它们将在你没有指定具体表名/视图名时,根据当前class对象生成名称
4748
* @Table("t_pet") 将对应到 "t_pet"
4849

4950
描述字段

src/org/nutz/dao/entity/annotation/Table.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,6 @@
6262
@Documented
6363
public @interface Table {
6464

65-
String value();
65+
String value() default "";
6666

6767
}

src/org/nutz/dao/entity/annotation/View.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,5 +23,5 @@
2323
@Target({ElementType.TYPE})
2424
@Documented
2525
public @interface View {
26-
String value();
26+
String value() default "";
2727
}

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

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -112,13 +112,14 @@ public <T> Entity<T> make(Class<T> type) {
112112
*/
113113
String tableName = null;
114114
if (null == ti.annTable) {
115-
tableName = Strings.lowerWord(type.getSimpleName(), '_');
115+
tableName = Daos.getTableNameMaker().make(type);
116116
if (null == ti.annView)
117117
log.warnf("No @Table found, fallback to use table name='%s' for type '%s'", tableName, type.getName());
118118
} else {
119-
tableName = ti.annTable.value();
119+
tableName = ti.annTable.value().isEmpty() ? Daos.getTableNameMaker().make(type) : ti.annTable.value();
120120
}
121-
String viewName = null == ti.annView ? tableName : ti.annView.value();
121+
String viewName = null == ti.annView ? tableName :
122+
(ti.annView.value().isEmpty() ? Daos.getViewNameMaker().make(type) : ti.annView.value());
122123
en.setTableName(tableName);
123124
en.setViewName(viewName);
124125

src/org/nutz/dao/util/Daos.java

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1032,6 +1032,41 @@ public static Set<String> sql2003Keywords() {
10321032

10331033
/** varchar 字段的默认字段长度 */
10341034
public static int DEFAULT_VARCHAR_WIDTH = 128;
1035+
1036+
/** Table&View名称生成器 */
1037+
public static interface NameMaker {
1038+
String make(Class<?> klass);
1039+
}
1040+
/** 默认的Table名称生成器 */
1041+
private static NameMaker tableNameMaker = new NameMaker() {
1042+
@Override
1043+
public String make(Class<?> klass) {
1044+
return Strings.lowerWord(klass.getSimpleName(), '_');
1045+
}
1046+
};
1047+
/** 默认的View名称生成器 */
1048+
private static NameMaker viewNameMaker = new NameMaker() {
1049+
@Override
1050+
public String make(Class<?> klass) {
1051+
return Strings.lowerWord(klass.getSimpleName(), '_');
1052+
}
1053+
};
1054+
1055+
public static NameMaker getTableNameMaker() {
1056+
return tableNameMaker;
1057+
}
1058+
1059+
public static void setTableNameMaker(NameMaker tableNameMaker) {
1060+
Daos.tableNameMaker = tableNameMaker;
1061+
}
1062+
1063+
public static NameMaker getViewNameMaker() {
1064+
return viewNameMaker;
1065+
}
1066+
1067+
public static void setViewNameMaker(NameMaker viewNameMaker) {
1068+
Daos.viewNameMaker = viewNameMaker;
1069+
}
10351070
}
10361071

10371072
class ExtDaoInvocationHandler implements InvocationHandler {

0 commit comments

Comments
 (0)