Skip to content

Commit c8eea20

Browse files
committed
替换为可以回写的KeyGenerator,扩展方法可以返回String类型。
1 parent 0187fae commit c8eea20

File tree

1 file changed

+12
-2
lines changed

1 file changed

+12
-2
lines changed

src/main/java/com/github/abel533/mapperhelper/MapperTemplate.java

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -112,14 +112,19 @@ protected void setResultType(MappedStatement ms, Class<?> entityClass) {
112112
}
113113

114114
/**
115-
* 重新设置SqlSource
115+
* 重新设置SqlSource,同时判断如果是Jdbc3KeyGenerator,就设置为MultipleJdbc3KeyGenerator
116116
*
117117
* @param ms
118118
* @param sqlSource
119119
*/
120120
protected void setSqlSource(MappedStatement ms, SqlSource sqlSource) {
121121
MetaObject msObject = SystemMetaObject.forObject(ms);
122122
msObject.setValue("sqlSource", sqlSource);
123+
//如果是Jdbc3KeyGenerator,就设置为MultipleJdbc3KeyGenerator
124+
KeyGenerator keyGenerator = ms.getKeyGenerator();
125+
if(keyGenerator instanceof Jdbc3KeyGenerator){
126+
msObject.setValue("keyGenerator", new MultipleJdbc3KeyGenerator());
127+
}
123128
}
124129

125130
/**
@@ -150,8 +155,13 @@ public void setSqlSource(MappedStatement ms) throws Exception {
150155
SqlNode sqlNode = (SqlNode) method.invoke(this, ms);
151156
DynamicSqlSource dynamicSqlSource = new DynamicSqlSource(ms.getConfiguration(), sqlNode);
152157
setSqlSource(ms, dynamicSqlSource);
158+
} else if (String.class.equals(method.getReturnType())) {
159+
String xmlSql = (String) method.invoke(this, ms);
160+
SqlSource sqlSource = createSqlSource(ms, xmlSql);
161+
//替换原有的SqlSource
162+
setSqlSource(ms, sqlSource);
153163
} else {
154-
throw new RuntimeException("自定义Mapper方法返回类型错误,可选的返回类型为void和SqlNode!");
164+
throw new RuntimeException("自定义Mapper方法返回类型错误,可选的返回类型为void,SqlNode,String三种!");
155165
}
156166
} catch (IllegalAccessException e) {
157167
throw new RuntimeException(e);

0 commit comments

Comments
 (0)