use of org.apache.ibatis.mapping.ParameterMapping in project mybatis-3 by mybatis.
the class ExecutorTestHelper method prepareInsertAuthorMappedStatementWithAutoKey.
public static MappedStatement prepareInsertAuthorMappedStatementWithAutoKey(final Configuration config) {
final TypeHandlerRegistry registry = config.getTypeHandlerRegistry();
MappedStatement ms = new MappedStatement.Builder(config, "insertAuthor", new StaticSqlSource(config, "INSERT INTO author (username,password,email,bio,favourite_section) values(?,?,?,?,?)"), SqlCommandType.INSERT).parameterMap(new ParameterMap.Builder(config, "defaultParameterMap", Author.class, 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());
}
}).build()).cache(authorCache).keyGenerator(Jdbc3KeyGenerator.INSTANCE).keyProperty("id").build();
return ms;
}
use of org.apache.ibatis.mapping.ParameterMapping in project mybatis-3 by mybatis.
the class MapperBuilderAssistant method getStatementParameterMap.
private ParameterMap getStatementParameterMap(String parameterMapName, Class<?> parameterTypeClass, String statementId) {
parameterMapName = applyCurrentNamespace(parameterMapName, true);
ParameterMap parameterMap = null;
if (parameterMapName != null) {
try {
parameterMap = configuration.getParameterMap(parameterMapName);
} catch (IllegalArgumentException e) {
throw new IncompleteElementException("Could not find parameter map " + parameterMapName, e);
}
} else if (parameterTypeClass != null) {
List<ParameterMapping> parameterMappings = new ArrayList<ParameterMapping>();
parameterMap = new ParameterMap.Builder(configuration, statementId + "-Inline", parameterTypeClass, parameterMappings).build();
}
return parameterMap;
}
use of org.apache.ibatis.mapping.ParameterMapping in project mybatis-3 by mybatis.
the class DefaultParameterHandler method setParameters.
@Override
public void setParameters(PreparedStatement ps) {
ErrorContext.instance().activity("setting parameters").object(mappedStatement.getParameterMap().getId());
List<ParameterMapping> parameterMappings = boundSql.getParameterMappings();
if (parameterMappings != null) {
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 {
MetaObject metaObject = configuration.newMetaObject(parameterObject);
value = metaObject.getValue(propertyName);
}
TypeHandler typeHandler = parameterMapping.getTypeHandler();
JdbcType jdbcType = parameterMapping.getJdbcType();
if (value == null && jdbcType == null) {
jdbcType = configuration.getJdbcTypeForNull();
}
try {
typeHandler.setParameter(ps, i + 1, value, jdbcType);
} catch (TypeException e) {
throw new TypeException("Could not set parameters for mapping: " + parameterMapping + ". Cause: " + e, e);
} catch (SQLException e) {
throw new TypeException("Could not set parameters for mapping: " + parameterMapping + ". Cause: " + e, e);
}
}
}
}
}
use of org.apache.ibatis.mapping.ParameterMapping in project mybatis-3 by mybatis.
the class BaseExecutor method createCacheKey.
@Override
public CacheKey createCacheKey(MappedStatement ms, Object parameterObject, RowBounds rowBounds, BoundSql boundSql) {
if (closed) {
throw new ExecutorException("Executor was closed.");
}
CacheKey cacheKey = new CacheKey();
cacheKey.update(ms.getId());
cacheKey.update(rowBounds.getOffset());
cacheKey.update(rowBounds.getLimit());
cacheKey.update(boundSql.getSql());
List<ParameterMapping> parameterMappings = boundSql.getParameterMappings();
TypeHandlerRegistry typeHandlerRegistry = ms.getConfiguration().getTypeHandlerRegistry();
// mimic DefaultParameterHandler logic
for (ParameterMapping parameterMapping : parameterMappings) {
if (parameterMapping.getMode() != ParameterMode.OUT) {
Object value;
String propertyName = parameterMapping.getProperty();
if (boundSql.hasAdditionalParameter(propertyName)) {
value = boundSql.getAdditionalParameter(propertyName);
} else if (parameterObject == null) {
value = null;
} else if (typeHandlerRegistry.hasTypeHandler(parameterObject.getClass())) {
value = parameterObject;
} else {
MetaObject metaObject = configuration.newMetaObject(parameterObject);
value = metaObject.getValue(propertyName);
}
cacheKey.update(value);
}
}
if (configuration.getEnvironment() != null) {
// issue #176
cacheKey.update(configuration.getEnvironment().getId());
}
return cacheKey;
}
use of org.apache.ibatis.mapping.ParameterMapping in project mybatis-paginator by HuQingmiao.
the class Dialect method init.
protected void init() {
boundSql = mappedStatement.getBoundSql(parameterObject);
parameterMappings = new ArrayList(boundSql.getParameterMappings());
if (parameterObject instanceof Map) {
pageParameters.putAll((Map) parameterObject);
} else {
for (ParameterMapping parameterMapping : parameterMappings) {
pageParameters.put(parameterMapping.getProperty(), parameterObject);
}
}
StringBuffer bufferSql = new StringBuffer(boundSql.getSql().trim());
if (bufferSql.lastIndexOf(";") == bufferSql.length() - 1) {
bufferSql.deleteCharAt(bufferSql.length() - 1);
}
String sql = bufferSql.toString();
//替换制表符
pageSQL = sql.replaceAll("\t", " ").trim();
//合并空格符
pageSQL = pageSQL.replaceAll("\\s{1,}", " ");
if (pageBounds.getOrders() != null && !pageBounds.getOrders().isEmpty()) {
pageSQL = getSortString(sql, pageBounds.getOrders());
}
if (pageBounds.getOffset() != RowBounds.NO_ROW_OFFSET || pageBounds.getLimit() != RowBounds.NO_ROW_LIMIT) {
pageSQL = getLimitString(pageSQL, pageBounds.getOffset(), pageBounds.getLimit());
}
if (pageBounds.isIfCount()) {
countSQL = getCountString(sql);
}
}
Aggregations