tl  tr
  Home | Tutorials | Articles | Videos | Products | Tools | Search
Interviews | Open Source | Tag Cloud | Follow Us | Bookmark | Contact   
 Spring > JDBC > Bean Property Row Mapper

Bean Property Row Mapper 

Spring JDBC Framework simplifies the use of JDBC and helps to avoid common errors. The following example shows using BeanPropertyRowMapper class. It maps each row of the resultset with a new instance of target class. The target class should have a no-argument default constructor. It maps the column names from resultset to properties of target class using public setter methods. If column name doesn't match with property name, we need to provide a column alias matching the property name.


File Name  :  
/SpringJDBC001/conf/basic/applicationContext.xml 

File Name  :  
com/bethecoder/tutorials/spring3/basic/User.java 
   
package com.bethecoder.tutorials.spring3.basic;

public class User {

  private int userId;
  private String userName;
  private int age;
  private long salary;
  
  public User() {
  }

  public User(int userId, String userName, int age, long salary) {
    super();
    this.userId = userId;
    this.userName = userName;
    this.age = age;
    this.salary = salary;
  }
  
  public int getUserId() {
    return userId;
  }
  public void setUserId(int userId) {
    this.userId = userId;
  }
  public String getUserName() {
    return userName;
  }
  public void setUserName(String userName) {
    this.userName = userName;
  }
  public int getAge() {
    return age;
  }
  public void setAge(int age) {
    this.age = age;
  }
  public long getSalary() {
    return salary;
  }
  public void setSalary(long salary) {
    this.salary = salary;
  }
  
  public String toString() {
    return "User[" + userId + ", " + userName + ", " + age + ", " + salary + "]";
  }
}
   

File Name  :  
com/bethecoder/tutorials/spring3/tests/BeanPropertyRowMapperTest.java 
Author  :  Sudhakar KV
Email  :  [email protected]
   
package com.bethecoder.tutorials.spring3.tests;

import javax.sql.DataSource;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;

import com.bethecoder.tutorials.spring3.basic.User;

public class BeanPropertyRowMapperTest {

  /**
   @param args
   */
  public static void main(String[] args) {

    /**
     * Initialize context and get the JdbcTemplate
     */
    ApplicationContext appContext = new ClassPathXmlApplicationContext("applicationContext.xml");
    DataSource dataSource = (DataSourceappContext.getBean("dataSource");
    JdbcTemplate template = new JdbcTemplate(dataSource);

    int userId = 3;
    User user = template.queryForObject(
        "select ID as userId, NAME as userName, AGE, SALARY " 
        "from USER where ID = ?"
        new BeanPropertyRowMapper<User>(User.class), userId);

    System.out.println(user);
  }

}
   

It gives the following output,
User[3, Sudhakar, 29, 19800000]



 
  


  
bl  br