CodesJava

Easy learning with example program codes

spring rowmapper example


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.

Example

package com.codesjava.SpringDB;
 
import java.util.List;
 
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ApplicationContext;
 
@SpringBootApplication
public class SpringDbApplication {
	private static ApplicationContext applicationContext;
    public static void main( String[] args ) {
    	applicationContext = SpringApplication.run(SpringDbApplication.class, args);  
    	performDBOperation();
    }
 
    private static void performDBOperation(){
    	JDBCController jdbcController = (JDBCController) applicationContext.getBean("jDBCController");
    	List<Users> userList = jdbcController.getUserList();
    	for(Users user : userList)  
            System.out.println(user);                 
    }
}
package com.codesjava.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;
 
@Repository("jDBCController")
public class JDBCController {
	@Autowired  
        JdbcTemplate jdbc;  
 
	public List<Users> getUserList() {
		return jdbc.query("select * from users",new RowMapper<Users>(){  
		    @Override  
		    public Users mapRow(ResultSet rs, int rownumber) throws SQLException {   
	        	Users user=new Users();  
	        	user.setSeq(rs.getInt(1));  
	        	user.setUsername(rs.getString(2));  
	        	user.setPassword(rs.getString(3));  
	        	return user;  
	        }  
	    });  
	}
}
package com.codesjava.SpringDB;
 
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;
 
@Entity  
@Table(name= "USERS") 
public class Users {
	private int seq;
	private String username;
	private String password;
 
	@Id
	@GeneratedValue
	@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;  
	} 
}
spring.datasource.url=jdbc:mysql://localhost:3306/test_schema?useSSL=false
spring.datasource.username=root
spring.datasource.password=*******
spring.datasource.driver-class-name=com.mysql.jdbc.Driver

Output

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

Related topics

Spring Tutorial

Spring framework.
Spring framework architecture.
Spring ioc container.
Spring bean.
Spring bean scopes.
Spring bean life cycle.
Spring callback methods.
Spring hello world.
Spring bean definition inheritance.
Spring bean definition template.
Spring dependency injection.
Spring constructor based injection.
Constructor injection type ambiguities.
Setter based dependency injection.
Spring dependency injection collections.
Spring autowire
Spring autowire by name
Spring autowire by type
Spring autowire by constructor
Spring JDBC tutorial
Spring JDBC Prepared Statement
Spring ResultSetExtractor
Spring RowMapper
Spring aop tutorial.
Spring AOP AspectJ Xml.
Spring AOP AspectJ Annotation.
Spring MVC tutorial.
Spring mvc framework.
Spring mvc configuration file.
Spring mvc hello world.
Spring MVC multiple controller.
Spring MVC login.
Spring mvc form handling.
Spring mvc exception handling.
Spring spel tutorial.
Spring spel hello world.
Spring spel operators.
Spring spel ternary operator.
Spring spel standardevaluationcontext.
Spring spel bean reference.
Spring spel method invocation.
Spring spel list, map.
Spring spel regex.
Maven Eclipse Spring
Spring boot overview
Spring boot architecture diagram
Spring boot components
Spring boot starter parent
Spring boot web app configuration
Run spring boot application
Spring boot change port
Spring boot change context path
Spring boot log sql statements
Spring boot hello world
Spring boot JSP
Spring boot thymeleaf
Spring boot with mysql
Spring security overview
Spring security architecture
Spring security maven dependency
Spring security xml
Spring security annotation
Spring security custom login xml
Spring security custom login annotation
Spring security form login
Spring security remember me
Spring security method level
Sign Up/ Sign In
Ask a Question


Industrial Training

We offers Placement Oriented Training on Java, Spring, JSF, Hibernate, PHP, AngularJS, Angular 4, PLSQL, Oracle BI Publisher etc. We also provides Online training, please mail us at hr@codesjava.com.

Development

We also provides the Development services for Website Development , Java Development, PHP Development, Android App Development etc. You can contact us on hr@codesjava.com.

Copyright © 2018 CodesJava DMCA.com Protection Status SiteMap Reference: Java Wiki