Spring Boot JDBC + MySQL – How to Create/Configure a DataSource

In this article, I am going to explain you how to create/configure a datasource in Spring boot with an example. We are all aware that the process of creating a traditional Spring JDBC application is little tedious because of its XML configurations and we need to follow few steps to configure any datasource. But believe me with Spring Boot creating a JDBC application is as easy as counting 1,2,3.. :-)

Steps to Create DataSource in Spring Boot Application

  • Add Spring Boot JDBC dependency in POM.xml
  • Add datasource information in
  • Get JDBCTemplate object in your DAO with @Autowired annotation and use it

…there is no point 4, that’t it ;)

1. Add Spring Boot JDBC Dependency

In order to work with Spring Boot JDBC, first we need to add the following dependency in your applications POM.xml

2. Add Datasource Information in

In real time applications, we will write the datasources information generally in the XML’s, I hope you all aware of that. In Spring Boot, rather then writing in XML’s just open your and add your datasource information to the Spring Boot’s predefined keys.

With this we have configured the datasource in our spring boot application.

3. Get JDBCTemplate object in your DAO with @Autowired annotation

Go to your DAO class and get the object of JdbcTemplate by @Autowired annotation and use it. Spring Boot will automatically get the datasource details from application.propeties and injects to jdbcTemplate object while auto wiring.

Spring Boot JDBC + MySQL Example – How to Create/Configure a DataSource

Directory Structure

Required files

  • pom.xml



In the properties file I have mentioned contextPath for the application, so the application URL will be…



You Might Also Like

  ::. About the Author .::

- Full Stack Java/J2EE & UI Web Developer
Founder of Java4s - Get It Yourself, A popular Java/J2EE Programming Blog, Love Java and UI frameworks.
You can sign-up for the Email Newsletter for your daily dose of Java tutorials.


6 Responses to “Spring Boot JDBC + MySQL – How to Create/Configure a DataSource”
  1. Chetan says:

    Hi Sir,
    Thanks a lot! Nice explanation for beginners to start on spring boot.
    Could you please cover some more topics on same.

  2. surya says:


    Thanks for nice Explanation, in the above example we have used JdbcTemplate so how jdbc template read connection properties is internally reads from with dedicated key names? thanks!

  3. Ratan says:


    Can you please give any example which use ENV for database connection?

    Like in you put


    But I want to override this configuration, so I want my own IP address from system env, export DB_HOST=x.x.x.x:3306.
    So when MySQL connection init at that time it take this DB_HOST on place of localhost.

    Thanks in advance.

  4. Sri Harsha says:

    Thank you so much sir very detailed explanation can you please Better to maintain a youtube channel for spring boot videos You will be the best


By posting your answer, you agree to our comments policy.
Most Recent Posts from Top Categories
Hibernate Spring Struts
Contact | About Us | Privacy Policy | Advertise With Us

© 2010 - 2018 Java4s - Get It Yourself.
The content is copyrighted to Sivateja Kandula and may not be reproduced on other websites.