随笔-42  评论-349  文章-4  trackbacks-0

(残梦追月原创,转载请注明)

本文地址: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的建表脚本如下:

   为此,我们编写一个对应的实体类admin,它是一个标准的javaBean,代码如下:

   以前,在相应的AdminDAO中,我们以前是这么做滴,看起来很麻烦,如果一个表的字段很多的话,就要人命了,我们必须不停的set、get:

   可见,我们必须的手工对ResultSet和Admin进行映射。而现在,我们只是需要这样:

    呵呵,只是一句话就完全搞定了……Sprin会为我们自动映射……显然这样比以前方便多了。我们还可以把它用在其它任何使用RowMapper的场合……毕竟它继承自RowMapper……

    需要注意的是:BeanPropertyRowMapper是根据字段名和实体类中的标准Setter方法进行映射滴。也就是说,我们需要使表中的字段名和实体类的成员变量名称一致。





By:残梦追月
posted on 2008-09-11 09:33 残梦追月 阅读(6143) 评论(8)  编辑  收藏 所属分类: Spring

评论:
# re: 发现了Spring2.5里的一个新东西, BeanPropertyRowMapper类。 2008-09-11 13:18 | super2
还是很生硬  回复  更多评论
  
# re: 发现了Spring2.5里的一个新东西, BeanPropertyRowMapper类。 2008-09-11 18:17 | 残梦追月
@super2
呵呵。我不是翻译滴……只是介绍了哈它的用法……我哪个翻字用错了。  回复  更多评论
  
# re: 发现了Spring2.5里的一个新东西, BeanPropertyRowMapper类。 2008-09-12 10:36 |
这个查询太简单了吧?要是只查某部分字段或复合查询(如count(XX)、sun(XX))该怎么用?还能自动匹配吗?  回复  更多评论
  
# re: 发现了Spring2.5里的一个新东西, BeanPropertyRowMapper类。 2008-09-12 13:40 | 残梦追月
@巫
这个只是个例子嘛……你可以用别名啊……
select Count(*) as n form table  回复  更多评论
  
# re: 发现了Spring2.5里的一个新东西, BeanPropertyRowMapper类。 2009-05-27 14:01 | liyaxi
多谢兄弟 ,可是我在spring 2.0是已经发现了,  回复  更多评论
  
# re: 发现了Spring2.5里的一个新东西, BeanPropertyRowMapper类。 2010-04-13 09:24 | dinstone
@liyaxi
你太牛逼了,人家V2.5才出现,你就在2.0看见了??
/*
* @author Thomas Risberg
* @author Juergen Hoeller
* @since 2.5
*/
public class BeanPropertyRowMapper implements RowMapper

  回复  更多评论
  
# re: 发现了Spring2.5里的一个新东西, BeanPropertyRowMapper类。 2010-05-16 10:00 | chrislee
这东西应该不是在所有情况下都能通用的!
如果在多表链接的时候,两个表的字段中都用id,或者name的列名。
映射的时候肯定就会乱掉。
最近写.net的东西的时候,就想到这样的问题。写了个通用的,需要在每个类中加上辅助的信息,以防止多表链接的时候这样列名冲突的情况  回复  更多评论
  
# re: 发现了Spring2.5里的一个新东西, BeanPropertyRowMapper类。[未登录] 2012-03-12 15:18 | rover
如果有下划线,也会自动被转化成camel命名法....  回复  更多评论
  

只有注册用户登录后才能发表评论。


网站导航: