Retrieve Data From Database Based On Conditions In MuleSoft

This article is the second part of the last article Fetch MySQL Data Using Database Connector in MuleSoft

Getting Started

  • First, open Anypoint Studio and Create a New Mule Project.
     
  • Enter the project name and selection location and click finish.
     
  • Drag and Drop Listener (HTTP) control from Mule palette to Message Flow, enter a display name, select Connector configuration, and Enter path with a query string value. In my sample, I am using {userId}.
    Retrieve data from database based on conditions in MuleSoft
  • Next, drag and drop Database > Select control from Mule palette to Message follow and setup Display Name, Connector configuration, Write SQL query text and Input parameters.
    Retrieve data from database based on conditions in MuleSoft
  • Next, drag and drop Logger from the Core palette and set a Generic message as Json Payload.
    Retrieve data from database based on conditions in MuleSoft
  • Lastly, Drag and Drop Set Payload from the Core palette and setup the Payload value in Json format.
    Retrieve data from database based on conditions in MuleSoft
    Retrieve data from database based on conditions in MuleSoft
  • That’s it. Now run the application.
    Retrieve data from database based on conditions in MuleSoft
  • Open Postman and enter Endpoint to see the output result.
    Retrieve data from database based on conditions in MuleSoft

Validate If No Data?

  • What is the best way to deal if no data exists for a given ID like this?
    Retrieve data from database based on conditions in MuleSoft
  • Drag and drop the Choice (Core) component from the Mule palette to massage flow before the Set payload.
    Retrieve data from database based on conditions in MuleSoft
  • Give Choice a name and set When expression “!isEmpty(payload)”.
  • Also, drag and drop the Set payload component inside of When expression and give a name to the payload.
    Retrieve data from database based on conditions in MuleSoft
  • Next, drag and drop Set Payload inside Default and set the default message.

    Retrieve data from database based on conditions in MuleSoft
  • Let’s run the application to see the result.
    Retrieve data from database based on conditions in MuleSoft

Conclusion

In this article, we learned how to setup a database connector to get MySQL database based on conditions and display results in Postman.


Similar Articles