@@ -46,7 +46,7 @@ public abstract class AbstractEntityManagerAdapter implements EntityManager {
46
46
/**
47
47
* 属性名和field
48
48
*/
49
- protected Map <String , Field > fieldMap ;
49
+ protected Map <String , Field > propertyFieldMap ;
50
50
/**
51
51
* 列名与属性名映射
52
52
*/
@@ -58,7 +58,7 @@ public abstract class AbstractEntityManagerAdapter implements EntityManager {
58
58
protected String className ;
59
59
protected Class <?> clazz ;
60
60
protected String simpleClassName ;
61
- protected DialectReader dialectReader ;
61
+ protected DialectAdapter dialectAdapter ;
62
62
protected int tableBucketCount ;
63
63
protected DatabaseSplitStrategy databaseSplitStrategy ;
64
64
protected String insert ;
@@ -73,17 +73,17 @@ private List<Field> extractFields(Class<?> clazz) {
73
73
java .lang .reflect .Field [] fields = ClassUtility .getOrderedFields (fieldList );
74
74
Map <String , Field > fieldMap = new LinkedHashMap <>();
75
75
76
- for (java .lang .reflect .Field field : fields ) {
77
- if (!field .isAnnotationPresent (Column .class ) && !field .isAnnotationPresent (SplitTable .class )) {
76
+ for (java .lang .reflect .Field property : fields ) {
77
+ if (!property .isAnnotationPresent (Column .class ) && !property .isAnnotationPresent (SplitTable .class )) {
78
78
continue ;
79
79
}
80
- Column column = field .getAnnotation (Column .class );
81
- SplitTable splitTable = field .getAnnotation (SplitTable .class );
82
- GeneratedValue generatedValue = field .getAnnotation (GeneratedValue .class );
83
- Id id = field .getAnnotation (Id .class );
80
+ Column column = property .getAnnotation (Column .class );
81
+ SplitTable splitTable = property .getAnnotation (SplitTable .class );
82
+ GeneratedValue generatedValue = property .getAnnotation (GeneratedValue .class );
83
+ Id id = property .getAnnotation (Id .class );
84
84
85
- Field ormField = new Field (field .getName (), field .getType (), column , splitTable , generatedValue , id );
86
- fieldMap .put (field .getName (), ormField );
85
+ Field ormField = new Field (property .getName (), property .getType (), column , splitTable , generatedValue , id );
86
+ fieldMap .put (property .getName (), ormField );
87
87
}
88
88
89
89
for (Method method : orderedMethods ) {
@@ -104,45 +104,52 @@ private List<Field> extractFields(Class<?> clazz) {
104
104
return new ArrayList <>(fieldMap .values ());
105
105
}
106
106
107
+ private void appendFields (StringBuilder fieldBuilder , String fieldNameOfDialect ) {
108
+ if (fieldBuilder .length () > 0 ) {
109
+ fieldBuilder .append ("," );
110
+ }
111
+ fieldBuilder .append (fieldNameOfDialect );
112
+ }
113
+
107
114
public AbstractEntityManagerAdapter (Class <?> clazz ) {
108
115
this .clazz = clazz ;
109
116
this .className = clazz .getName ();
110
117
this .simpleClassName = clazz .getSimpleName ();
111
118
List <Field > fields = this .extractFields (clazz );
112
119
int fieldCount = fields .size ();
113
120
uniqueFieldMap = new LinkedHashMap <>();
114
- columnPropertyMap = new LinkedHashMap <String , String >(fieldCount );
121
+ this .columnPropertyMap = new LinkedHashMap <>(fieldCount );
122
+ // 初始化字段列表
123
+ this .propertyFieldMap = new LinkedHashMap <>(fieldCount );
115
124
hashFieldMap = new TreeMap <>();
116
125
126
+ StringBuilder fieldBuilder = new StringBuilder ();
117
127
StringBuilder insertSQL = new StringBuilder ("insert into " );
118
128
StringBuilder insertParameter = new StringBuilder ();
119
129
StringBuilder updateSQL = new StringBuilder ("update " );
120
130
StringBuilder createDDLField = new StringBuilder ();
121
131
initTable ();
122
-
123
132
updateSQL .append (this .dialectTableName );
124
133
insertSQL .append (this .dialectTableName );
125
-
126
134
String createDDLHeader = String .format ("DROP TABLE IF EXISTS %1$s;\n CREATE TABLE %1$s (\n " , this .dialectTableName );
127
135
String primaryCreateDDL = "" ;
128
136
insertSQL .append ("(" );
129
137
updateSQL .append (" set " );
130
138
for (Field field : fields ) {
131
- String propertyName = field .getName ();
139
+ String propertyName = field .getPropertyName ();
132
140
SplitTable splitTable = field .getSplitTable ();
133
141
if (splitTable != null ) {
134
142
this .hashFieldMap .put (splitTable .index (), field );
135
143
if (!field .isPersistence ()) {
136
144
continue ;
137
145
}
138
146
}
139
-
140
147
Column column = field .getColumn ();
141
148
if (column == null ) {
142
149
continue ;
143
150
}
144
151
if (field .isUnique ()) {
145
- uniqueFieldMap .put (field .getName (), field );
152
+ uniqueFieldMap .put (field .getPropertyName (), field );
146
153
}
147
154
if ("status" .equalsIgnoreCase (column .name ())) {
148
155
this .status = field ;
@@ -159,17 +166,20 @@ public AbstractEntityManagerAdapter(Class<?> clazz) {
159
166
}
160
167
161
168
this .columnPropertyMap .put (column .name (), propertyName );
162
- String fieldName = dialectReader .getOpenQuote () + column .name ()
163
- + dialectReader .getCloseQuote ();
169
+ this .propertyFieldMap .put (field .getPropertyName (), field );
170
+ String fieldNameOfDialect = dialectAdapter .getOpenQuote () + column .name ()
171
+ + dialectAdapter .getCloseQuote ();
172
+ this .appendFields (fieldBuilder , fieldNameOfDialect );
173
+
164
174
// insertSQL
165
175
if (!TableSplitStrategy .ORIGIN_NOT_PERSISTENCE .equals (field .getHashStrategy ()) && !GenerationType .IDENTITY .equals (field .getGenerationType ())) {
166
176
insertSQL .append ("\n " );
167
- insertSQL .append (fieldName );
177
+ insertSQL .append (fieldNameOfDialect );
168
178
insertSQL .append (Symbol .COMMA );
169
179
170
180
insertParameter .append ("\n " );
171
181
insertParameter .append (this .parsePropertyParameter (column .name (), propertyName ));
172
- insertParameter .append ("," );
182
+ insertParameter .append (Symbol . COMMA );
173
183
}
174
184
175
185
// updateSQL
@@ -180,7 +190,7 @@ public AbstractEntityManagerAdapter(Class<?> clazz) {
180
190
181
191
if (column .updatable ()) {
182
192
updateSQL .append ("\n " );
183
- updateSQL .append (fieldName ).append (Symbol .EQUAL );
193
+ updateSQL .append (fieldNameOfDialect ).append (Symbol .EQUAL );
184
194
updateSQL .append (this .parsePropertyParameter (column .name (), propertyName ));
185
195
updateSQL .append ("," );
186
196
}
@@ -194,31 +204,19 @@ public AbstractEntityManagerAdapter(Class<?> clazz) {
194
204
updateSQL .deleteCharAt (updateSQL .length () - 1 )
195
205
.append (" where " )
196
206
.append (this .primary .getColumnName ())
197
- .append ("=" ).append (this .parsePropertyParameter (this .primary .getColumnName (), this .primary .getName ()));
207
+ .append ("=" ).append (this .parsePropertyParameter (this .primary .getColumnName (), this .primary .getPropertyName ()));
198
208
String deleteSQL = "delete from " + this .dialectTableName + " where "
199
- + this .primary .getColumnName () + "=" + this .parsePropertyParameter (this .primary .getColumnName (), this .primary .getName ());
209
+ + this .primary .getColumnName () + "=" + this .parsePropertyParameter (this .primary .getColumnName (), this .primary .getPropertyName ());
200
210
201
211
createDDLField .append (String .format ("PRIMARY KEY (`%s`)\n " , this .primary .getColumnName ()));
202
212
createDDLField .append (") ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='" ).append (tableName ).append ("';\n " );
203
213
204
- this .createDDL = createDDLHeader + primaryCreateDDL + createDDLField . toString () ;
214
+ this .createDDL = createDDLHeader + primaryCreateDDL + createDDLField ;
205
215
this .insert = insertSQL .toString ();
206
216
// 初始化delete SQL语句
207
217
this .delete = deleteSQL ;
208
218
// 初始化update SQL语句
209
219
this .update = updateSQL .toString ();
210
- // 初始化字段列表
211
- this .fieldMap = new LinkedHashMap <String , Field >(fieldCount );
212
- StringBuilder fieldBuilder = new StringBuilder ();
213
- for (Field field : fields ) {
214
- if (fieldBuilder .length () > 0 ) {
215
- fieldBuilder .append (",\n " );
216
- }
217
- if (field .isPersistence ()) {
218
- fieldBuilder .append (dialectReader .getOpenQuote ()).append (field .getColumnName ()).append (dialectReader .getCloseQuote ());
219
- }
220
- this .fieldMap .put (field .getName (), field );
221
- }
222
220
this .fields = fieldBuilder .toString ();
223
221
}
224
222
@@ -237,18 +235,18 @@ public void initTable() {
237
235
if (dialect != null ) {
238
236
dbDialect = dialect .strategy ();
239
237
}
240
- this .dialectReader = new DialectReader (dbDialect );
238
+ this .dialectAdapter = new DialectAdapter (dbDialect );
241
239
Split split = null ;
242
240
if (clazz .isAnnotationPresent (Split .class )) {
243
241
split = clazz .getAnnotation (Split .class );
244
242
}
245
243
246
244
if (split == null ) {
247
245
//`table-name`
248
- this .dialectTableName = String .format ("%s%s%s" , dialectReader .getOpenQuote (), tableName , dialectReader .getCloseQuote ());
246
+ this .dialectTableName = String .format ("%s%s%s" , dialectAdapter .getOpenQuote (), tableName , dialectAdapter .getCloseQuote ());
249
247
return ;
250
248
}
251
- this .dialectTableName = String .format ("%s%s%s%s" , dialectReader .getOpenQuote (), tableName , Constant .TABLE_SUFFIX , dialectReader .getCloseQuote ());
249
+ this .dialectTableName = String .format ("%s%s%s%s" , dialectAdapter .getOpenQuote (), tableName , Constant .TABLE_SUFFIX , dialectAdapter .getCloseQuote ());
252
250
// 分表的桶数
253
251
int bucketCount ;
254
252
if (split .table_bucket_count () > 1 ) {
@@ -284,8 +282,8 @@ public String getDialectTableName() {
284
282
}
285
283
286
284
@ Override
287
- public DialectReader getDialect () {
288
- return dialectReader ;
285
+ public DialectAdapter getDialect () {
286
+ return dialectAdapter ;
289
287
}
290
288
291
289
@ Override
@@ -309,8 +307,8 @@ public String getFields() {
309
307
}
310
308
311
309
@ Override
312
- public Map <String , Field > getFieldMap () {
313
- return fieldMap ;
310
+ public Map <String , Field > getPropertyFieldMap () {
311
+ return propertyFieldMap ;
314
312
}
315
313
316
314
@ Override
@@ -329,7 +327,7 @@ public String getProperty(String columnName) {
329
327
330
328
@ Override
331
329
public Field getField (String property ) {
332
- return fieldMap .get (property );
330
+ return propertyFieldMap .get (property );
333
331
}
334
332
335
333
@ Override
0 commit comments