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.w3schools.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.w3schools.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.w3schools.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.w3schools.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.w3schools.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;
}
} |
package com.w3schools.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 |
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 |
1 Jai jai@123
2 Vivek vivek@123
12 hkumar h123
15 Vishal v123
Related topics