使用Apache骆驼在mybatis中选择对象列表

时间:2018-12-03 09:56:46

标签: xml apache-camel mybatis

假设我有一个xml看起来像这样的对象列表:

<people>
    <names>
        <name>Martin</name>
        <name>Kassed</name>
        <name>Billy</name>
        <name>Mark</name>
        <name>Bob</name>
    </names>
</people>

预期输出:

<people>
    <surnames>
        <surname>Kentucky</surname>
        <surname>Snow</surname>
        <surname>Arm</surname>
        <surname>Potter</surname>
        <surname>Dawn</surname>
    </surnames>
</people>

我的意思是我想对每个名称元素使用查询。

我的问题很清楚吗?

1 个答案:

答案 0 :(得分:1)

您需要使用xpath组件将其拆分,然后汇总所有请求以重建xml。

<route id="route">
            <from  uri="direct:start"/>
            <log  message="xml input : ${body}"/>

          <split>
                <xpath>/people/names</xpath>
                <log message="split data :  ${body}"/>
                <select id="selectAllAccounts"> select name from USER </select>
                <to uri="mybatis:selectUserName"/>
                <to uri="direct:aggregateQueries"/>
          </split>
</route>

<route>
  <from uri="direct:aggregateQueries"/>
  <aggregate strategyRef="aggrTask" completionInterval="8000" >
    <correlationExpression>
      <xpath>//te:Order</xpath>
    </correlationExpression>

    <setBody"><simple><people><surnames>${body}</people></surnames></simple></setBody>  
  </aggregate>
</route>  

聚合器策略来连接字符串:

  public Exchange aggregate(Exchange oldExchange, Exchange newExchange) { 
            if (oldExchange == null) { 
                return newExchange; // first message
            } 

            String oldBody = oldExchange.getIn().getBody(String.class); 
            String newBody = newExchange.getIn().getBody(String.class); 
            oldExchange.getIn().setBody(oldBody + "+" + newBody); 
            return oldExchange; 
        }