Skip to content

Commit 902267d

Browse files
committed
调整代码顺序
1 parent 92eb5d0 commit 902267d

File tree

1 file changed

+31
-9
lines changed

1 file changed

+31
-9
lines changed

src/main/java/tk/mybatis/mapper/mapperhelper/MapperTemplate.java

Lines changed: 31 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,12 @@ public static String getMethodName(String msId) {
102102
return msId.substring(msId.lastIndexOf(".") + 1);
103103
}
104104

105+
/**
106+
* 该方法仅仅用来初始化ProviderSqlSource
107+
*
108+
* @param record
109+
* @return
110+
*/
105111
public String dynamicSQL(Object record) {
106112
return "dynamicSQL";
107113
}
@@ -128,6 +134,10 @@ public boolean getBEFORE() {
128134
return mapperHelper.getConfig().getBEFORE();
129135
}
130136

137+
public boolean isNotEmpty() {
138+
return mapperHelper.getConfig().isNotEmpty();
139+
}
140+
131141
/**
132142
* 是否支持该通用方法
133143
*
@@ -172,15 +182,15 @@ protected void setSqlSource(MappedStatement ms, SqlSource sqlSource) {
172182
}
173183

174184
/**
175-
* check ms cache
185+
* 检查是否配置过缓存
176186
*
177187
* @param ms
178188
* @throws Exception
179189
*/
180190
private void checkCache(MappedStatement ms) throws Exception {
181191
if (ms.getCache() == null) {
182192
String nameSpace = ms.getId().substring(0, ms.getId().lastIndexOf("."));
183-
Cache cache = null;
193+
Cache cache;
184194
try {
185195
//不存在的时候会抛出异常
186196
cache = ms.getConfiguration().getCache(nameSpace);
@@ -207,13 +217,18 @@ public void setSqlSource(MappedStatement ms) throws Exception {
207217
}
208218
Method method = methodMap.get(getMethodName(ms));
209219
try {
220+
//第一种,直接操作ms,不需要返回值
210221
if (method.getReturnType() == Void.TYPE) {
211222
method.invoke(this, ms);
212-
} else if (SqlNode.class.isAssignableFrom(method.getReturnType())) {
223+
}
224+
//第二种,返回SqlNode
225+
else if (SqlNode.class.isAssignableFrom(method.getReturnType())) {
213226
SqlNode sqlNode = (SqlNode) method.invoke(this, ms);
214227
DynamicSqlSource dynamicSqlSource = new DynamicSqlSource(ms.getConfiguration(), sqlNode);
215228
setSqlSource(ms, dynamicSqlSource);
216-
} else if (String.class.equals(method.getReturnType())) {
229+
}
230+
//第三种,返回xml形式的sql字符串
231+
else if (String.class.equals(method.getReturnType())) {
217232
String xmlSql = (String) method.invoke(this, ms);
218233
SqlSource sqlSource = createSqlSource(ms, xmlSql);
219234
//替换原有的SqlSource
@@ -230,6 +245,17 @@ public void setSqlSource(MappedStatement ms) throws Exception {
230245
}
231246
}
232247

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+
233259
/**
234260
* 获取返回值类型 - 实体类型
235261
*
@@ -392,7 +418,7 @@ protected SqlNode getAllIfColumnNode(Class<?> entityClass) {
392418
boolean first = true;
393419
//对所有列循环,生成<if test="property!=null">column = #{property}</if>
394420
for (EntityColumn column : columnList) {
395-
ifNodes.add(getIfNotNull(column, getColumnEqualsProperty(column, first), mapperHelper.getConfig().isNotEmpty()));
421+
ifNodes.add(getIfNotNull(column, getColumnEqualsProperty(column, first), isNotEmpty()));
396422
first = false;
397423
}
398424
return new MixedSqlNode(ifNodes);
@@ -548,8 +574,4 @@ public WhereSqlNode updateByExampleWhereClause(Configuration configuration) {
548574
WhereSqlNode whereSqlNode = new WhereSqlNode(configuration, forEachSqlNode);
549575
return whereSqlNode;
550576
}
551-
552-
public SqlSource createSqlSource(MappedStatement ms, String xmlSql) {
553-
return languageDriver.createSqlSource(ms.getConfiguration(), "<script>\n\t" + xmlSql + "</script>", null);
554-
}
555577
}

0 commit comments

Comments
 (0)