如何为jdbc inbound-channel-adapter设置默认参数值?

时间:2018-08-20 13:29:06

标签: java spring jdbc spring-integration

我有带有选择参数化查询的jdbc inbound-channel-adapter。 如何设置默认参数值?

2 个答案:

答案 0 :(得分:0)

我相信您可以使用类似ExpressionEvaluatingSqlParameterSourceFactory.createParameterSourceNoCache(null)的内容:

/**
 * Create an expression evaluating {@link SqlParameterSource} that does not cache it's results. Useful for cases
 * where the source is used multiple times, for example in a {@code <int-jdbc:inbound-channel-adapter/>} for the
 * {@code select-sql-parameter-source} attribute.
 * @param input The root object for the evaluation.
 * @return The parameter source.
 */
public SqlParameterSource createParameterSourceNoCache(final Object input) {

没有类似“默认参数值”的内容,但是您可以使用SpEL的Elvis运算符来模拟特定的参数名称。请参阅上述工厂的public void setParameterExpressions(Map<String, String> parameterExpressions) {

答案 1 :(得分:0)

使用创建类解决它:

public class SqlParameterTransfer extends AbstractSqlParameterSource {

public void setValue(String key) {
    synchronized (lock) {
        this.key = key;
    }
}

@Override
public String getValue(String paramName) throws IllegalArgumentException {
    String value = null;
    if (KEY_PARAM_NAME.equals(paramName)) {
        value = key;
    }
    return value;
}

@Override
public boolean hasValue(String paramName) {
    return KEY_PARAM_NAME.equals(paramName);
}

private static final String KEY_PARAM_NAME = "key";
private Object lock = new Object();
private String key = "default_value";

}

然后以这种方式(通过Bean)在inbound-channel-adapter中使用它:

<int-jdbc:inbound-channel-adapter  
    ...
    select-sql-parameter-source="sqlParameterTransfer">
    ...
</int-jdbc:inbound-channel-adapter>