(残梦追月原创,转载请注明)
本文地址:http://www.blogjava.net/cmzy/archive/2008/09/11/228271.html
今天看SpringAPI的时候无意中发现了Spring2.5新增了一个RowMapper的实现类
org.springframework.jdbc.core.BeanPropertyRowMapper,但是貌似Spring的refrence里
面根本就没提及到。Google了一下……貌似也莫得多少文档。
Spring API Doc的说明如下:
RowMapper implementation that converts a
row into a new instance of the specified mapped target class. The mapped
target class must be a top-level class and it must have a default or
no-arg constructor.
Column values are mapped based on matching
the column name as obtained from result set metadata to public setters
for the corresponding properties. The names are matched either directly
or by transforming a name separating the parts with underscores to the
same name using "camel" case.
Mapping is provided for fields in the
target class for many common types, e.g.: String, boolean, Boolean,
byte, Byte, short, Short, int, Integer, long, Long, float, Float,
double, Double, BigDecimal, java.util.Date, etc.
To facilitate mapping between columns and
fields that don't have matching names, try using column aliases in the
SQL statement like "select fname as first_name from customer".
Please note that this class is designed to
provide convenience rather than high performance. For best performance
consider using a custom RowMapper.
也就说,它可以把ResultSet和实体类的字段进行实现自动映射。
一个具体的例子如下:
假如有这样一个表,SQL-Server2000的建表脚本如下:
-
/*
-
管理员表
-
*/
-
CREATE
TABLE
admin(
-
id int
identity(1,1)
primary
key
,
-
username varchar
(20)
not
null
,
-
password
varchar
(32)
not
null
,
-
)
为此,我们编写一个对应的实体类admin,它是一个标准的javaBean,代码如下:
-
-
-
-
package
db.demo;
-
-
-
-
-
-
-
-
-
public
class
Admin {
-
private
int
id;
-
private
String username;
-
private
String password;
-
-
public
int
getId() {
-
return
id;
-
}
-
-
public
void
setId(
int
id) {
-
this
.id = id;
-
}
-
-
public
String getUsername() {
-
return
username;
-
}
-
-
public
void
setUsername(String username) {
-
this
.username = username;
-
}
-
-
public
String getPassword() {
-
return
password;
-
}
-
-
public
void
setPassword(String password) {
-
this
.password = password;
-
}
-
}
以前,在相应的AdminDAO中,我们以前是这么做滴,看起来很麻烦,如果一个表的字段很多的话,就要人命了,我们必须不停的set、get:
-
-
-
-
package
db.demo;
-
-
import
java.sql.ResultSet;
-
import
java.sql.SQLException;
-
import
java.util.List;
-
-
import
org.springframework.jdbc.core.RowMapper;
-
import
org.springframework.jdbc.core.support.JdbcDaoSupport;
-
-
-
-
-
-
-
-
public
class
AdminDAO
extends
JdbcDaoSupport {
-
-
private
final
String ID =
"id"
;
-
private
final
String USERNAME =
"username"
;
-
private
final
String PASSWORD =
"password"
;
-
private
final
String TABLE_NAME =
"admin"
;
-
-
-
-
-
public
List<Admin> queryAll() {
-
final
String sql =
"Select * from "
+ TABLE_NAME;
-
-
return
getJdbcTemplate().query(sql,
new
RowMapper(){
-
-
public
Object mapRow(ResultSet rs,
int
rowNum)
throws
SQLException {
-
Admin admin = new
Admin();
-
admin.setId(rs.getInt(ID));
-
admin.setUsername(rs.getString(USERNAME));
-
admin.setPassword(rs.getString(PASSWORD));
-
return
admin;
-
}
-
-
});
-
}
-
}
可见,我们必须的手工对ResultSet和Admin进行映射。而现在,我们只是需要这样:
-
-
-
-
package
db.demo;
-
-
import
java.util.List;
-
-
import
org.springframework.jdbc.core.BeanPropertyRowMapper;
-
import
org.springframework.jdbc.core.support.JdbcDaoSupport;
-
-
-
-
-
-
-
-
public
class
AdminDAO
extends
JdbcDaoSupport {
-
-
private
final
String TABLE_NAME =
"admin"
;
-
-
-
-
-
public
List<Admin> queryAll() {
-
final
String sql =
"Select * from "
+ TABLE_NAME;
-
-
return
getJdbcTemplate().query(sql,
new
BeanPropertyRowMapper(Admin.
class
));
-
}
-
}
呵呵,只是一句话就完全搞定了……Sprin会为我们自动映射……显然这样比以前方便多了。我们还可以把它用在其它任何使用RowMapper的场合……毕竟它继承自RowMapper……
需要注意的是:BeanPropertyRowMapper是根据字段名和实体类中的标准Setter方法进行映射滴。也就是说,我们需要使表中的字段名和实体类的成员变量名称一致。
分享到:
相关推荐
精通Spring2.5pdf书籍proSpring2.5
spring2.5 定时器任务 spring2.5 定时器任务 spring2.5 定时器任务 spring2.5 定时器任务
spring 2.5 spring 2.5帮助文档 spring 2.5中文帮助文档 spring帮助文档 spring文档
struts2.0 spring2.5 hibernate3.2 注解最新框架
Spring2.5中文框架开发手册Spring2.5中文框架开发手册Spring2.5中文框架开发手册
spring2.5中文文档 spring2.5中文文档
spring2.5是根据Sping2.5官方文档生成 chm格式
这是一个struts 1.2 + spring 2.5 + hibernate 3.2框架demo, 运行环境为eclipse 3.2 + tomcat 5.5 + oracle 11g
最近做的Spring 2.5 AOP 例子,保留下以后复习说不定用得上. 1.使用类扫描功能这个有点意思.使用时方便了许多. 2.XML配置方式
eclipse搭建spring2.5eclipse搭建spring2.5eclipse搭建spring2.5
Spring2.5的源代码,需要认真研究!
spring 2.5新特性spring 2.5新特性spring 2.5新特性spring 2.5新特性spring 2.5新特性
压缩文件中包含spring2.5 用到的所有jar文件;提供给大家下载使用
精通spring2.5精通spring2.5精通spring2.5精通spring2.5精通spring2.5
Spring2.5支持更多的java注释,使用起来更方便
spring2.5需要的jarspring2.5需要的jarspring2.5需要的jarspring2.5需要的jar
spring2.5源码 spring2.5源码 spring2.5源码
spring2.5常用的包,希望能给大家带来帮助