W3Schools Learner's Blog

W3Schools Programming knowledge summary website



RowMapper In Spring JdbcTemplate

Like ResultSetExtractor, we can use RowMapper interface to fetch the records from the database using query() method of JdbcTemplate class. In the execute of we need to pass the instance of RowMapper now.

Syntax of query method using RowMapper

  1. public T query(String sql,RowMapper<T> rm)  

RowMapper Interface

RowMapper interface allows to map a row of the relations with the instance of user-defined class. It iterates the ResultSet internally and adds it into the collection. So we don't need to write a lot of code to fetch the records as ResultSetExtractor.

Advantage of RowMapper over ResultSetExtractor

RowMapper saves a lot of code becuase it internally adds the data of ResultSet into the collection.

Method of RowMapper interface

It defines only one method mapRow that accepts ResultSet instance and int as the parameter list. Syntax of the method is given below:
  1. public T mapRow(ResultSet rs, int rowNumber)throws SQLException  

Example of RowMapper Interface to show all the records of the table

We are assuming that you have created the following table inside the Oracle10g database.
  1. create table employee(  
  2. id number(10),  
  3. name varchar2(100),  
  4. salary number(10)  
  5. );  
This class contains 3 properties with constructors and setter and getters and one extra method toString().
  1. package com.javatpoint;  
  3. public class Employee {  
  4. private int id;  
  5. private String name;  
  6. private float salary;  
  7. //no-arg and parameterized constructors  
  8. //getters and setters  
  9. public String toString(){  
  10.     return id+" "+name+" "+salary;  
  11. }  
  12. }  
It contains on property jdbcTemplate and one method getAllEmployeesRowMapper.
  1. package com.javatpoint;  
  2. import java.sql.ResultSet;  
  3. import java.sql.SQLException;  
  4. import java.util.ArrayList;  
  5. import java.util.List;  
  6. import org.springframework.dao.DataAccessException;  
  7. import org.springframework.jdbc.core.JdbcTemplate;  
  8. import org.springframework.jdbc.core.ResultSetExtractor;  
  9. import org.springframework.jdbc.core.RowMapper;  
  11. public class EmployeeDao {  
  12. private JdbcTemplate template;  
  14. public void setTemplate(JdbcTemplate template) {  
  15.     this.template = template;  
  16. }  
  18. public List<Employee> getAllEmployeesRowMapper(){  
  19.  return template.query("select * from employee",new RowMapper<Employee>(){  
  20.     @Override  
  21.     public Employee mapRow(ResultSet rs, int rownumber) throws SQLException {  
  22.         Employee e=new Employee();  
  23.         e.setId(rs.getInt(1));  
  24.         e.setName(rs.getString(2));  
  25.         e.setSalary(rs.getInt(3));  
  26.         return e;  
  27.     }  
  28.     });  
  29. }  
  30. }  

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 property.

Here, we are using the JdbcTemplate object in the EmployeeDao class, so we are passing it by the setter method but you can use constructor also.
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <beans  
  3.     xmlns="http://www.springframework.org/schema/beans"  
  4.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  5.     xmlns:p="http://www.springframework.org/schema/p"  
  6.     xsi:schemaLocation="http://www.springframework.org/schema/beans   
  7. http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">  
  9. <bean id="ds" class="org.springframework.jdbc.datasource.DriverManagerDataSource">  
  10. <property name="driverClassName" value="oracle.jdbc.driver.OracleDriver" />  
  11. <property name="url" value="jdbc:oracle:thin:@localhost:1521:xe" />  
  12. <property name="username" value="root" />  
  13. <property name="password" value="root" />  
  14. </bean>  
  16. <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">  
  17. <property name="dataSource" ref="ds"></property>  
  18. </bean>  
  20. <bean id="edao" class="com.javatpoint.EmployeeDao">  
  21. <property name="jdbcTemplate" ref="jdbcTemplate"></property>  
  22. </bean>  
  24. </beans>  
This class gets the bean from the applicationContext.xml file and calls the getAllEmployeesRowMapper() method of EmployeeDao class.
  1. package com.javatpoint;  
  3. import java.util.List;  
  5. import org.springframework.context.ApplicationContext;  
  6. import org.springframework.context.support.ClassPathXmlApplicationContext;  
  7. public class Test {  
  8. public static void main(String[] args) {  
  9.     ApplicationContext ctx=new ClassPathXmlApplicationContext("applicationContext.xml");  
  10.     EmployeeDao dao=(EmployeeDao)ctx.getBean("edao");  
  11.     List<Employee> list=dao.getAllEmployeesRowMapper();  
  13.     for(Employee e:list)  
  14.         System.out.println(e);  
  15. }  
  16. }  

No comments:

Post a Comment

Note: only a member of this blog may post a comment.