Skip to content

Commit

Permalink
optimize DefaultParameterHandler creating MeteObject multiple times w…
Browse files Browse the repository at this point in the history
…hen obtaining properties of the same parameter object
  • Loading branch information
yffstart committed Sep 1, 2023
1 parent ead5a1d commit 3421298
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ public void setParameters(PreparedStatement ps) {
ErrorContext.instance().activity("setting parameters").object(mappedStatement.getParameterMap().getId());
List<ParameterMapping> parameterMappings = boundSql.getParameterMappings();
if (parameterMappings != null) {
MetaObject metaObject = null;
for (int i = 0; i < parameterMappings.size(); i++) {
ParameterMapping parameterMapping = parameterMappings.get(i);
if (parameterMapping.getMode() != ParameterMode.OUT) {
Expand All @@ -75,7 +76,9 @@ public void setParameters(PreparedStatement ps) {
} else if (typeHandlerRegistry.hasTypeHandler(parameterObject.getClass())) {
value = parameterObject;
} else {
MetaObject metaObject = configuration.newMetaObject(parameterObject);
if (metaObject == null) {
metaObject = configuration.newMetaObject(parameterObject);
}
value = metaObject.getValue(propertyName);
}
TypeHandler typeHandler = parameterMapping.getTypeHandler();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
import static org.mockito.Mockito.doThrow;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.times;

import java.sql.PreparedStatement;
import java.sql.SQLException;
Expand All @@ -29,6 +31,8 @@
import java.util.List;

import org.apache.ibatis.builder.StaticSqlSource;
import org.apache.ibatis.domain.blog.Author;
import org.apache.ibatis.domain.blog.Section;
import org.apache.ibatis.mapping.BoundSql;
import org.apache.ibatis.mapping.MappedStatement;
import org.apache.ibatis.mapping.ParameterMapping;
Expand Down Expand Up @@ -98,4 +102,35 @@ MappedStatement getMappedStatement() {
}).build();
}

@Test
void testParameterObjectMetaObjectGetValue() {
Configuration config = new Configuration();
TypeHandlerRegistry registry = config.getTypeHandlerRegistry();

MappedStatement mappedStatement = new MappedStatement.Builder(config, "testSelect", new StaticSqlSource(config, "some select statement"), SqlCommandType.SELECT).build();

Author parameterObject = mock(Author.class);

BoundSql boundSql = new BoundSql(config, "some select statement", new ArrayList<ParameterMapping>() {
{
add(new ParameterMapping.Builder(config, "username", registry.getTypeHandler(String.class)).build());
add(new ParameterMapping.Builder(config, "password", registry.getTypeHandler(String.class)).build());
add(new ParameterMapping.Builder(config, "email", registry.getTypeHandler(String.class)).build());
add(new ParameterMapping.Builder(config, "bio", registry.getTypeHandler(String.class)).jdbcType(JdbcType.VARCHAR).build());
add(new ParameterMapping.Builder(config, "favouriteSection", registry.getTypeHandler(Section.class)).jdbcType(JdbcType.VARCHAR).build());
}
}, parameterObject);

DefaultParameterHandler defaultParameterHandler = new DefaultParameterHandler(mappedStatement, parameterObject, boundSql);

PreparedStatement ps = mock(PreparedStatement.class);

defaultParameterHandler.setParameters(ps);

verify(parameterObject, times(1)).getUsername();
verify(parameterObject, times(1)).getPassword();
verify(parameterObject, times(1)).getEmail();
verify(parameterObject, times(1)).getBio();
verify(parameterObject, times(1)).getFavouriteSection();
}
}

0 comments on commit 3421298

Please sign in to comment.