Java 如何使用ApacheCamel从xml中提取数据并将其持久化到DB

Java 如何使用ApacheCamel从xml中提取数据并将其持久化到DB,java,xml,jaxb,apache-camel,Java,Xml,Jaxb,Apache Camel,我是新来的阿帕奇骆驼。我有下面的XML,我使用的是restful api。我已经使用JaxB为它生成了四个对象。i、 e.使用ApacheCamel的ConsumerList.java、Consumer.java、Address.java <consumer_list> <consumer> <name>John</name> <address>

我是新来的阿帕奇骆驼。我有下面的XML,我使用的是restful api。我已经使用JaxB为它生成了四个对象。i、 e.使用ApacheCamel的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>

约翰
13 B
孟买
保罗
82 A
德里
现在我的要求是将这4个对象保存到DB。以下是我从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>

部门=7&;名称=强生公司;偏移量=0&;限额=200

使用常规的insert/update查询,使用apache camel将数据持久化到数据库中

<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>
将以下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>
例如

如果查询包含:
插入样本表值(:#样本值1)

地图应包括:

    hashMapObj.put("sample_val1","<<<YOUR_VALUE>>>");
exchange.getOut().setBody(hashMapObj);
hashMapObj.put(“示例值1”);
exchange.getOut().setBody(hashMapObj);
具有地图中设置的样本值


注意:查询应包含#对于HashMap中应替换的值

使用常规的插入/更新查询,使用apache camel将数据持久保存到数据库中

<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>
将以下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>
例如

如果查询包含:
插入样本表值(:#样本值1)

地图应包括:

    hashMapObj.put("sample_val1","<<<YOUR_VALUE>>>");
exchange.getOut().setBody(hashMapObj);
hashMapObj.put(“示例值1”);
exchange.getOut().setBody(hashMapObj);
具有地图中设置的样本值


注意:查询应该包含#对于HashMap中应该替换的值

,您可以从Camel端点和的文档开始。我已经看到了这些。我唯一的问题是我不确定应该在insert语句中输入哪些值。我的意思是xml中的地址字段具有street和city属性。jaxb创建了相同的地址类。现在,我如何获取street和city属性并将其放入insert语句中。这里有许多数据库示例,您可以首先学习:您可以从骆驼端点和的文档开始。我已经看到了这些示例。我唯一的问题是我不确定应该在insert语句中输入哪些值。我的意思是xml中的地址字段具有street和city属性。jaxb创建了相同的地址类。现在,我如何获取street和city属性并将其放入insert语句中。这里有许多数据库示例,您可以首先学习: