Search Tutorials

Spring Boot + Apache Camel JDBC component + MySQL - Hello World Example | JavaInUse

Spring Boot + Apache Camel JDBC component + MySQL - Hello World Example

In previous tutorial we had implemented Apache Camel + Spring SQL Component Example for inserting and retrieving records from MySQL.  In that example we had made use of Spring XML configuration. In this tutorial we will be making use of Spring Boot and Apache Camel JDBC Component for inserting and retrieving records from MySQL.
The JDBC component enables us to access databases through JDBC, where SQL queries (SELECT) and operations (INSERT, UPDATE, etc) are sent in the message body. This component uses the standard JDBC API, unlike the SQL Component, which uses spring-jdbc.

The application we will be developing is as follows -
Apache Camel Spring Boot JDBC Component Maven


This tutorial is explained in the below Youtube Video.

Apache Camel - Table of Contents

File Transfer Using Java DSL Apache Camel Apache Camel Java DSL + Spring Integration Hello World Example Apache Camel Exception Handling Using Simple Example Apache Camel Redelivery policy using example Integrate Apache Camel and ActiveMQ EIP patterns using Apache Camel Apache Camel Tutorial- Integrate Spring Boot+ Apache Camel Apache Camel Tutorial- Integrate with MySQL DB using SQL query Apache Camel EIP - Splitter and Aggregator pattern Apache Camel Unit Testing Apache Camel + Spring + Quartz Hello World Example Camel application deployment on JBoss Fuse Apache Camel + Apache CXF SOAP Webservices Apache Camel + JAX-RS REST Webservice Apache Camel + CXFRS REST Webservice Apache Camel Routing Slip EIP Pattern Apache Camel Dynamic Router Pattern Apache Camel Load Balancer EIP Pattern Apache Camel Interceptors Apache Camel + Kafka Hello World Example Apache Camel - Marshalling/Unmarshalling XML/JSON Data Example Calling and Consuming Webservices using Apache Camel Apache Camel Tutorial - Send SMTP Email Using Gmail Apache Camel Tutorial - SEDA component Hello World Example Apache Camel Tutorial - Idempotent Consumer using MemoryIdempotentRepository and FileIdempotentRepository Spring Boot + Apache Camel + RabbitMQ - Hello World Example Spring Boot + Apache Camel JDBC component + MySQL - Hello World Example Spring Boot + Apache Camel SQL component + MySQL - Hello World Example Spring Boot + Apache Camel SQL component + Transaction Management Example
    The Maven Project we will be developing is as follows-  
Apache Camel Spring Boot JDBC Component Maven
  The pom.xml is as follows. We make use of the camel-jdbc dependency.
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="" xmlns:xsi=""


	<description>Demo project for Spring Boot</description>

		<relativePath /> <!-- lookup parent from repository -->




In a previous tutorial we had seen the advantages of make using Spring Boot JDBC.  One advantage is that we do not need to create any datasource bean. It get automatically created for us. In the file specify the datasource properties
Create the schema-mysql.sql file and specify the initialization scripts. Spring Boot JDBC will automatically run this initialization scripts on startup.

CREATE TABLE employee (
  empName VARCHAR(100) NOT NULL

Create the Employee Domain class as follows-
package com.javainuse.model;

public class Employee {

	private String empId;
	private String empName;

	public String getEmpId() {
		return empId;

	public void setEmpId(String empId) {
		this.empId = empId;

	public String getEmpName() {
		return empName;

	public void setEmpName(String empName) {
		this.empName = empName;

	public String toString() {
		return "Employee [empId=" + empId + ", empName=" + empName + "]";


Create the Service class where will be configuring the camel route. Also in the service class we autowire the datasource bean create for us by Spring Boot JDBC.
We will be configuring 2 routes -
  • Insert Route -  Using this route we will be inserting the employee details in the MySQL database. As we are making use of camel jdbc we will need to create the insert query and set it in the body. This is done using the Camel Processor.
  • Select Route - Using this route we retrieve the employee details from the MySQL database. We then return the list of employees.

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.sql.DataSource;

import org.apache.camel.Exchange;
import org.apache.camel.Processor;
import org.apache.camel.builder.RouteBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.javainuse.model.Employee;

public class EmployeeServiceImpl extends RouteBuilder {

	DataSource dataSource;

	public DataSource getDataSource() {
		return dataSource;

	public void setDataSource(DataSource dataSource) {
		this.dataSource = dataSource;

	public void configure() throws Exception {
	       	 //Insert Route
		from("direct:insert").process(new Processor() {
			public void process(Exchange xchg) throws Exception {
			//Take the Employee object from the exchange and create the insert query
				Employee employee = xchg.getIn().getBody(Employee.class);
				String query = "INSERT INTO employee(empId,empName)values('" + employee.getEmpId() + "','"
						+ employee.getEmpName() + "')";
			// Set the insert query in body and call camel jdbc
		// Select Route
		from("direct:select").setBody(constant("select * from Employee")).to("jdbc:dataSource")
				.process(new Processor() {
					public void process(Exchange xchg) throws Exception {
					   //the camel jdbc select query has been executed. We get the list of employees.
						ArrayList<Map<String, String>> dataList = (ArrayList<Map<String, String>>) xchg.getIn()
						List<Employee> employees = new ArrayList<Employee>();
						for (Map<String, String> data : dataList) {
							Employee employee = new Employee();


Create the Controller class from where will be calling the Camel route using the ProducerTemplate. The ProducerTemplate interface allows you to send message exchanges to endpoints in a variety of different ways to make it easy to work with Camel Endpoint instances from Java code.
We expose two REST endpoints in this class -
  • GET API to fetch all employees from mysql database. This calls the camel select route.
  • POST API to insert employee details in mysql database. This calls the camel insert route with the Employee details to be inserted.
package com.javainuse.controller;

import java.util.List;

import org.apache.camel.ProducerTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.javainuse.model.Employee;

public class EmployeeController {

	ProducerTemplate producerTemplate;

	@RequestMapping(value = "/employees", method = RequestMethod.GET)
	public List<Employee> getAllEmployees() {
		List<Employee> employees = producerTemplate.requestBody("direct:select", null, List.class);
		return employees;


	@RequestMapping(value = "/employees", consumes = "application/json", method = RequestMethod.POST)
	public boolean insertEmployee(@RequestBody Employee emp) {
		producerTemplate.requestBody("direct:insert", emp, List.class);
		return true;
Finally create the Spring Boot Main class with the SpringBootApplication Annotation
package com.javainuse;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

public class SpringBootJdbcApplication {

	public static void main(String[] args) {, args);
Start the application.
Send a POST request to insert new employee detail-
Apache Camel Spring Boot POST Request
  Send a GET request to retrieve all employee details-
Apache Camel Spring Boot GET Request

Download Source Code

Download it -
Spring Boot + Apache Camel JDBC component + MySQL

See Also

Spring Boot Hello World Application- Create simple controller and jsp view using Maven Spring Boot Tutorial-Spring Data JPA Spring Boot + Simple Security Configuration Pagination using Spring Boot Simple Example Spring Boot + ActiveMQ Hello world Example Spring Boot + Swagger Example Hello World Example Spring Boot + Swagger- Understanding the various Swagger Annotations Spring Boot Main Menu Spring Boot Interview Questions