java 在表中插入一行的 Apache Camel 示例

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10777825/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 02:32:00  来源:igfitidea点击:

Apache Camel example to insert a row in a table

javajdbcapache-camel

提问by Himanshu Yadav

I want to insert exchange.body to a database table for one of the condition of my route.

我想将 exchange.body 插入到我的路线条件之一的数据库表中。

  • Is there any example/tutorial of camel-jdbc component to insert message body?
  • Can I import the SQL statement itself and pass exchange.body to it?
  • 是否有插入消息正文的camel-jdbc组件的示例/教程?
  • 我可以导入 SQL 语句本身并将 exchange.body 传递给它吗?

I looked at http://camel.apache.org/jdbc.htmlexample, but could not understand it.

我查看了http://camel.apache.org/jdbc.html示例,但无法理解。

Here Spring example is confusing for me. I didn't get why is it setting the body as SQL query and again importing some query from the class path. (There is no insert query example mentioned here.)

这里的 Spring 示例让我感到困惑。我不明白为什么它将主体设置为 SQL 查询并再次从类路径导入一些查询。(这里没有提到插入查询示例。)

采纳答案by Petter Nordlander

You probably need to do some restructure of your payload before inserting it anyway, so there should probably be no issue to do a transformation using whatever method in Camel to set the body to the appropriate INSERT statement.

在插入之前,您可能需要对有效负载进行一些重组,因此使用 Camel 中的任何方法进行转换以将主体设置为适当的 INSERT 语句应该没有问题。

The important thing is what kind of payload structure your incoming message have. In the basic case - it's a string - it should be fairly simple

重要的是您的传入消息具有什么样的负载结构。在基本情况下 - 它是一个字符串 - 它应该相当简单

// In a Java bean/processor before the JDBC endpoint.
// Update: make sure to sanitize the payload from SQL injections if it contains user inputs or external data not generated by trusted sources.
exchange.getIn().setBody("INSERT INTO MYTABLE VALUES('" + exchange.getIn().getBody(String.class) + "', 'fixedValue', 1.0, 42)");

In case your message contains complex data structures, this code will of course be more complex, but it's pretty much the same way regular application will generate SQL queries.

如果您的消息包含复杂的数据结构,此代码当然会更复杂,但它与常规应用程序生成 SQL 查询的方式几乎相同。

The classpath example you are refering to

您所指的类路径示例

 <jdbc:embedded-database id="testdb" type="DERBY">
        <jdbc:script location="classpath:sql/init.sql"/>
 </jdbc:embedded-database>

Simply shows how to test the JDBC component by starting a Database server embedded (Apache Derby) and populate it with some initial data (the sql/init.sql file). This part is not really part of the core jdbc component, but simply in the documentation to get up and running a sample without needing to configure a DB server and setup the JDBC connection properties.

简单地展示了如何通过启动嵌入式数据库服务器 (Apache Derby) 并使用一些初始数据(sql/init.sql 文件)填充它来测试 JDBC 组件。这部分实际上并不是核心 jdbc 组件的一部分,而只是在文档中用于启动和运行示例,而无需配置 DB 服务器和设置 JDBC 连接属性。

That said, you might want to use the SQL component for more complex scenarios.

也就是说,您可能希望将 SQL 组件用于更复杂的场景。

回答by Henryk Konsek

If you want to insert using the same statement(changing the parameters only) - use SQL component.

如果要使用相同的语句插入(仅更改参数) - 使用SQL 组件

If you want to insert using arbitrary SQL statementinto the component - use JDBC component.

如果您想使用任意 SQL 语句插入组件 - 使用JDBC 组件

SQL component usage:

SQL组件用法:

from("direct:start").to("sql:insert into table foo (c1, c1) values ('#','#')");

com.google.common.collect.Lists;
producerTemplate.sendBody("direct:start", Lists.newArrayList("value1","value2"));

JDBC component usage:

JDBC组件使用:

from("direct:start").to("jdbc:dataSource");

producerTemplate.sendBody("direct:start", "insert into table foo (c1, c1) values ('value1','value2')");