2. In this tutorial we are going to fetch the records
from Mysql Table by using MySql Database
connector with select operation
Pre-requisites :
1.Anypoint Studio 5.2.0
2.Mule Server 3.7.0/3.7.1
3.Mysql Database v6.x
4.Database Table script
3. MySql Table Script :
CREATE DATABASE IF NOT EXISTS `muleesb`;
USE `muleesb`;
DROP TABLE IF EXISTS `ESB_version`;
CREATE TABLE `ESB_version` (
`server_id` int(11) NOT NULL AUTO_INCREMENT,
`server_version` varchar(45) DEFAULT NULL,
`release_date` datetime DEFAULT NULL,
PRIMARY KEY (`server_id`),
KEY `index1` (`server_id`) USING BTREE
) ENGINE=InnoDB AUTO_INCREMENT=5 DEFAULT CHARSET=latin1;
LOCK TABLES `ESB_version` WRITE;
INSERT INTO `ESB_version` VALUES (1,'3.7.0','2015-07-07 00:00:00'),(2,'3.7.0','2015-
07-07 00:00:00'),(3,'3.7.0','2015-07-07 00:00:00'),(4,'3.7.0','2015-07-07 00:00:00');
UNLOCK TABLES;
4. Mule Flow :
In the above flow i am fetching the table data from database connector as
ArrayList and this is list is converted to JSON string in the last transformer and
the JSON will be returned to the calling http request .
5. Mule Source xml :
<http:listener-config name="HTTP_Listener_Configuration" host="0.0.0.0"
port="8081" doc:name="HTTP Listener Configuration"/>
<db:mysql-config name="MySQL_Configuration" host="127.0.0.1" port="3306"
user="anil" password="anil" database="muleesb" doc:name="MySQL
Configuration"/>
<flow name="fetchmysqldataFlow">
<http:listener config-ref="HTTP_Listener_Configuration" path="/"
doc:name="HTTP"/>
<db:select config-ref="MySQL_Configuration" doc:name="Database">
<db:parameterized-query><![CDATA[select * from
muleesb.ESB_version]]></db:parameterized-query>
</db:select>
<logger message="no of records fetched : #[payload.size()]" level="INFO"
doc:name="Logger"/>
<json:object-to-json-transformer mimeType="application/json"
doc:name="Object to JSON"/></flow>