Spring boot JDBC example

Previous

In this post, we will see Spring boot JDBC example.

As we already know Spring boot does lot of auto configurations which help us to avoid a lot of boilerplate code. In the case of JDBC, spring boot provides auto-configuration such as creating DataSource and JDBCTemplate objects automatically based on application.properties.

So you just need to autowire JdbcTemplate bean in your application code as below.

Let’s create a simple Spring boot JDBC example.


Tools used

  • Spring Boot 2.2.2.RELEASE
  • Spring JDBC 5.2.2.RELEASE
  • HikariCP 3.4.1
  • Maven 3
  • Java 8

Project Structure

SpringBootJDBCProjectStructure


Create new Spring boot project

Step 1:  Go to https://start.spring.io and create a project with following dependencies

  • spring-boot-starter-jdbc

Here is the screenshot for the same.
SpringBootJDBCStartIO


Maven configuration

Add Mysql drive dependency
We need to add MySQL JDBC drive dependency in order to connect to Mysql.

Your pom.xml will look like below:


Configure datasource in Application.properties

Please change your username and password as per your mysql local setup.


Create database table

Let’s create a database table named Students which we are going to use in this example.

CREATE TABLE STUDENTS (
id int(11) NOT NULL AUTO_INCREMENT,
studentName varchar(255) DEFAULT NULL,
age int(3) DEFAULT NULL,
PRIMARY KEY (id)
);


Create model class

Let’s create simple Student.java.


Create Repository interface and implementation

Create its implemntation named StudentRepositoryImpl.java

We have use Spring’s JDBCTemplate to interact with database. JDBCTemplate helps us to avoid boiler plate code and provides convenient methods to retrieve and insert data in database.


Create service interface and implementation

Create a service interface StudentService.java

Create its implementation named StudentRepositoryImpl.java

Please note that we have annotated StudentServiceImpl with @Service.Spring will automatically create a bean based on this annotation.

StudentRepository is being injected into StudentServiceImpl class and StudentServiceImpl is delegating all CRUD operatons to StudentRepository which actually interacts with database with Spring JDBCTemplate.


Create Application class

we have injected StudentService class in SpringBootJdbcExampleApplication and use StudentService to do the CRUD operation here.

In testStudentData(), we have create 4 student objects and performed CRUD operation with the help of StudentService methods.


Run the application

When you will run the application, you will get below output:

StartApplication…
[SAVE]
Saving student with name: John
2019-12-25 00:09:09.183 INFO 18336 — [ main] com.zaxxer.hikari.HikariDataSource : HikariPool-1 – Starting…
2019-12-25 00:09:10.190 INFO 18336 — [ main] com.zaxxer.hikari.HikariDataSource : HikariPool-1 – Start completed.
Saving student with name: Martin
Saving student with name: Mary
Saving student with name: Ricky
get All students: [Student [studentId=1, StudentName=John,age =16], Student [studentId=2, StudentName=Martin,age =18], Student [studentId=3, StudentName=Mary,age =20], Student [studentId=4, StudentName=Ricky,age =15]]
Find Student with id 2
Student with id 2: Student [studentId=2, StudentName=Martin,age =18]
Update age of Martin to 19
Rows affected: 1
Delete Student with id 4
Rows affected: 1
get updated list of Students: [Student [studentId=1, StudentName=John,age =16], Student [studentId=2, StudentName=Martin,age =19], Student [studentId=3, StudentName=Mary,age =20]]

Verify database output

Let’s run the query to datbase and check if our changes are reflected in Students table

As you can see, tables shows updated data after performing CRUD operations.

Github Source code

That’s all about Spring boot JDBC example.

Previous

Add Comment