【问题标题】:How to set the default parameter value for jdbc inbound-channel-adapter?如何设置 jdbc inbound-channel-adapter 的默认参数值?
【发布时间】:2018-08-20 13:29:06
【问题描述】:

我有带有选择参数化查询的 jdbc 入站通道适配器。 如何设置默认参数值?

【问题讨论】:

  • 你需要显示配置。
  • 感谢您的回复。发布我的解决方案。

标签: java spring jdbc spring-integration


【解决方案1】:

我相信你可以使用类似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&lt;String, String&gt; parameterExpressions) {

【讨论】:

  • 感谢您的回复。发布我的解决方案。
【解决方案2】:

用创建类解决:

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>  

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多