325.12. 使用带有动态值的 IN 查询

可从 Camel 2.17 开始

从采用 SQL 生产者的 Camel 2.17,通过 IN 语句使用 SQL 查询,其中 IN 值是动态计算的。例如,邮件正文或标题等。

使用 IN (CA)需要:

  • 为参数名称加上前缀 
  • 在 参数中添加 ()

解释这个示例可以更好地说明。使用以下查询:

-- this is a comment
select *
from projects
where project in (:#in:names)
order by id

在以下路由中:

from("direct:query")
    .to("sql:classpath:sql/selectProjectsIn.sql")
    .to("log:query")
    .to("mock:query");

然后 IN 查询可以使用带有动态值的密钥名称的标头,例如:

// use an array
template.requestBodyAndHeader("direct:query", "Hi there!", "names", new String[]{"Camel", "AMQ"});

// use a list
List<String> names = new ArrayList<String>();
names.add("Camel");
names.add("AMQ");

template.requestBodyAndHeader("direct:query", "Hi there!", "names", names);

// use a string separated values with comma
template.requestBodyAndHeader("direct:query", "Hi there!", "names", "Camel,AMQ");

查询也可以在端点中指定,而不是外部化(注意,外部大小可使维护 SQL 查询更为简单)

from("direct:query")
    .to("sql:select * from projects where project in (:#in:names) order by id")
    .to("log:query")
    .to("mock:query");