July 28 2016

Spring RowMapper Example

Spring RowMapper Example

The RowMapper is interface used by JdbcTemplate for mapping each row of data in the ResultSet.

public interface RowMapper<T>

Implementation class of the interface has to implement the ‘mapRow’ method and map each row of ResultSet to required Object. The ‘mapRow’ method returns an Object for the current row. SQLExceptions will be caught and handled by the calling JdbcTemplate.

Object mapRow(ResultSet rs,int rowNum) throws SQLException

In the below example, mapRow() method is implemented to map each row of ResultSet to Employee Object.

Example

Employee.java


package com.answersz.springJdbcTemplate;

public class Employee {

private Integer age;
private Integer employeeId;
private String fatherName;
private String name;

public Integer getAge() {
return age;
}

public void setAge(Integer age) {
this.age = age;
}

public Integer getEmployeeId() {
return employeeId;
}

public void setEmployeeId(Integer employeeId) {
this.employeeId = employeeId;
}

public String getFatherName() {
return fatherName;
}

public void setFatherName(String fatherName) {
this.fatherName = fatherName;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

}

EmployeeRowMapper.java


package com.answersz.springJdbcTemplate;

import java.sql.ResultSet;
import java.sql.SQLException;

import org.springframework.jdbc.core.RowMapper;

public class EmployeeRowMapper implements RowMapper {

public Object mapRow(ResultSet rs, int rowNo) throws SQLException {

Employee employee = new Employee();
employee.setEmployeeId(rs.getInt(1));
employee.setName(rs.getString(2));
employee.setFatherName(rs.getString(3));
employee.setAge(rs.getInt(4));
return employee;
}

}


EmployeeDAO.java

package com.answersz.springJdbcTemplate;

import java.util.List;

import org.springframework.jdbc.core.support.JdbcDaoSupport;

public class EmployeeDAO extends JdbcDaoSupport {

 // Setter DI
 private EmployeeRowMapper employeeRowMapper;

 public List<Employee> getEmployeeList() {
 String selectQuery = "SELECT * FROM EMPLOYEE;";

 // Passing EmployeeRowMapper to JdbcTemplate
 List<Employee> employeeList = getJdbcTemplate().query(selectQuery, employeeRowMapper);
 return employeeList;
 }

 public EmployeeRowMapper getEmployeeRowMapper() {
 return employeeRowMapper;
 }

 public void setEmployeeRowMapper(EmployeeRowMapper employeeRowMapper) {
 this.employeeRowMapper = employeeRowMapper;
 }

}

jdbcTemplate-rowMapper.xml


<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">

<bean id="employeeDAO" class="com.answersz.springJdbcTemplate.EmployeeDAO">
<property name="dataSource" ref="dataSource" />
<property name="employeeRowMapper" ref="employeeRowMapper" />
</bean>

<bean id="dataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver" />
<property name="url" value="jdbc:mysql://localhost:3306/answersz" />
<property name="username" value="root" />
<property name="password" value="admin" />
</bean>

<bean id="employeeRowMapper" class="com.answersz.springJdbcTemplate.EmployeeRowMapper" />

</beans>

EmployeeRowMapperTest.java

package com.answersz.springJdbcTemplate;

import java.util.List;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class EmployeeRowMapperTest {

 public static void main(String[] args) {

 // Spring RowMapper Example

 ApplicationContext context = new ClassPathXmlApplicationContext("jdbcTemplate-rowMapper.xml");

 EmployeeDAO employeeDAO = (EmployeeDAO) context.getBean("employeeDAO");

 List<Employee> empList = employeeDAO.getEmployeeList();

 System.out.println("EmpList size : " + empList.size());

 Employee employee = empList.get(0);

 System.out.println("ID : " + employee.getEmployeeId());
 System.out.println("Name : " + employee.getName());
 System.out.println("Father Name : " + employee.getFatherName());
 System.out.println("Age : " + employee.getAge());
 }

}

Output


EmpList size : 20
ID : 1
Name : Robert
Father Name : William
Age : 29



Copyright © 2014. All rights reserved.

Posted July 28, 2016 by answersz in category Spring

Leave a Comment

Your email address will not be published. Required fields are marked *

*