use of org.apache.ibatis.type.TypeHandler in project mybatis-3 by mybatis.
the class XMLMapperBuilder method parameterMapElement.
private void parameterMapElement(List<XNode> list) throws Exception {
for (XNode parameterMapNode : list) {
String id = parameterMapNode.getStringAttribute("id");
String type = parameterMapNode.getStringAttribute("type");
Class<?> parameterClass = resolveClass(type);
List<XNode> parameterNodes = parameterMapNode.evalNodes("parameter");
List<ParameterMapping> parameterMappings = new ArrayList<ParameterMapping>();
for (XNode parameterNode : parameterNodes) {
String property = parameterNode.getStringAttribute("property");
String javaType = parameterNode.getStringAttribute("javaType");
String jdbcType = parameterNode.getStringAttribute("jdbcType");
String resultMap = parameterNode.getStringAttribute("resultMap");
String mode = parameterNode.getStringAttribute("mode");
String typeHandler = parameterNode.getStringAttribute("typeHandler");
Integer numericScale = parameterNode.getIntAttribute("numericScale");
ParameterMode modeEnum = resolveParameterMode(mode);
Class<?> javaTypeClass = resolveClass(javaType);
JdbcType jdbcTypeEnum = resolveJdbcType(jdbcType);
@SuppressWarnings("unchecked") Class<? extends TypeHandler<?>> typeHandlerClass = (Class<? extends TypeHandler<?>>) resolveClass(typeHandler);
ParameterMapping parameterMapping = builderAssistant.buildParameterMapping(parameterClass, property, javaTypeClass, jdbcTypeEnum, resultMap, modeEnum, typeHandlerClass, numericScale);
parameterMappings.add(parameterMapping);
}
builderAssistant.addParameterMap(id, parameterClass, parameterMappings);
}
}
use of org.apache.ibatis.type.TypeHandler in project mybatis-3 by mybatis.
the class DefaultResultSetHandler method createRowKeyForMappedProperties.
private void createRowKeyForMappedProperties(ResultMap resultMap, ResultSetWrapper rsw, CacheKey cacheKey, List<ResultMapping> resultMappings, String columnPrefix) throws SQLException {
for (ResultMapping resultMapping : resultMappings) {
if (resultMapping.getNestedResultMapId() != null && resultMapping.getResultSet() == null) {
// Issue #392
final ResultMap nestedResultMap = configuration.getResultMap(resultMapping.getNestedResultMapId());
createRowKeyForMappedProperties(nestedResultMap, rsw, cacheKey, nestedResultMap.getConstructorResultMappings(), prependPrefix(resultMapping.getColumnPrefix(), columnPrefix));
} else if (resultMapping.getNestedQueryId() == null) {
final String column = prependPrefix(resultMapping.getColumn(), columnPrefix);
final TypeHandler<?> th = resultMapping.getTypeHandler();
List<String> mappedColumnNames = rsw.getMappedColumnNames(resultMap, columnPrefix);
// Issue #114
if (column != null && mappedColumnNames.contains(column.toUpperCase(Locale.ENGLISH))) {
final Object value = th.getResult(rsw.getResultSet(), column);
if (value != null || configuration.isReturnInstanceForEmptyRow()) {
cacheKey.update(column);
cacheKey.update(value);
}
}
}
}
}
use of org.apache.ibatis.type.TypeHandler in project mybatis-3 by mybatis.
the class DefaultResultSetHandlerTest method shouldThrowExceptionWithColumnName.
@Test
public void shouldThrowExceptionWithColumnName() throws Exception {
final MappedStatement ms = getMappedStatement();
final RowBounds rowBounds = new RowBounds(0, 100);
final DefaultResultSetHandler defaultResultSetHandler = new DefaultResultSetHandler(null, /*executor*/
ms, null, /*parameterHandler*/
null, /*resultHandler*/
null, /*boundSql*/
rowBounds);
final ResultSetWrapper rsw = mock(ResultSetWrapper.class);
when(rsw.getResultSet()).thenReturn(mock(ResultSet.class));
final ResultMapping resultMapping = mock(ResultMapping.class);
final TypeHandler typeHandler = mock(TypeHandler.class);
when(resultMapping.getColumn()).thenReturn("column");
when(resultMapping.getTypeHandler()).thenReturn(typeHandler);
when(typeHandler.getResult(any(ResultSet.class), any(String.class))).thenThrow(new SQLException("exception"));
List<ResultMapping> constructorMappings = Collections.singletonList(resultMapping);
try {
defaultResultSetHandler.createParameterizedResultObject(rsw, null, /*resultType*/
constructorMappings, null, /*constructorArgTypes*/
null, /*constructorArgs*/
null);
Assert.fail("Should have thrown ExecutorException");
} catch (Exception e) {
Assert.assertTrue("Expected ExecutorException", e instanceof ExecutorException);
Assert.assertTrue("", e.getMessage().contains("mapping: " + resultMapping.toString()));
}
}
use of org.apache.ibatis.type.TypeHandler in project mybatis-paginator by HuQingmiao.
the class DefaultParameterHandler method setParameters.
public void setParameters(PreparedStatement ps) throws SQLException {
ErrorContext.instance().activity("setting parameters").object(mappedStatement.getParameterMap().getId());
List<ParameterMapping> parameterMappings = boundSql.getParameterMappings();
if (parameterMappings != null) {
MetaObject metaObject = parameterObject == null ? null : configuration.newMetaObject(parameterObject);
for (int i = 0; i < parameterMappings.size(); i++) {
ParameterMapping parameterMapping = parameterMappings.get(i);
if (parameterMapping.getMode() != ParameterMode.OUT) {
Object value;
String propertyName = parameterMapping.getProperty();
if (boundSql.hasAdditionalParameter(propertyName)) {
// issue #448 ask first for additional params
value = boundSql.getAdditionalParameter(propertyName);
} else if (parameterObject == null) {
value = null;
} else if (typeHandlerRegistry.hasTypeHandler(parameterObject.getClass())) {
value = parameterObject;
} else {
value = metaObject == null ? null : metaObject.getValue(propertyName);
}
TypeHandler typeHandler = parameterMapping.getTypeHandler();
if (typeHandler == null) {
throw new ExecutorException("There was no TypeHandler found for parameter " + propertyName + " of statement " + mappedStatement.getId());
}
JdbcType jdbcType = parameterMapping.getJdbcType();
if (value == null && jdbcType == null)
jdbcType = configuration.getJdbcTypeForNull();
typeHandler.setParameter(ps, i + 1, value, jdbcType);
}
}
}
}
Aggregations