[英]Some doubts about RowMapper use in JDBC in a Spring Framework application
我正在研究如何在Spring Framework中使用JDBC對數據庫執行查詢。
我正在學習本教程: http : //www.tutorialspoint.com/spring/spring_jdbc_example.htm
在本教程中,我定義了一個StudentDAO接口,它只定義了我想要的CRUD方法。
然后定義Student類,它是我想要在Student數據庫表上保留的實體。
然后定義StudentMapper類,它是RowMapper接口的特定實現,在這種情況下,用於將ResultSet中的特定記錄(由查詢返回)映射到Student對象。
然后,我有我的rappresent接口StudentDAO的實施StudentJDBCTemplate,在這個類我實現的接口中定義的CRUD方法。
好了,現在我有一個關於如何StudentMapper類工作的疑問:在這個StudentJDBCTemplate類有定義的返回是在學生數據庫表中的所有記錄的列表的方法,這其中:
public List<Student> listStudents() {
String SQL = "select * from Student";
List <Student> students = jdbcTemplateObject.query(SQL,
new StudentMapper());
return students;
}
你怎么看,這個方法返回一個List of Student對象並按以下方式工作:
它首先要做的是定義返回 SQL String中Student數據庫表中所有記錄的查詢。
然后通過jdbcTemplateObject對象上的查詢方法調用執行此查詢(這是JdbcTemplate Spring類的一個等級**
此方法有兩個參數:SQL String(包含必須執行的SQL查詢)和一個新的StudentMapper對象,它接受查詢返回的ResultSet對象並將其記錄映射到新的Student對象上
在這里閱讀: http ://static.springsource.org/spring/docs/current/javadoc-api/org/springframework/jdbc/core/JdbcTemplate.html sayas: 執行給定靜態SQL的查詢,將每一行映射到Java通過RowMapper對象。
我的疑問是有關事實,我的StudentMapper映射使用mapRow()方法的Student對象上一個ResultSet記錄,這是代碼:
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自動調用的嗎? (因為在這個例子中永遠不會手動調用...)
TNX
安德里亞
然后通過jdbcTemplateObject對象上的查詢方法調用執行此查詢(這是JdbcTemplate Spring類的一個等級**
將RowMapper
的實例傳遞給JdbcTemplate
方法時
List <Student> students = jdbcTemplateObject.query(SQL, new StudentMapper());
JdbcTemplate
取決於您調用的方法,將在內部使用映射器以及從JDBC Connection獲取的結果集,以創建所請求類型的對象。 例如,由於您調用了JdbcTemplate#query(String, RowMapper)
,該方法將使用您的String SQL查詢數據庫,並將循環遍歷ResultSet
中的每個“行”,如下所示:
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
方法來創建預期的返回對象。
這是我與BeanPropertyRowMapper一起使用的典型模式。 它節省了大量的編碼。 您的查詢需要對每列進行別名以匹配類中的屬性名稱。 在這種情況下, species_name as species
,其他列名稱恰好匹配。
public class Animal {
String species;
String phylum;
String family;
...getters and setters omitted
}
@Repository
public class AnimalRepository {
private NamedParameterJdbcTemplate namedParameterJdbcTemplate;
@Autowired
public void setDataSource(DataSource dataSource) {
this.namedParameterJdbcTemplate = new NamedParameterJdbcTemplate(dataSource);
}
public List<Animal> getAnimalsByPhylum(String phylum) {
String sql = " SELECT species_name as species, phylum, family FROM animals"
+" WHERE phylum = :phylum";
Map<String, Object> namedParameters = new HashMap<String, Object>();
namedParameters.put("phylum", phylum);
SqlParameterSource params = new MapSqlParameterSource(namedParameters);
List<Animal> records = namedParameterJdbcTemplate.query(sql,
params, BeanPropertyRowMapper.newInstance(Animal.class));
return records;
}
}
另一種方法是在每行需要更多自定義時使用RowMapper(此示例僅使用匿名類):
List<Animal> records = namedParameterJdbcTemplate.query(sql,
params, new RowMapper<Animal>(){
public Animal mapRow(ResultSet rs, int i) throws SQLException {
Animal animal = new Animal();
animal.setSpecies(rs.getString("species_name"));
if (some condition) {
animal.setPhylum(rs.getString("phylum"));
} else {
animal.setPhylum(rs.getString("phylum")+someThing());
}
animal.setFamily(rs.getString("family"));
return animal;
}
});
在Spring中使用RowMapper
import java.sql.ResultSet;
import java.sql.SQLException;
import org.springframework.jdbc.core.RowMapper;
public class RowsMap implements RowMapper<EmpPojo>{
@Override
public EmpPojo mapRow(ResultSet rs, int counts) throws SQLException {
EmpPojo em=new EmpPojo();
em.setEid(rs.getInt(1));
em.setEname(rs.getString(2));
em.setEsal(rs.getDouble(3));
return em;
}
}
Finally in Main class
List<EmpPojo> lm=jt.query("select * from emps", new RowsMap());
for(EmpPojo e:lm)
{
System.out.println(e.getEid()+" "+e.getEname()+" "+e.getEsal());
}
那么,誰調用這個mapRow方法呢? 它是由Spring Framework自動調用的嗎? (因為在這個例子中永遠不會手動調用...)
這是由spring框架自動調用的。 所有你需要的是指定
- 連接參數,
- SQL語句
- 聲明參數並提供參數值
- 為每次迭代做好工作。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.