gpt4 book ai didi

java - 如何使用 Apache Camel 从 xml 中提取数据并将其保存到数据库

转载 作者:太空宇宙 更新时间:2023-11-04 12:51:27 32 4
gpt4 key购买 nike

我是 Apache Camel 新手。我有下面的 XML,我从 Restful api 中使用它。我已经使用 JaxB 为其生成了四个对象。即使用 apache Camel 的 ConsumerList.java、Consumer.java、Address.java。

<consumer_list>
<consumer>
<name>John</name>
<address>
<street>13 B</street>
<city>Mumbai</city>
</address>
</consumer>
<consumer>
<name>Paul</name>
<address>
<street>82 A</street>
<city>Delhi</city>
</address>
</consumer>

</consumer_list>

现在我的要求是将这4个对象保存到数据库中。以下是我来自camel-context.xml的路线:

        <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="org.postgresql.Driver"/>
<property name="url" value="jdbc:postgresql://localhost:5432/firstdb"/>
<property name="username" value="postgres"/>
<property name="password" value="xyz"/>
</bean>

<!-- configure the Camel SQL component to use the JDBC data source -->
<bean id="sql" class="org.apache.camel.component.sql.SqlComponent">
<property name="dataSource" ref="dataSource"/>
</bean>

<camelContext xmlns="http://camel.apache.org/schema/spring">
<route id="generateOrder-route">
<from uri="timer://foo?fixedRate=true&amp;period=60000"/>
<setHeader headerName="Exchange.HTTP_QUERY">
<constant>dept=7&amp;name=Johnson&amp;offset=0&amp;limit=200</constant>
</setHeader>
<to uri="http://example.com/ibp/api/v3/business_partners/search"/>
<unmarshal>
<jaxb prettyPrint="true" contextPath="target.jaxb.beans"/>
</unmarshal>
<to ???"/>
</route>
</camelContext>

我已经解码了这些对象,但我不知道如何将其插入数据库。

最佳答案

使用正常的插入/更新查询,使用 apache Camel 将数据保存到数据库。

将以下 bean 添加到您的 SpringConfig.xml 文件

<!-- JDBC data source bean-->
<bean id="dataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="oracle.jdbc.driver.OracleDriver" />
<property name="url"
value="YOUR_CONNECTION_STRING" />
<property name="username" value="YOUR_USERNAME" />
<property name="password" value="YOUR_PASSWORD" />
</bean>

<!-- configure the Camel SQL component to use the JDBC data source -->
<bean id="sql" class="org.apache.camel.component.sql.SqlComponent">
<property name="dataSource" ref="dataSource" />
</bean>

根据插入查询中提到的名称设置已解码到 HashMap 中的值,并将其设置为 MessageProcessor 类中的 exchange.getOut().setBody

然后在CamelRouter.java中使用它,将其传递给转换主体

.transform().body() 会将这些 HashMap 值设置为您的查询参数。

from("direct:yourHandleName").to("bean:messsageProcessor?method=setMessageInHashMap")
.transform().body()
.to("sql:{{----YOUR QUERY HERE----}}")
.log("INFORMATION SUCCESSFULLY INSERTED IN DB").end();

例如。

如果查询包含:插入到sample_table值中(:#sample_val1)

map 应包含:

    hashMapObj.put("sample_val1","<<<YOUR_VALUE>>>");
exchange.getOut().setBody(hashMapObj);

在 map 中设置sample_val1的值

注意:查询应包含 # 来表示应从 HashMap 替换的值

关于java - 如何使用 Apache Camel 从 xml 中提取数据并将其保存到数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35791095/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com