将 JSON 插入到 Mule 中数据库的多个表中

Insert JSON into multiple tables on Database in Mule

我正在尝试使用 Mule ESB 将 JSON 的内容插入到 MySql 数据库中。 JSON 看起来像:

{
 "id":106636,
 "client_id":9999,
 "comments":"Credit",
 "salesman_name":"Salvador Dali", 
 "cart_items":[
              {"citem_id":1066819,"quantity":3}, 
              {"citem_id":1066820,"quantity":10}
            ]
}

在 mule 上,我想在一个步骤中插入所有数据,例如:

目前我已经在 Mule 上走了这么远: MuleSoft Flow

好的。因为,您已经在流程中将 JSON 转换为对象,您可以使用它们的对象引用来引用单个值,例如 obj.id、obj.client_id 等

  1. 接下来获取数据库连接器。
  2. 在 "Connector Configuration" 中配置您的 MySQL 数据库。
  3. 操作:选择"Bulk execute"
  4. In "Query text" :编写多个 INSERT 查询并从对象(从 JSON 转换而来)传递适当的值。请记住在查询文本中用分号 (;) 分隔多个查询。

就是这样!!如果您遇到任何问题,请告诉我。希望对你有用..

这里有一篇优秀的文章http://www.dotnetfunda.com/articles/show/2078/parse-json-keys-to-insert-records-into-postgresql-database-using-mule 那应该有帮助。您可能需要修改,因为您需要先写入 order_header 数据,然后为 order_detail 使用集合拆分器并将整个包装在事务中。

使用数据库连接器的批量执行操作。 您将插入到多个表中。 例如:

查询文字

Insert INTO order_header(payload.id,payload.client_id,payload.comments,payload.salesman_name);
Insert INTO order_detail(payload.id,payload.cart_items[0].citem_id,payload.cart_items[0].quantity); etc..