In this tutorials I am going to show you how to work with Spring Boot JdbcTemplate using MySql Database.
Spring Boot JdbcTemplate :
Technologies :
The DriverManagerDataSource is used to contain the information about the database such as driver class name, connnection URL, username and password. There are a property named datasource in the JdbcTemplate class of DriverManagerDataSource type. So, we need to provide the reference of DriverManagerDataSource object in the JdbcTemplate class for the datasource. Select For Update Spring Jdbctemplate Average ratng: 3,5/5 4528 reviews Context: The framework used is Spring and all queries are run with JdbcTemplate. Mysql Server version is 5.6.19. May 09, 2017 In this Spring CRUD Example, we will build a Simple Spring Application and perform CRUD operations using Spring JdbcTemplate.We will create a simple Employee management application which has abilities to create a new employee, update the existing employee, get a particular employee/ all employee and finally delete the existing employee.
- Spring Boot-1.5.10
- Spring-Boot-Starter-jdbc
- Java 8
- MySql 5.5
Project Structure :
Spring Boot JdbcTemplate Example :
As part of this tutorials, I am going to implement a complete CRUD operations using Spring Boot JdbcTemplate.
Recommended :Spring JDBCTemplate Example
Defining all necessary data source, authentication credentials.
Database Preparation :
Create your mysql database (otp) and create below item table under otp database.
Created an item table, which represents all items and I am going to do all CRUD operations on this table.
Create Item Model to represent the above table.
Creating Item Repository :
This is the key class of our example, under which all CRUD operations are happening.
Create RestController to provide an endpoint to access from outside.
Application.java
Run it !
Access the Application :
Getting All Items :
http://localhost:8080/getAllItems
Add an Item :
http://localhost:8080/addItem?id=4&name=Refrigerator&category=Refrigerator
Getting All Items After adding :
http://localhost:8080/getAllItems
Select For Update Jdbc Example
Delete an Item from the list :
References :
Happy Learning 🙂
Download Example
- SpringBoot JDBCTemplate MySQL
File size: 88 KBDownloads: 1296
Related Posts
In this Spring CRUD Example, we will build a Simple Spring Application and perform CRUD operations using Spring JdbcTemplate. We will create a simple Employee management application which has abilities to create a new employee, update the existing employee, get a particular employee/ all employee and finally delete the existing employee.
Spring Jdbctemplate Select For Update Example
Creating table
Create EMPLOYEETable, simply Copy and Paste the following SQL query in the query editor to get the table created.
Folder Structure:
- Create a simple Maven Project “SpringJDBC”by selecting maven-archetype-quickstart and create a package for our source files “com.javainterviewpoint” under src/main/java
- Now add the following dependency in the POM.xml
- Create the Java classes Employee.java,EmployeeDAOImpl.java andSpringJDBCExample.java under com.javainterviewpointfolder.
Other interesting articles which you may like …
Spring CRUD Example
Employee.java
Our Employee class is a simple POJO class consisting getters and setters of Employee properties id, name, age, dept.
SpringConfig.xml
In our configuration file, we have defined the three beans
- DriverManagerDataSource – DriverManagerDataSource contains database related configurations such as driver class name, connection URL, username and password.
- JdbcTemplate – We will be referencing the dataSource id (DriverManagerDataSource ) to the property dataSource of the JdbcTemplate class.
- EmployeeDAOImpl – We will be referencing the jdbcTemplate id to the property jdbcTemplate of the EmployeeDAOImpl class.
EmployeeDAO.java
EmployeeDAOImpl.java
Jdbctemplate Run Update Query
EmployeeDAOImpl class implements the interface EmployeeDAO and overrides all the unimplemented methods. We have the below methods in our EmployeeDAOImpl class
- setJdbcTemplate() – Through Spring setter injection we will be injecting the jdbcTemplate from the Spring configuration file.
- getAllEmployee() – In order to fetch all the records from the database we just need to pass the SQL and the instance of the ResultSetExtractor to the query() method of jdbcTemplate. ResultSetExtractor interface accepts the ResultSet and returns a Java List. We need to override the extractData() method and map each ResultSet to an Employee object add to a list.
- getEmployeeById() – In order to fetch a particular record we just need to pass the SQL and the instance of the RowMapper to the queryForObject() method of jdbcTemplate. RowMapper interface internally iterates the ResultSet and adds it to the Collection (Map). Hence there is no need for us to iterate the ResultSet as we do in the ResultSetExtractor.
- updateEmployee() – We will be updating the corresponding employee by calling the update() method of the jdbcTemplate passing the SQL and the parameters.
- deleteEmployee() – In order to delete an employee, we need to call the update() method of the jdbcTemplate passing the SQL and the id.
SpringJDBC.java
Select For Update Spring Jdbctemplate 2020
- ClassPathXmlApplicationContext class reads our Configuration File(SpringConfig.xml)
- We will get our EmployeeDAOImpl Class instance by calling the getBean() method over the context.
- Call the saveEmployee(), getEmployeeById(), getAllEmployees(),updateEmployee() and deleteEmployee() methods over the EmployeeDAOImpl instance which we got above.
Select For Update Spring Jdbctemplate Training
Output:
Select For Update Mysql
Download Source Code