关于RowMapper在Spring Framework应用程序中使用JDBC的一些疑问 [英] Some doubts about RowMapper use in JDBC in a Spring Framework application

查看:223
本文介绍了关于RowMapper在Spring Framework应用程序中使用JDBC的一些疑问的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在研究如何在Spring Framework中使用JDBC对数据库执行查询。

I am studying how to execute query on a database using JDBC in Spring Framework.

我正在学习本教程: http://www.tutorialspoint.com/spring/spring_jdbc_example.htm

在本教程中,我定义了一个 StudentDAO 接口,它只定义了我想要的CRUD方法。

In this tutorial I define a StudentDAO interface which only define the CRUD method that I want.

然后定义学生类,我想要在Student数据库表上保留的实体。

Then is defined the Student class that is the entity that I want to persist on the Student database table.

然后定义 StudentMapper 类是一个 RowMapper 接口的具体实现,在这种情况下,用于将 ResultSet 中的特定记录(由查询返回)映射到学生对象。

Then is defined the StudentMapper class that is a specific implementation of RowMapper interface that, in this case, is used to map a specific record in the ResultSet (returned by a query) to a Student object.

然后我有 StudentJDBCTemplate ,它支持我的 StudentDAO interf的实现ace,在这个类中我实现了界面中定义的CRUD方法。

Then I have the StudentJDBCTemplate that rappresent the implementation of my StudentDAO interface, in this class I implement the CRUD method that was defined in the interface.

好的,现在我对 StudentMapper class work:在这个 StudentJDBCTemplate 类中,定义了返回Student数据库表中所有记录列表的方法,这一个:

Ok, and now I have a doubt about how the StudentMapper class work: in this StudentJDBCTemplate class there is defined the method that return the list of all record that are in the Student database table, this one:

   public List<Student> listStudents() {
      String SQL = "select * from Student";
      List <Student> students = jdbcTemplateObject.query(SQL, 
                                new StudentMapper());
      return students;
   }

你怎么看,这个方法返回一个List of Student对象并且工作在以下方式:

How you can see, this method return a List of Student object and work in the following way:

它首先要做的是在SQL字符串中定义返回学生数据库表中的所有记录的查询。

the first thing that it do is to define the query that return all record in the Student database table in the SQL String.

然后这个查询由jdbcTemplateObject对象上的查询方法调用执行(这是 JdbcTemplate Spring类**的基础

Then this query is executed by the query method call on the jdbcTemplateObject object (that is an istance of JdbcTemplate Spring class**

这个方法有两个参数:SQL String(包含必须执行的SQL查询)和一个新的 StudentMapper 对象,它带有 ResultSet 对象,并将其记录在新的Student对象上

This method take two parameter: the SQL String (that contains the SQL query that must be executed) and a new StudentMapper object that take the ResultSet object returned by the query and map it's record on a new Student object

在此处阅读: http://static.springsource.org/spring/docs/current/javadoc-api /组织/ springframework的/ J dbc / core / JdbcTemplate.html 说:执行给定静态SQL的查询,通过RowMapper将每一行映射到Java对象。

我的怀疑与我的 StudentMapper 使用 mapRow()方法在Student对象上映射ResultSet记录有关,这是代码:

My doubt is related to the fact that my StudentMapper map a ResultSet record on a Student object using the mapRow() method, this is the code:

package com.tutorialspoint;

import java.sql.ResultSet;
import java.sql.SQLException;
import org.springframework.jdbc.core.RowMapper;

public class StudentMapper implements RowMapper<Student> {
   public Student mapRow(ResultSet rs, int rowNum) throws SQLException {
      Student student = new Student();
      student.setId(rs.getInt("id"));
      student.setName(rs.getString("name"));
      student.setAge(rs.getInt("age"));
      return student;
   }
}

那么,谁叫这个 mapRow 方法?它是由Spring Framework自动调用的吗? (因为在这个例子中永远不会被手动调用...)

So, who call this mapRow method? is it called automatically by the Spring Framework? (because in this example is never called manually...)

Tnx

Andrea

然后,此查询由jdbcTemplateObject对象上的查询方法调用执行(即 JdbcTemplate Spring class **

Then this query is executed by the query method call on the jdbcTemplateObject object (that is an istance of JdbcTemplate Spring class**

推荐答案

RowMapper 的实例传递给 JdbcTemplate 方法

List <Student> students = jdbcTemplateObject.query(SQL, new StudentMapper());

JdbcTemplate 取决于你调用的方法,在内部使用mapper和它从JDBC Connection获取的结果集来创建所请求类型的对象。例如,因为你调用了 JdbcTemplate#query(String,RowMapper) ,该方法将使用您的String SQL查询数据库,并将循环遍历 ResultSet 类中的每个行:

The JdbcTemplate depending on which method you called, will internally use the mapper with the result set it gets from the JDBC Connection to create an object of your requested type. For example, since you called JdbcTemplate#query(String, RowMapper), the method will use your String SQL to query the database and will loop through each "row" in the ResultSet kind of like this:

ResultSet rs = ... // execute query
List<Student> students = ...// some list
int rowNum = 0;
while(rs.next()) {
    Student student = rowMapper.mapRow(rs, rowNum);
    students.add(student);
    rowNum++;
}

return students;

所以, Spring JdbcTemplate 方法将使用您提供的 RowMapper 并调用其 mapRow 方法创建预期的返回对象。

So, Spring's JdbcTemplate method will use the RowMapper you provide and call its mapRow method to create the expected return object.

您可能想看看Martin Fowler的数据Mapper 表格数据网关结合使用,了解这些内容的分布情况并提供低耦合

You might like to look at Martin Fowler's Data Mapper in conjunction with Table Data Gateway for an idea of how these things are distributed and provide low coupling.

这篇关于关于RowMapper在Spring Framework应用程序中使用JDBC的一些疑问的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆