Publish Your Article Here

Spring Data JPA with Spring Boot Applications - MySql example - example

This page gives an example to configure Spring Boot application with Spring Data JPA along with MySql Database.

Include below dependencies in your pom.xml file:

1) spring-boot-starter-data-jpa

2) MySQL Driver

3) HikariCP 2.6 (to maintain connection pool)

Here is the example pom.xml for your reference:

<project xmlns="" xmlns:xsi=""



	    <!-- include Spring data JPA dependencies, exclude default tomcat pool-->
        <!-- MySql JDBC driver -->

        <!-- HikariCP connection pool -->

			<!-- Package as an executable jar/war -->

Entity Class - Employee

Employee Class
package com.java2novice.springboot.models;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.SequenceGenerator;

public class Employee {

    @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "emp_seq")
    @SequenceGenerator(sequenceName = "emp_seq", allocationSize = 1, name = "emp_seq")
	@Column(name = "emp_id")
    private Long empId;

	private String name;

	private Integer age;

	private Integer salary;

	public Long getEmpId() {
		return empId;

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

	public String getName() {
		return name;

	public void setName(String name) { = name;

	public Integer getAge() {
		return age;

	public void setAge(Integer age) {
		this.age = age;

	public Integer getSalary() {
		return salary;

	public void setSalary(Integer salary) {
		this.salary = salary;

You can have your DB datasource related configurations at file as shown below:
# Oracle DB Settings

# HikariCP settings


Create Spring Data CrudRepository:

package com.java2novice.springboot.dao;

import java.util.List;


import com.java2novice.springboot.models.Employee;

public interface EmployeeDao extends CrudRepository<Employee, Long>{

	List<Employee> findByName(String name);

Spring Boot Starter

package com.java2novice.springboot;

import javax.sql.DataSource;
import org.springframework.transaction.annotation.Transactional;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

import com.java2novice.springboot.dao.EmployeeDao;

public class SpringBootWebApplication implements CommandLineRunner{

	private static Logger logger = LoggerFactory.getLogger(SpringBootWebApplication.class);

    private DataSource dataSource;

	private EmployeeDao employeeDao;

	public static void main(String[] args) throws Exception {, args);

	@Transactional(readOnly = true)
    public void run(String... args) throws Exception {

<< Previous Program | Next Program >>

Spring-Boot Examples

  1. Spring-Boot initial setup.
  2. Spring-Boot hello world example
  3. What is spring-boot-starter-parent in Spring-Boot pom.xml file?
  4. What is @SpringBootApplication annotation in spring boot?
  5. What is in spring boot?
  6. What is @ConfigurationProperties annotation in spring boot?
  7. Spring Boot @ConfigurationProperties example
  8. Spring Boot @ConfigurationProperties Property Validation
  9. Difference between @ConfigurationProperties and @Value
  10. Spring boot web application configurations.
  11. How to run spring boot application through command line?
  12. How to run spring boot as a standalone application (non-web)?
  13. Spring boot property resolution order.
  14. Spring Boot – Profile based properties example.
  15. How to configure logback (SLF4J) logging to spring boot applications?
  16. How to update application context path in spring boot?
  17. How to disable spring logo banner in spring boot?
  18. Spring Data JPA with Spring Boot Applications - Oracle - example
  19. Spring Data JPA with Spring Boot Applications - MySql example
  20. How to configure Spring Boot to show Hibernate SQL Query in logs?
  21. Spring Boot – List all Beans loaded in the ApplicationContext
  22. How to load external property files into Spring Boot application?
  23. How to rename file in Spring Boot application?
  24. How to configure multiple DataSources (Databases) with Spring Boot and Spring Data?
Knowledge Centre
doPost Vs doGet methods
doGet() method is used to get information, while doPost() method is used for posting information. doGet() requests can't send large amount of information and is limited to 240-255 characters. However, doPost()requests passes all of its data, of unlimited length. A doGet() request is appended to the request URL in a query string and this allows the exchange is visible to the client, whereas a doPost() request passes directly over the socket connection as part of its HTTP request body and the exchange are invisible to the client.
Famous Quotations
If you don’t make mistakes, you’re not working on hard enough problems. And that’s a big mistake.
-- Frank Wilczek

About Author

I'm Nataraja Gootooru, programmer by profession and passionate about technologies. All examples given here are as simple as possible to help beginners. The source code is compiled and tested in my dev environment.

If you come across any mistakes or bugs, please email me to [email protected].

Most Visited Pages

Other Interesting Sites

Reference: Java™ Platform Standard Ed. 7 - API Specification | Java™ Platform Standard Ed. 8 - API Specification | Java is registered trademark of Oracle.
Privacy Policy | Copyright © 2022 by Nataraja Gootooru. All Rights Reserved.