Monday, 21 April 2014

RowMapper Example in Spring JDBC


The following example illustrates using RowMapper interface to query for data as object. The RowMapper, the name itself says, as every row in a table is represented by an object, for every record that we obtain from the result set, we convert it into corresponding class object.

Brief view of the RowMapper interface

This interface contains only one method mapRow() which is as follows:

public Object mapRow(ResultSet rs, int rowNum);

You should write a class, which we call mapper class that implements this interface and provide bod for this method. This method should return the corresponding object which contains the data in that row.

Theme of the application

The theme of the application is to query for a particular record by sno. This application revolves around a student table that contains three columns sno, sname, age.

Project structure

spring50
          |_ jdbcContext.xml
          |_ spring50
                         |_ Student.java
                         |_ Student.class
                         |_ StudentDao.java
                         |_ StudentDao.class
                         |_ StudentMapper.java
                         |_ StudentMapper.class
                         |_ SpringPrg.java
                         |_ SpringPrg.class

jdbcContext.xml

This file contains two bean definitions. One pointing to the datasource object that contains details about database connection and the other is the dao class.


<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-3.2.xsd">

        <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
            <property name="driverClassName" value="oracle.jdbc.driver.OracleDriver"/>
            <property name="url" value="jdbc:oracle:thin:@localhost:1521:xe"/>
            <property name="username" value="scott"/>
            <property name="password" value="tiger"/>
        </bean>

        <bean id="studentDao" class="spring50.StudentDao">
            <property name="datasource" ref="dataSource"/>
        </bean>

</beans>

Student.java

This bean class presents the table student in the database.

package spring50;

public class Student
{
private int sno;
private String sname;
private int age;

    public Student()
    {

    }

    public Student(int sno,String sname,int age)
    {
        this.sno=sno;
        this.sname=sname;
        this.age=age;
    }

    public void setSno(int sno)
    {
        this.sno=sno;
    }

    public int getSno()
    {
        return sno;
    }

    public void setSname(String sname)
    {
        this.sname=sname;
    }

    public String getSname()
    {
        return sname;
    }

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

    public int getAge()
    {
        return age;
    }

    public String toString()
    {
        return sno+" "+sname+" "+age;
    }
}

StudentDao.java



package spring50;

import javax.sql.*;
import org.springframework.jdbc.core.JdbcTemplate;

public class StudentDao
{
private DataSource datasource;
private JdbcTemplate jdbcTemplate;

private String findBySno;
   
    {
        findBySno="select *from student where sno=?";
    }

    public void setDatasource(DataSource datasource)
    {
        this.datasource=datasource;
        jdbcTemplate=new JdbcTemplate(datasource);
    }

    public DataSource getDataSource()
    {
        return datasource;
    }

    public Student findBySno(int sno)
    {
        return jdbcTemplate.queryForObject(findBySno,new Object[]{sno},new StudentMapper());
    }

}

This class takes in datasource with the help of which it will create a JdbcTemplate object. This JdbcTemplate object is used to perform database operations. 
 
findBySno(int sno): This method returns the object that contains the details of a student with a particular sno. Here, we have used the method queryForObject() 
 
queryForObject(String sqlQuery, Object[] paramValues, RowMapper mapper) The first is the select command, next is the parameter values i.e. the values that should be put in place of ? in the command. Here there is only one ? and value that should be placed is sno. Next, is the StudentMapper class that you will see below. This method returns the Student object that contains the details queried from the database.


StudentMapper.java





package spring50;

import org.springframework.jdbc.core.*;
import java.sql.*;

public class StudentMapper implements RowMapper<Student>
{
    public Student mapRow(ResultSet rs,int rowNum) throws SQLException
    {
        Student st=new Student();
        st.setSno(rs.getInt("sno"));
        st.setSname(rs.getString("sname"));
        st.setAge(rs.getInt("age"));

        return st;
    }
}

Here, we will make use of the getInt(), getString() to get the details of that particular column in the row.


SpringPrg.java



package spring50;

import org.springframework.context.support.GenericXmlApplicationContext;

public class SpringPrg
{
    public static void main(String args[])
    {
        GenericXmlApplicationContext gc=new GenericXmlApplicationContext("classpath:jdbcContext.xml");

        StudentDao st=gc.getBean("studentDao",StudentDao.class);

        // find by sno
        Student student=st.findBySno(101);

        System.out.println(student);
    }
}
 
Here we pass in the sno to the findBySno() method and get the object as result.
 

No comments:

Post a Comment