Spring boot: Database connectivity to MySQL using Hibernate + Web

Hello friends,
In this post we will see how to connect database using boot, for this demo will use MySQL as a database and Hibernate as an ORM.

For this example we need “Web” and “JPA” dependency in our project.

Add below dependency in your pom.xml

Note: artifactId spring-boot-starter-data-jpa will internally provides support and download dependency for spring-orm, hibernate-entity-manager and spring-data-jpa

or download fully configured project using url below.

http://start.spring.io/starter.zip
?name=db-mysql-jpa
&groupId=com.kode12
&artifactId=db-mysql-jpa
&version=0.0.1-SNAPSHOT
&description=Project+to+show+database+connectivity.
&packageName=com.kode12.dbmysqljpa
&type=maven-project
&packaging=war
&javaVersion=1.7
&language=java
&bootVersion=1.3.3.RELEASE
&dependencies=data-jpa
&dependencies=web

After downloading and importing project in to your editor you will find one java file DbMysqlJpaApplication.java with main method. We are using this class to start server.

DbMysqlJpaApplication.java

Code

After that i created one controller in controllers package, one model class in models package and one service interface in services package.

Find project structure in below screenshot.

Now lets see all files with code.

application.properties

Code

This file contains some new properties comparing to previous posts.
spring.datasource.url is used to provide database connection url.
spring.datasource.username is used to provide database user name.
spring.datasource.password is used to provide database password.

spring.jpa.hibernate.ddl-auto is used to provide what hibernate needs to do on ddl change, update meand it will update database schema if model changed.
spring.jpa.show-sql is used to toggle SQL output inn console, true means hibernate will log all queries executed by in console.

Employee.java

Code

This is a Pojo class is used as hibernate entity as we annotated by @Entity.

EmployeeService.java

Code

Oops !, nothing is written in this file.
You are right reader, here we just create one interface and extended another interface called CrudRepository with Parameter Employee as an type of entity and Long as an type of ID field.

Technically CrudRepository extends Repository interface.

Boot will allow to do some basic CRUD (Create, Read, Update and Delete) operation without any implementations using CrudRepository interface.

Basic operation like save, delete, count, findAll, findById, exist, etc…

Employeecontroller.java

Code

In this controller we used three methods from CrudRepository.
Here, we just Autowired EmployeeService interface and used findAll(), findOne(Long id) and count() method to perform operations.

Run Project and Output

To run project execute main method from class DbMysqlJpaApplication.java.

Insert sample data

Use below script to insert sample data in Employee table.

Invoke getAll endpoint

Use http://localhost:8081/employee/getAll to invoke getAll endpoint.

Output

Query by hibernate

It will return 3 records in JSON format.

Invoke getById endpoint

Use http://localhost:8081/employee/getById/3 to invoke getById endpoint.

Output

Query by hibernate

It will return 3rd record where name=Aarush available as we passed value 3 in path.

Invoke count endpoint

Use http://localhost:8081/employee/count to invoke count endpoint.

Output

Query by hibernate

It will return total no of records available in database.

At Last, i would like to hear from you guys for some mistakes, new blogs, demo, ideas or anything which will help us to make our blog better.

Share current post by copy: http://goo.gl/R7cMbS

:)