Mule-通过DB连接器为sql查询创建动态where条件 [英] Mule-Creating dynamic where condition for sql query through DB connector

查看:166
本文介绍了Mule-通过DB连接器为sql查询创建动态where条件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要创建动态查询,其中where条件将根据进入mule的请求进行更改。请求将始终使用查询参数获取。以下是示例:
http:// localhost:8084 / basePath?name = balwant& age = 26 ,或
http: // localhost:8084 / basePath?name = balwant& age = 26& gender = M

I need to create dynamic query wherein the where condition will be changed based on the request coming in to mule.The request will be always get with query parameter. Here goes the example: http://localhost:8084/basePath?name=balwant&age=26 , OR http://localhost:8084/basePath?name=balwant&age=26&gender=M

同样它也是动态的。现在我需要一个方法我可以创建一个查询,其中WHERE条件将根据请求中的查询参数添加。

Likewise it will be dynamic.Now I need a way by which I can create a query where the WHERE condition will be added based on the query parameters in request.

推荐答案

我有这个我使用Custom Transformers的想法。所以我用java变压器。

I had this in my mind of using Custom Transformers. So I used java transformer for this.

逻辑看起来像这样:

public class QueryBuilder extends AbstractMessageTransformer {

@Override
public Object transformMessage(MuleMessage message, String outputEncoding)
        throws TransformerException {

    System.out.println("Query Params : "
            + message.getInboundProperty("http.query.params").getClass()
                    .getName());

    Map<?, ?> map = message.getInboundProperty("http.query.params");

    System.out.println("Map keys : " + map.keySet());
    String where = "";
    for (Map.Entry<?, ?> entry : map.entrySet()) {
        System.out.println(entry.getKey() + "/" + entry.getValue());
        where = where+" "+entry.getKey()+"="+"'"+entry.getValue()+"'"+" and";
    }
    String whereCondition = where.substring(0, where.lastIndexOf(" "));
    System.out.println("Where condition is : "+ where.substring(0, where.lastIndexOf(" ")));
    return whereCondition;
}}

现在返回字符串类型的有效负载。

Now this returns the payload which is string type.

在数据库连接器中,选择查询类型为动态。在 WHERE 条件之后添加#[payload]

In DB connector, select Query type as Dynamic. After WHERE condition add #[payload].

干杯

这篇关于Mule-通过DB连接器为sql查询创建动态where条件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆