-
Notifications
You must be signed in to change notification settings - Fork 12.9k
Expose properties in DefaultParameterHandler.java #2172
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
sample code to use these properties as below: public class BatchParameterHandler implements Interceptor {
public Object intercept(Invocation invocation) throws Throwable {
if (invocation.getTarget() instanceof DefaultParameterHandler) {
DefaultParameterHandler parameterHandler = (DefaultParameterHandler) invocation.getTarget();
MappedStatement mappedStatement = (MappedStatement) ReflectUtils.getValueByFieldName(parameterHandler, "mappedStatement");
Object paramObj = ReflectUtils.getValueByFieldName(parameterHandler, "parameterObject");
if (paramObj instanceof BatchParameter) {
PreparedStatement ps = (PreparedStatement) invocation.getArgs()[0];
BoundSql boundSql = (BoundSql) ReflectUtils.getValueByFieldName(parameterHandler, "boundSql");
@SuppressWarnings({ "unchecked", "rawtypes" })
List<Object> parameterObject = (List) ((BatchParameter) paramObj).getData();
ps.clearBatch();
ps.clearParameters();
@SuppressWarnings("rawtypes")
int batchSize = ((BatchParameter) paramObj).getBatchSize();
int i = 0;
for (Object pobject : parameterObject) {
DefaultParameterHandler handler = new DefaultParameterHandler(mappedStatement, pobject, boundSql);
handler.setParameters(ps);
ps.addBatch();
i += 1;
if (i % batchSize == 0) {
ps.executeBatch();
}
}
if (parameterObject.size() % batchSize != 0) {
ps.executeBatch();
}
return i;
}
}
return invocation.proceed();
}
public Object plugin(Object target) {
return Plugin.wrap(target, this);
}
public void setProperties(Properties properties) {
}
} |
defaultExecutorType can satisfy ? |
defaultExecutorType looks attractive, while, how can I use BATCH only for specified SQL IDs and others use SIMPLE? |
in different sqlSession? |
Hi bitgaoshu, happy Chinese New Year to you! |
So, even my problem is resolved by adding aonther ExecutorType, I think, Expose properties in DefaultParameterHandler.java can bring more flexiblity to developers, why not do it? |
Hello @johnny2002 ! I'm sorry about the belated response. Regarding your request, there have been similar requests before, but we have no plan to expose those internal variables (they are intentionally hidden from the public API).
I'm pretty sure MyBatis uses |
In order to extend mybatis to support batch sql by Preparedstatement, we need to add an plugin, thus, in the pulgin, we need to get "mappedStatement", "parameterObject","boundSql" from DefaultParameterHandler.java. While, these 3 properties are all private, so we have to use reflect.
Suggest to add getters for these 3 properties.
Full path:
/src/main/java/org/apache/ibatis/scripting/defaults/DefaultParameterHandler.java
The text was updated successfully, but these errors were encountered: