Java Code Examples for org.apache.ibatis.mapping.BoundSql#setAdditionalParameter()

The following examples show how to use org.apache.ibatis.mapping.BoundSql#setAdditionalParameter() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: DynamicSqlSource.java    From mybaties with Apache License 2.0 6 votes vote down vote up
@Override
 public BoundSql getBoundSql(Object parameterObject) {
   //生成一个动态上下文
   DynamicContext context = new DynamicContext(configuration, parameterObject);
//这里SqlNode.apply只是将${}这种参数替换掉,并没有替换#{}这种参数
   rootSqlNode.apply(context);
//调用SqlSourceBuilder
   SqlSourceBuilder sqlSourceParser = new SqlSourceBuilder(configuration);
   Class<?> parameterType = parameterObject == null ? Object.class : parameterObject.getClass();
//SqlSourceBuilder.parse,注意这里返回的是StaticSqlSource,解析完了就把那些参数都替换成?了,也就是最基本的JDBC的SQL写法
   SqlSource sqlSource = sqlSourceParser.parse(context.getSql(), parameterType, context.getBindings());
//看似是又去递归调用SqlSource.getBoundSql,其实因为是StaticSqlSource,所以没问题,不是递归调用
   BoundSql boundSql = sqlSource.getBoundSql(parameterObject);
   for (Map.Entry<String, Object> entry : context.getBindings().entrySet()) {
     boundSql.setAdditionalParameter(entry.getKey(), entry.getValue());
   }
   return boundSql;
 }
 
Example 2
Source File: DynamicSqlSource.java    From mybatis with Apache License 2.0 6 votes vote down vote up
@Override
 public BoundSql getBoundSql(Object parameterObject) {
   //生成一个动态上下文
   DynamicContext context = new DynamicContext(configuration, parameterObject);
//这里SqlNode.apply只是将${}这种参数替换掉,并没有替换#{}这种参数
   rootSqlNode.apply(context);
//调用SqlSourceBuilder
   SqlSourceBuilder sqlSourceParser = new SqlSourceBuilder(configuration);
   Class<?> parameterType = parameterObject == null ? Object.class : parameterObject.getClass();
//SqlSourceBuilder.parse,注意这里返回的是StaticSqlSource,解析完了就把那些参数都替换成?了,也就是最基本的JDBC的SQL写法
   SqlSource sqlSource = sqlSourceParser.parse(context.getSql(), parameterType, context.getBindings());
//看似是又去递归调用SqlSource.getBoundSql,其实因为是StaticSqlSource,所以没问题,不是递归调用
   BoundSql boundSql = sqlSource.getBoundSql(parameterObject);
   for (Map.Entry<String, Object> entry : context.getBindings().entrySet()) {
     boundSql.setAdditionalParameter(entry.getKey(), entry.getValue());
   }
   return boundSql;
 }
 
Example 3
Source File: ExecutorUtil.java    From Mybatis-PageHelper with MIT License 6 votes vote down vote up
/**
 * 执行自动生成的 count 查询
 *
 * @param dialect
 * @param executor
 * @param countMs
 * @param parameter
 * @param boundSql
 * @param rowBounds
 * @param resultHandler
 * @return
 * @throws SQLException
 */
public static Long executeAutoCount(Dialect dialect, Executor executor, MappedStatement countMs,
                                    Object parameter, BoundSql boundSql,
                                    RowBounds rowBounds, ResultHandler resultHandler) throws SQLException {
    Map<String, Object> additionalParameters = getAdditionalParameter(boundSql);
    //创建 count 查询的缓存 key
    CacheKey countKey = executor.createCacheKey(countMs, parameter, RowBounds.DEFAULT, boundSql);
    //调用方言获取 count sql
    String countSql = dialect.getCountSql(countMs, boundSql, parameter, rowBounds, countKey);
    //countKey.update(countSql);
    BoundSql countBoundSql = new BoundSql(countMs.getConfiguration(), countSql, boundSql.getParameterMappings(), parameter);
    //当使用动态 SQL 时,可能会产生临时的参数,这些参数需要手动设置到新的 BoundSql 中
    for (String key : additionalParameters.keySet()) {
        countBoundSql.setAdditionalParameter(key, additionalParameters.get(key));
    }
    //执行 count 查询
    Object countResultList = executor.query(countMs, parameter, RowBounds.DEFAULT, resultHandler, countKey, countBoundSql);
    Long count = (Long) ((List) countResultList).get(0);
    return count;
}
 
Example 4
Source File: ExecutorUtil.java    From Mybatis-PageHelper with MIT License 6 votes vote down vote up
/**
 * 分页查询
 *
 * @param dialect
 * @param executor
 * @param ms
 * @param parameter
 * @param rowBounds
 * @param resultHandler
 * @param boundSql
 * @param cacheKey
 * @param <E>
 * @return
 * @throws SQLException
 */
public static  <E> List<E> pageQuery(Dialect dialect, Executor executor, MappedStatement ms, Object parameter,
                             RowBounds rowBounds, ResultHandler resultHandler,
                             BoundSql boundSql, CacheKey cacheKey) throws SQLException {
    //判断是否需要进行分页查询
    if (dialect.beforePage(ms, parameter, rowBounds)) {
        //生成分页的缓存 key
        CacheKey pageKey = cacheKey;
        //处理参数对象
        parameter = dialect.processParameterObject(ms, parameter, boundSql, pageKey);
        //调用方言获取分页 sql
        String pageSql = dialect.getPageSql(ms, boundSql, parameter, rowBounds, pageKey);
        BoundSql pageBoundSql = new BoundSql(ms.getConfiguration(), pageSql, boundSql.getParameterMappings(), parameter);

        Map<String, Object> additionalParameters = getAdditionalParameter(boundSql);
        //设置动态参数
        for (String key : additionalParameters.keySet()) {
            pageBoundSql.setAdditionalParameter(key, additionalParameters.get(key));
        }
        //执行分页查询
        return executor.query(ms, parameter, RowBounds.DEFAULT, resultHandler, pageKey, pageBoundSql);
    } else {
        //不执行分页的情况下,也不执行内存分页
        return executor.query(ms, parameter, RowBounds.DEFAULT, resultHandler, cacheKey, boundSql);
    }
}
 
Example 5
Source File: MyBatisUtils.java    From platform with Apache License 2.0 6 votes vote down vote up
/**
 * 复制BoundSql
 *
 * @param mappedStatement   ${@link MappedStatement}
 * @param boundSql          ${@link BoundSql}
 * @param sql               SQL
 * @param parameterMappings 参数映射
 * @param parameter         参数
 * @return {@link BoundSql}
 */
public static BoundSql copyFromBoundSql(MappedStatement mappedStatement,
                                        BoundSql boundSql,
                                        String sql,
                                        Object parameter) {

    List<ParameterMapping> parameterMappings = new ArrayList<>(boundSql.getParameterMappings());

    BoundSql newBoundSql = new BoundSql(mappedStatement.getConfiguration(), sql, parameterMappings, parameter);

    for (ParameterMapping mapping : boundSql.getParameterMappings()) {
        String prop = mapping.getProperty();
        if (boundSql.hasAdditionalParameter(prop)) {
            newBoundSql.setAdditionalParameter(prop, boundSql.getAdditionalParameter(prop));
        }
    }
    return newBoundSql;
}
 
Example 6
Source File: PagePluging.java    From aaden-pay with Apache License 2.0 5 votes vote down vote up
void processIntercept(final Object[] queryArgs) {
	MappedStatement ms = (MappedStatement) queryArgs[MAPPED_STATEMENT_INDEX];
	Object parameter = queryArgs[PARAMETER_INDEX];
	final RowBounds rowBounds = (RowBounds) queryArgs[ROWBOUNDS_INDEX];
	int offset = rowBounds.getOffset();
	int limit = rowBounds.getLimit();
	if (dialect.supportsLimit() && (offset != RowBounds.NO_ROW_OFFSET || limit != RowBounds.NO_ROW_LIMIT)) {
		BoundSql boundSql = ms.getBoundSql(parameter);
		String sql = boundSql.getSql().trim();
		if (dialect.supportsLimitOffset()) {
			sql = dialect.getLimitString(sql, offset, limit);
			offset = RowBounds.NO_ROW_OFFSET;
		} else {
			sql = dialect.getLimitString(sql, 0, limit);
		}
		limit = RowBounds.NO_ROW_LIMIT;
		queryArgs[ROWBOUNDS_INDEX] = new RowBounds(offset, limit);
		BoundSql newBoundSql = new BoundSql(ms.getConfiguration(), sql, boundSql.getParameterMappings(),
				boundSql.getParameterObject());
		for (ParameterMapping mapping : boundSql.getParameterMappings()) {
			String prop = mapping.getProperty();
			if (boundSql.hasAdditionalParameter(prop)) {
				newBoundSql.setAdditionalParameter(prop, boundSql.getAdditionalParameter(prop));
			}
		}
		MappedStatement newMs = copyFromMappedStatement(ms, new BoundSqlSqlSource(newBoundSql));
		queryArgs[MAPPED_STATEMENT_INDEX] = newMs;
	}
}
 
Example 7
Source File: OffsetLimitInterceptor.java    From AsuraFramework with Apache License 2.0 5 votes vote down vote up
private BoundSql copyFromBoundSql(MappedStatement ms, BoundSql boundSql,
		String sql, List<ParameterMapping> parameterMappings,Object parameter) {
	BoundSql newBoundSql = new BoundSql(ms.getConfiguration(),sql, parameterMappings, parameter);
	for (ParameterMapping mapping : boundSql.getParameterMappings()) {
	    String prop = mapping.getProperty();
	    if (boundSql.hasAdditionalParameter(prop)) {
	        newBoundSql.setAdditionalParameter(prop, boundSql.getAdditionalParameter(prop));
	    }
	}
	return newBoundSql;
}
 
Example 8
Source File: VelocitySqlSource.java    From mybaties with Apache License 2.0 5 votes vote down vote up
public BoundSql getBoundSql(Object parameterObject) {
  Map<String, Object> bindings = createBindings(parameterObject, configuration);
  VelocityContext context = new VelocityContext(bindings);
  StringWriter sw = new StringWriter();
  script.merge(context, sw);
  VelocitySqlSourceBuilder sqlSourceParser = new VelocitySqlSourceBuilder(configuration);
  Class<?> parameterType = parameterObject == null ? Object.class : parameterObject.getClass();
  SqlSource sqlSource = sqlSourceParser.parse(sw.toString(), parameterType);
  BoundSql boundSql = sqlSource.getBoundSql(parameterObject);
  for (Map.Entry<String, Object> entry : bindings.entrySet()) {
    boundSql.setAdditionalParameter(entry.getKey(), entry.getValue());
  }
  return boundSql;

}
 
Example 9
Source File: PaginationPlugin.java    From easyooo-framework with Apache License 2.0 5 votes vote down vote up
private void cpyAndAppendParameters(MappedStatement ms, Pagination pg, BoundSql boundSql, BoundSql newBoundSql){
	// cpy old parameters
	for (ParameterMapping mapping : newBoundSql.getParameterMappings()) {
		String prop = mapping.getProperty();
		if (boundSql.hasAdditionalParameter(prop)) {
			newBoundSql.setAdditionalParameter(prop,
					boundSql.getAdditionalParameter(prop));
		}
	}
	
	// append pagination parameters
	Configuration cf = ms.getConfiguration();
	TypeHandler<?> type =  cf.getTypeHandlerRegistry().getTypeHandler(Integer.class);
	ParameterMapping offsetMapping = new ParameterMapping.Builder(cf,
			OFFSET_PARAMETER, type).build();
	ParameterMapping limitMapping = new ParameterMapping.Builder(cf,
			LIMIT_PARAMETER, type).build();
	
	ParameterMapping[] mappings = new ParameterMapping[]{offsetMapping, limitMapping}; 
	for (Order order : dialect.order()) {
		newBoundSql.getParameterMappings().add(mappings[order.ordinal()]);
	}
	
	newBoundSql.setAdditionalParameter(OFFSET_PARAMETER, pg.getOffset());
	
	// 如果是Oracle,第二个参数需要设置起始位置加Limit得到结束位置
	// 与MySql是不一样
	if(DBMS.ORACLE.name().equals(dbms)){
		newBoundSql.setAdditionalParameter(LIMIT_PARAMETER, pg.getOffset() + pg.getLimit());
	}else{
		newBoundSql.setAdditionalParameter(LIMIT_PARAMETER, pg.getLimit());
	}
}
 
Example 10
Source File: VelocitySqlSource.java    From mybatis with Apache License 2.0 5 votes vote down vote up
public BoundSql getBoundSql(Object parameterObject) {
  Map<String, Object> bindings = createBindings(parameterObject, configuration);
  VelocityContext context = new VelocityContext(bindings);
  StringWriter sw = new StringWriter();
  script.merge(context, sw);
  VelocitySqlSourceBuilder sqlSourceParser = new VelocitySqlSourceBuilder(configuration);
  Class<?> parameterType = parameterObject == null ? Object.class : parameterObject.getClass();
  SqlSource sqlSource = sqlSourceParser.parse(sw.toString(), parameterType);
  BoundSql boundSql = sqlSource.getBoundSql(parameterObject);
  for (Map.Entry<String, Object> entry : bindings.entrySet()) {
    boundSql.setAdditionalParameter(entry.getKey(), entry.getValue());
  }
  return boundSql;

}