Spring 里的一个新东西, BeanPropertyRowMapper类

今天看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的建表脚本如下:

  代码 

/*  
管理员表  
*/  
CREATETABLEadmin(  
 idintidentity(1,1)primarykey,  
 usernamevarchar(20)notnull,  
 passwordvarchar(32)notnull,   
)


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

代码
/**
* 
*/ 
packagedb.demo;  
/**
*@authorzhangyong
* 
*@version8:11:57PM
* 
*/ 
publicclassAdmin{  
    privateintid;  
    privateStringusername;  
    privateStringpassword;  
    publicintgetId(){  
        returnid;  
    }  
    publicvoidsetId(intid){  
        this.id=id;  
    }  
    publicStringgetUsername(){  
        returnusername;  
    }  
    publicvoidsetUsername(Stringusername){  
        this.username=username;  
    }  
    publicStringgetPassword(){  
        returnpassword;  
    }  
    publicvoidsetPassword(Stringpassword){  
        this.password=password;  
    }  
}


以前,在相应的AdminDAO中,我们以前是这么做滴,看起来很麻烦,如果一个表的字段很多的话,就要人命了,我们必须不停的set、get:
/**
* 
*/ 
packagedb.demo;  
importjava.sql.ResultSet;  
importjava.sql.SQLException;  
importjava.util.List;  
importorg.springframework.jdbc.core.RowMapper;  
importorg.springframework.jdbc.core.support.JdbcDaoSupport;  
/**
*@authorzhangyong
* 
*@version10:05:37PM
* 
*/ 
publicclassAdminDAOextendsJdbcDaoSupport{  
    privatefinalStringID="id";  
    privatefinalStringUSERNAME="username";  
    privatefinalStringPASSWORD="password";  
    privatefinalStringTABLE_NAME="admin";  
    /**
    *查询记录总数

    */ 
    publicListqueryAll(){  
        finalStringsql="Select*from"+TABLE_NAME;  
          
        returngetJdbcTemplate().query(sql,newRowMapper(){  
            publicObjectmapRow(ResultSetrs,introwNum)throwsSQLException{  
                Adminadmin=newAdmin();  
                admin.setId(rs.getInt(ID));  
                admin.setUsername(rs.getString(USERNAME));  
                admin.setPassword(rs.getString(PASSWORD));  
                returnadmin;  
            }  
              
        });  
    }  
}  


/**
* 
*/ 
packagedb.demo;  
importjava.util.List;  
importorg.springframework.jdbc.core.BeanPropertyRowMapper;  
importorg.springframework.jdbc.core.support.JdbcDaoSupport;  
/**
*@authorzhangyong
* 
*@version10:05:37PM
* 
*/ 
publicclassAdminDAOextendsJdbcDaoSupport{  
    privatefinalStringTABLE_NAME="admin";  
    /**
    *查询记录总数

    */ 
    publicListqueryAll(){  
        finalStringsql="Select*from"+TABLE_NAME;  
          
        returngetJdbcTemplate().query(sql,newBeanPropertyRowMapper(Admin.class));  
    }  
}



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

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

【转载地址】http://licg1234.blog.163.com/blog/static/1390823332010230111137644/

猜你喜欢

转载自hck.iteye.com/blog/1727960
今日推荐