Newsletter

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 application.properties
  • 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 application.properties

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 application.properties and add your datasource information to the Spring Boot’s predefined keys.

application.properties

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
  • SpringBootApp.java
  • SpringJava4sController.java
  • SpringJava4sDAO.java
  • Customer.java
  • application.properties

pom.xml

SpringBootApp.java

SpringJava4sController.java

SpringJava4sDAO.java

Customer.java

application.properties

Output

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

http://localhost:8080/springbootds/getcustInfo

 

You Might Also Like

  ::. About the Author .::

Java4s_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.

Comments

One Response 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.

Name*
Mail*
Website



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.