@@ -102,6 +102,12 @@ public static String getMethodName(String msId) {
102
102
return msId .substring (msId .lastIndexOf ("." ) + 1 );
103
103
}
104
104
105
+ /**
106
+ * 该方法仅仅用来初始化ProviderSqlSource
107
+ *
108
+ * @param record
109
+ * @return
110
+ */
105
111
public String dynamicSQL (Object record ) {
106
112
return "dynamicSQL" ;
107
113
}
@@ -128,6 +134,10 @@ public boolean getBEFORE() {
128
134
return mapperHelper .getConfig ().getBEFORE ();
129
135
}
130
136
137
+ public boolean isNotEmpty () {
138
+ return mapperHelper .getConfig ().isNotEmpty ();
139
+ }
140
+
131
141
/**
132
142
* 是否支持该通用方法
133
143
*
@@ -172,15 +182,15 @@ protected void setSqlSource(MappedStatement ms, SqlSource sqlSource) {
172
182
}
173
183
174
184
/**
175
- * check ms cache
185
+ * 检查是否配置过缓存
176
186
*
177
187
* @param ms
178
188
* @throws Exception
179
189
*/
180
190
private void checkCache (MappedStatement ms ) throws Exception {
181
191
if (ms .getCache () == null ) {
182
192
String nameSpace = ms .getId ().substring (0 , ms .getId ().lastIndexOf ("." ));
183
- Cache cache = null ;
193
+ Cache cache ;
184
194
try {
185
195
//不存在的时候会抛出异常
186
196
cache = ms .getConfiguration ().getCache (nameSpace );
@@ -207,13 +217,18 @@ public void setSqlSource(MappedStatement ms) throws Exception {
207
217
}
208
218
Method method = methodMap .get (getMethodName (ms ));
209
219
try {
220
+ //第一种,直接操作ms,不需要返回值
210
221
if (method .getReturnType () == Void .TYPE ) {
211
222
method .invoke (this , ms );
212
- } else if (SqlNode .class .isAssignableFrom (method .getReturnType ())) {
223
+ }
224
+ //第二种,返回SqlNode
225
+ else if (SqlNode .class .isAssignableFrom (method .getReturnType ())) {
213
226
SqlNode sqlNode = (SqlNode ) method .invoke (this , ms );
214
227
DynamicSqlSource dynamicSqlSource = new DynamicSqlSource (ms .getConfiguration (), sqlNode );
215
228
setSqlSource (ms , dynamicSqlSource );
216
- } else if (String .class .equals (method .getReturnType ())) {
229
+ }
230
+ //第三种,返回xml形式的sql字符串
231
+ else if (String .class .equals (method .getReturnType ())) {
217
232
String xmlSql = (String ) method .invoke (this , ms );
218
233
SqlSource sqlSource = createSqlSource (ms , xmlSql );
219
234
//替换原有的SqlSource
@@ -230,6 +245,17 @@ public void setSqlSource(MappedStatement ms) throws Exception {
230
245
}
231
246
}
232
247
248
+ /**
249
+ * 通过xmlSql创建sqlSource
250
+ *
251
+ * @param ms
252
+ * @param xmlSql
253
+ * @return
254
+ */
255
+ public SqlSource createSqlSource (MappedStatement ms , String xmlSql ) {
256
+ return languageDriver .createSqlSource (ms .getConfiguration (), "<script>\n \t " + xmlSql + "</script>" , null );
257
+ }
258
+
233
259
/**
234
260
* 获取返回值类型 - 实体类型
235
261
*
@@ -392,7 +418,7 @@ protected SqlNode getAllIfColumnNode(Class<?> entityClass) {
392
418
boolean first = true ;
393
419
//对所有列循环,生成<if test="property!=null">column = #{property}</if>
394
420
for (EntityColumn column : columnList ) {
395
- ifNodes .add (getIfNotNull (column , getColumnEqualsProperty (column , first ), mapperHelper . getConfig (). isNotEmpty ()));
421
+ ifNodes .add (getIfNotNull (column , getColumnEqualsProperty (column , first ), isNotEmpty ()));
396
422
first = false ;
397
423
}
398
424
return new MixedSqlNode (ifNodes );
@@ -548,8 +574,4 @@ public WhereSqlNode updateByExampleWhereClause(Configuration configuration) {
548
574
WhereSqlNode whereSqlNode = new WhereSqlNode (configuration , forEachSqlNode );
549
575
return whereSqlNode ;
550
576
}
551
-
552
- public SqlSource createSqlSource (MappedStatement ms , String xmlSql ) {
553
- return languageDriver .createSqlSource (ms .getConfiguration (), "<script>\n \t " + xmlSql + "</script>" , null );
554
- }
555
577
}
0 commit comments