Spring rowmapper

RowMapper interface:

RowMapper is a callback interface used by JdbcTemplate’s query methods to process the ResultSet. It actually maps each row of ResultSet to a user defined object.

Method of ResultSetExtractor interface:

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

Where T represents the return type.


package com.w3spoint.SpringDB;
import java.util.List;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ApplicationContext;
public class SpringDbApplication {
	private static ApplicationContext applicationContext;
    public static void main( String[] args ) {
    	applicationContext = SpringApplication.run(SpringDbApplication.class, args);
    private static void performDBOperation(){
    	JDBCController jdbcController = (JDBCController) applicationContext.getBean("jDBCController");
    	List<users> userList = jdbcController.getUserList();
    	for(Users user : userList)
package com.w3spoint.SpringDB;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.ResultSetExtractor;
import org.springframework.stereotype.Repository;
public class JDBCController {
        JdbcTemplate jdbc;
	public List<users> getUserList() {
		return jdbc.query("select * from users",new RowMapper<users>(){
		    public Users mapRow(ResultSet rs, int rownumber) throws SQLException {
	        	Users user=new Users();
	        	return user;
package com.w3spoint.SpringDB;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;
@Table(name= "USERS")
public class Users {
	private int seq;
	private String username;
	private String password;
	@Column(name = "SEQ", unique = true, nullable = false)
	public int getSeq() {
		return seq;
	public void setSeq(int seq) {
		this.seq = seq;
	@Column(name = "USER_NAME")
	public String getUsername() {
		return username;
	public void setUsername(String username) {
		this.username = username;
	@Column(name = "PASSWORD")
	public String getPassword() {
		return password;
	public void setPassword(String password) {
		this.password = password;
        public String toString(){
	    return seq+" "+username+" "+password;


1 Jai jai@123
2 Vivek vivek@123
12 hkumar h123
15 Vishal v123

Related topics

Please follow and like us:
Content Protection by DMCA.com