Spring and JDBC using JdbcTemplate
As part of this topic we will be integrating Spring and JDBC using JdbcTemplate.
We will be using Spring Tool Suit to create spring Java application which is nothing but an extension of Eclipse editor as part of this blog using with bean.xml.
To download and setup Spring Tool Kit please click on this link.
Follow below steps to create our very first Spring application project.
- Open Spring Tool kit we installed here.
- Click on File in top navigation menu and than click on new. There will be an option Spring Starter Project start appearing once we click on create new option.
Below window will start appearing.
Provide below highlighted value for Group and Artifact for the project and click on Next.
3. In the below screen select open Web option and select Spring Web. Click on Finish will create Spring project.
4. Create Person.java pojo class as mentioned in below screenshot with id, firstName, and age as a field with setter getter.
5. Create an interface PersonDao.java class to declare function which will help us to insert, update, select and delete record from database.
6. Create PersonMapper.java class to map MySQL person table with Person class object as shown below:
7. Create person table in MySQL database using below query
create table person(id int NOT NULL AUTO_INCREMENT, firstname varchar(20), age int, primary key(id));
8. Add below highlighted code in application pom.xml which will help us to connect with MySQL database.
9. Create JdbcPersonDAO.java class which will be implementing PersonDao.java interface.
Create a DataSource variable at line number 13 with setter method to collect all configurations from bean.xml file.
Create one more JdbcTemplate object to perform MySQL operation in Java.
insertPerson() is to insert a Person record in the database.
listAllPerson() method fetch all records from person table and print all the results.
delete() method will delete record for specific id from person MySQL table.
update() method will update person age to 30 having id equal to 3.
getPerson(id) method will fetch Person record from MySQL database having id equal to 3.
10. Create bean.xml under below hierarchy src/main/resources.
11. Place below content inside created bean.xml
Above code from line number 10 to 15 will help to create JDBC data source object having all details require to connect with MySQL database.
Create an id=personDAO of com.example.demo.JdbcPersonDAO class and assign a dataSource value to it.
12. Place below code at line number 11 and 12 inside Springannotation1Application.java class.
13. Run main method from Springannotation1Application.java class will generate below output.
Imran Khan, Adobe Community Advisor, AEM certified developer and Java Geek, is an experienced AEM developer with over 11 years of expertise in designing and implementing robust web applications. He leverages Adobe Experience Manager, Analytics, and Target to create dynamic digital experiences. Imran possesses extensive expertise in J2EE, Sightly, Struts 2.0, Spring, Hibernate, JPA, React, HTML, jQuery, and JavaScript.