Spring框架JdbcTemplate类中查询方法介绍

 今天看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;
/**
 * @author zhangyong
 * 
 * @version 8:11:57 PM
 * 
 */
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中,如果用原生RowMapper做法如下注意的是rs.getString(USERNAME)  这里数据库字段要和实体属性名保持一致,否则null

/**
 * 
 */
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;


/**
 * @author zhangyong
 * 
 * @version 10:05:37 PM
 * 
 */
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;
                        }
                        
                });
        }
}


  现在使用BeanPropertyRowMapper 做法如下:注意的是使用BeanPropertyRowMapper实体属性要和数据库字段名有个规范:

自动绑定,需要列名称和Java实体类名字一致,如:属性名 “userName” 可以匹配数据库中的列字段 "USERNAME" 或 “user_name”。这样,我们就不需要一个个手动绑定了,大大提高了开发效率。java属性形如:my_Num 对应数据库字段MY_NUM 也是可以的。

/
 * 
 */
package db.demo;


import java.util.List;


import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.support.JdbcDaoSupport;


/**
 * @author zhangyong
 * 
 * @version 10:05:37 PM
 * 
 */
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));
        }
}


查询时使用RowMapper还是其他BeanPropertyRowMapper,ColumnMapRowMapper,SingleColumnRowMapper 定要注意数据库和实体属性命名。


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章