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 | 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 ExecutorTestHelper method prepareSelectTwoSetsOfAuthorsProc.
static MappedStatement prepareSelectTwoSetsOfAuthorsProc(final Configuration config) {
final TypeHandlerRegistry registry = config.getTypeHandlerRegistry();
return new MappedStatement.Builder(config, "selectTwoSetsOfAuthors", new StaticSqlSource(config, "{call selectTwoSetsOfAuthors(?,?)}"), SqlCommandType.SELECT).statementType(StatementType.CALLABLE).parameterMap(new ParameterMap.Builder(config, "defaultParameterMap", Author.class, new ArrayList<ParameterMapping>() {
{
add(new ParameterMapping.Builder(config, "id1", registry.getTypeHandler(int.class)).build());
add(new ParameterMapping.Builder(config, "id2", registry.getTypeHandler(int.class)).build());
}
}).build()).resultMaps(new ArrayList<ResultMap>() {
{
ResultMap map = new ResultMap.Builder(config, "defaultResultMap", Author.class, new ArrayList<ResultMapping>() {
{
add(new ResultMapping.Builder(config, "id", "id", registry.getTypeHandler(int.class)).build());
add(new ResultMapping.Builder(config, "username", "username", registry.getTypeHandler(String.class)).build());
add(new ResultMapping.Builder(config, "password", "password", registry.getTypeHandler(String.class)).build());
add(new ResultMapping.Builder(config, "email", "email", registry.getTypeHandler(String.class)).build());
add(new ResultMapping.Builder(config, "bio", "bio", registry.getTypeHandler(String.class)).build());
}
}).build();
add(map);
add(map);
}
}).build();
}
use of org.apache.ibatis.mapping.ParameterMapping in project mybatis.flying by limeng32.
the class AutoMapperInterceptor method intercept.
@Override
public Object intercept(Invocation invocation) throws Throwable {
StatementHandler statementHandler = (StatementHandler) invocation.getTarget();
MetaObject metaStatementHandler = getRealObj(statementHandler);
String originalSql = (String) metaStatementHandler.getValue(DELEGATE_BOUNDSQL_SQL);
Configuration configuration = (Configuration) metaStatementHandler.getValue(DELEGATE_CONFIGURATION);
Object parameterObject = metaStatementHandler.getValue(DELEGATE_BOUNDSQL_PARAMETEROBJECT);
MappedStatement mappedStatement = (MappedStatement) metaStatementHandler.getValue(DELEGATE_MAPPEDSTATEMENT);
FlyingModel flyingModel = FlyingManager.fetchFlyingFeatureNew(originalSql, configuration, mappedStatement);
if (flyingModel.isHasFlyingFeature()) {
boolean needHandleLimiterAndSorter = false;
String newSql = null;
switch(flyingModel.getActionType()) {
case COUNT:
newSql = SqlBuilder.buildCountSql(parameterObject, flyingModel);
break;
case DELETE:
newSql = SqlBuilder.buildDeleteSql(parameterObject);
break;
case INSERT:
newSql = SqlBuilder.buildInsertSql(parameterObject, flyingModel);
break;
case INSERT_BATCH:
newSql = SqlBuilder.buildInsertBatchSql(parameterObject, flyingModel);
break;
case SELECT:
newSql = SqlBuilder.buildSelectSql(mappedStatement.getResultMaps().get(0).getType(), flyingModel);
break;
case SELECT_ALL:
newSql = SqlBuilder.buildSelectAllSql(parameterObject, flyingModel);
needHandleLimiterAndSorter = true;
break;
case SELECT_ONE:
newSql = SqlBuilder.buildSelectOneSql(parameterObject, flyingModel);
needHandleLimiterAndSorter = true;
break;
case UPDATE:
newSql = SqlBuilder.buildUpdateSql(parameterObject, flyingModel);
break;
case UPDATE_BATCH:
newSql = SqlBuilder.buildUpdateBatchSql(parameterObject, flyingModel);
break;
case UPDATE_PERSISTENT:
newSql = SqlBuilder.buildUpdatePersistentSql(parameterObject, flyingModel);
break;
default:
break;
}
if (!LogLevel.NONE.equals(logLevel)) {
log(logger, logLevel, new StringBuilder("Auto generated sql: ").append(newSql).toString());
}
SqlSource sqlSource = buildSqlSource(configuration, newSql, parameterObject.getClass());
List<ParameterMapping> parameterMappings = sqlSource.getBoundSql(parameterObject).getParameterMappings();
String sqlToexecute = sqlSource.getBoundSql(parameterObject).getSql();
metaStatementHandler.setValue(DELEGATE_BOUNDSQL_SQL, sqlToexecute);
metaStatementHandler.setValue(DELEGATE_BOUNDSQL_PARAMETERMAPPINGS, parameterMappings);
/* Start dealing with paging */
if (needHandleLimiterAndSorter && (parameterObject instanceof Conditionable) && (invocation.getTarget() instanceof RoutingStatementHandler)) {
Conditionable condition = (Conditionable) parameterObject;
if (condition.getLimiter() != null) {
BoundSql boundSql = statementHandler.getBoundSql();
// The total count needs to be identified when encountering "selectAll"
if (ActionType.SELECT_ALL.equals(flyingModel.getActionType())) {
Connection connection = (Connection) invocation.getArgs()[0];
String countSql = new StringBuilder("select count(0) from (").append(sqlToexecute).append(") myCount").toString();
PreparedStatement countStmt = connection.prepareStatement(countSql);
BoundSql countBS = new BoundSql(mappedStatement.getConfiguration(), countSql, boundSql.getParameterMappings(), parameterObject);
setParameters(countStmt, mappedStatement, countBS, parameterObject);
ResultSet rs = countStmt.executeQuery();
try {
int count = 0;
if (rs.next()) {
count = rs.getInt(1);
}
condition.getLimiter().setTotalCount(count);
} finally {
rs.close();
countStmt.close();
}
}
sqlToexecute = generatePageSql(sqlToexecute, condition);
metaStatementHandler.setValue(DELEGATE_BOUNDSQL_SQL, sqlToexecute);
} else if (condition.getSorter() != null) {
BoundSql boundSql = statementHandler.getBoundSql();
String sql = boundSql.getSql();
sqlToexecute = generatePageSql(sql, condition);
metaStatementHandler.setValue(DELEGATE_BOUNDSQL_SQL, sqlToexecute);
}
}
if (loggerDescriptionHandler != null) {
LogLevel loggerLevel = loggerDescriptionHandler.getLogLevel(flyingModel.getId());
if (!LogLevel.NONE.equals(loggerLevel)) {
BoundSql boundSqlTemp = statementHandler.getBoundSql();
String sqlTemp = boundSqlTemp.getSql();
StringBuilder stringBuilder = new StringBuilder();
stringBuilder.append("Method: ").append(flyingModel.getId()).append("\r\n");
stringBuilder.append("Bound sql: ").append(sqlTemp).append("\r\n");
if (ActionType.SELECT.equals(flyingModel.getActionType())) {
stringBuilder.append("Bound value: {").append(parameterMappings.get(0).getProperty()).append("=").append(parameterObject).append("};");
} else {
MetaObject metaObject = parameterObject == null ? null : configuration.newMetaObject(parameterObject);
stringBuilder.append("Bound value: {");
boolean b = true;
for (ParameterMapping parameterMapping : parameterMappings) {
if (parameterMapping.getMode() != ParameterMode.OUT) {
Object value;
String propertyName = parameterMapping.getProperty();
value = metaObject == null ? null : metaObject.getValue(propertyName);
if (b) {
b = false;
} else {
stringBuilder.append(", ");
}
stringBuilder.append(propertyName).append("=").append(value);
}
}
stringBuilder.append("};");
}
log(logger, loggerLevel, stringBuilder.toString());
}
}
} else if (loggerDescriptionHandler != null) {
LogLevel loggerLevel = loggerDescriptionHandler.getLogLevel(mappedStatement.getId());
if (!LogLevel.NONE.equals(loggerLevel)) {
String sqlTemp = statementHandler.getBoundSql().getSql();
StringBuilder stringBuilder = new StringBuilder();
stringBuilder.append("Method: ").append(mappedStatement.getId()).append("\r\n");
stringBuilder.append("Bound sql: ").append(sqlTemp).append("\r\n");
stringBuilder.append("Bound value: ").append(parameterObject).append(";");
log(logger, loggerLevel, stringBuilder.toString());
}
}
/* Pass to the next interceptor. */
return invocation.proceed();
}
use of org.apache.ibatis.mapping.ParameterMapping in project mybatis-3 by mybatis.
the class ExecutorTestHelper method prepareInsertAuthorMappedStatement.
public static MappedStatement prepareInsertAuthorMappedStatement(final Configuration config) {
final TypeHandlerRegistry registry = config.getTypeHandlerRegistry();
MappedStatement ms = new MappedStatement.Builder(config, "insertAuthor", new StaticSqlSource(config, "INSERT INTO author (id,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, "id", registry.getTypeHandler(int.class)).build());
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).build();
return ms;
}
use of org.apache.ibatis.mapping.ParameterMapping in project qiuyj-code by qiuyuanjun.
the class SqlProvider method primaryKeyResolver.
/**
* 生成以主键作为条件的sql的返回值
*/
private ReturnValueWrapper primaryKeyResolver(PropertyColumnMapping primaryKey, Configuration config, String sql) {
List<ParameterMapping> parameterMappings = new ArrayList<>(1);
parameterMappings.add(new ParameterMapping.Builder(config, primaryKey.getJavaClassPropertyName(), primaryKey.getTypeHandler()).build());
return new ReturnValueWrapper(new StaticTextSqlNode(sql), parameterMappings);
}
Aggregations