jdbctemplate之BeanPropertyRowMapper

今天看SpringAPI的時候無心中發現了Spring2.5新增了一個RowMapper的實現類org.springframework.jdbc.core.BeanPropertyRowMapper,可是貌似Spring的refrence裏面根本就沒說起到。Google了一下……貌似也莫得多少文檔。 java

    Spring API Doc的說明以下: spring

   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. sql

   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. app

   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. ide

   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". this

   Please note that this class is designed to provide convenience rather than high performance. For best performance consider using a custom RowMapper. orm


   也就說,它能夠把ResultSet和實體類的字段進行實現自動映射。 繼承

   一個具體的例子以下: ci

   假若有這樣一個表,SQL-Server2000的建表腳本以下: 文檔

代碼  
 /*
管理員表
*/
CREATE TABLE admin(
   id int identity(1,1) primary key,
   username varchar(20) not null,
   password varchar(32) not null,  
)

   爲此,咱們編寫一個對應的實體類admin,它是一個標準的javaBean,代碼以下:

代碼   
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";

        /**
         * 查詢記錄總數<br/>
         */
        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";        /**
         * 查詢記錄總數<br/>
         */
        public List<Admin> queryAll() {
                final String sql = "Select * from " + TABLE_NAME;
               
                return getJdbcTemplate().query(sql, new BeanPropertyRowMapper(Admin.class));
        }
}

 

    呵呵,只是一句話就徹底搞定了……Sprin會爲咱們自動映射……顯然這樣比之前方便多了。咱們還能夠把它用在其它任何使用RowMapper的場合……畢竟它繼承自RowMapper……

    須要注意的是:BeanPropertyRowMapper是根據字段名和實體類中的標準Setter方法進行映射滴。也就是說,咱們須要使表中的字段名和實體類的成員變量名稱一致。

相關文章
相關標籤/搜索