Spring – RowMapper Interface with Example
Spring is one of the most popular Java EE frameworks. It is an open-source lightweight framework that allows Java EE 7 developers to build simple, reliable, and scalable enterprise applications. This framework mainly focuses on providing various ways to help you manage your business objects. It made the development of Web applications much easier than compared to classic Java frameworks and application programming interfaces (APIs), such as Java database connectivity (JDBC), JavaServer Pages(JSP), and Java Servlet. This framework uses various new techniques such as Aspect-Oriented Programming (AOP), Plain Old Java Object (POJO), and dependency injection (DI), to develop enterprise applications. In Spring, the RowMapper interface is used to fetch the records from the database using the query() method of the JdbcTemplate class.
Syntax for query() method of JdbcTemplate class:
public T query(String sqlQuery, RowMapper<T> rowMapper)
RowMapper is a callback interface that is called for each row and maps the row of relations with the instances to the model(user-defined) class. Unlike ResultSetExtractor the RowMapper iterates the ResultSet internally and adds the extracted data into a collection, And we do not need to write the code for collections as we do in ResultSetExtractor. It has only one method mapRow() which takes two arguments ResultSet and rowNumber respectively. In order to use RowMapper, we need to implement this interface and provide the definition for mapRow() method.
Syntax for rowMapper() method:
public T mapRow(ResultSet resultSet, int rowNumber)throws SQLException
In this example, we will extract all the records from a Student table using RowMapper. For this tutorial, we will be using the following schema for the Student table.
Student(id INT, name VARCHAR(45), department VARCHAR(45))
Step By Step Implementation
Step 1: Create Table
In this step, we will create a Student table to store students’ information. For this tutorial, we will assume you have created the following table in your database.
CREATE TABLE STUDENT( id INT, name VARCHAR(45), department VARCHAR(45));
After creating the table we will insert the following data in our table.
INSERT INTO STUDENT VALUES(1, "geek", "computer science");
Step 2: Adding dependencies
In this step, we will add the maven dependencies to our application. Add the following dependencies to your pom.xml
Step 3: Create a model class
Now, we will create a model class for our students. This class will have three-member variables id, name, and department. We will also define its getters and setters method along with the toString() method.
Step 4: Create a Dao interface
Now, we will create an interface and name it is StudentDao, which we will use to access data from the database of data storage. We need to define the getAllStudentDetails() method which will return all the details of the student.
Step 5: Create an implementation class for Dao Interface
In this step, we will create an implementation class StudentDaoImpl.java. This class implements the StudentDao interface and provides the definition to the getAllStudentDetails() method of the StudentDao interface. In this class, we will also implement the RowMapper interface and provide the definition of its mapRow() method.
Step 6: Bean Configuration
In this step, we will create the spring configuration file and name it application-context.xml. We will configure our beans and use the factory-method attribute for bean creation. In order to make a connection to the database, we need the following information username, password, database connection, URL, and the driver class name. All this information is contained in the DriverManagerDataSource class, it has getConnection() method which returns a connection of java type. We are using the instance of JdbcTemplate in our StudentDao class and passing it using the setter injection method.
Note: In application context, you need to define the whole path of your dao class
Step 7: Creating Utilities Class
Now, we will create a Utility class for testing our application. For this create a new class and name it TestRowMapper.java and add the following code to it.
Step 8: Output
Now, we will run our application.
Please Login to comment...