Spring2.5 BeanPropertyRowMapper ResultSet和实体类的字段进行实现自动映射
2016-05-01 00:35
976 查看
org.springframework.jdbc.core.BeanPropertyRowMapper 可以将查询的ResultSet和实体类的字段进行实现自动映射
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.
一个具体的例子如下:
假如有这样一个表,SQL-Server2000的建表脚本如下:
1. /*
2. 管理员表
3. */
4. CREATE TABLE admin(
5. id int identity(1,1) primary key,
6. username varchar(20) not null,
7. password varchar(32) not null,
8. )
javaBean:
1. /**
2. *
3. */
4. package db.demo;
5.
6.
7. /**
8. * @author zhangyong
9. *
10. * @version 8:11:57 PM
11. *
12. */
13. public class Admin {
14. private int id;
15. private String username;
16. private String password;
17.
18. public int getId() {
19. return id;
20. }
21.
22. public void setId(int id) {
23. this.id = id;
24. }
25.
26. public String getUsername() {
27. return username;
28. }
29.
30. public void setUsername(String username) {
31. this.username = username;
32. }
33.
34. public String getPassword() {
35. return password;
36. }
37.
38. public void setPassword(String password) {
39. this.password = password;
40. }
41. }
以前,在相应的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;
#
# /**
# * @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我们只是需要这样:
1. /**
2. *
3. */
4. package db.demo;
5.
6. import java.util.List;
7.
8. import org.springframework.jdbc.core.BeanPropertyRowMapper;
9. import org.springframework.jdbc.core.support.JdbcDaoSupport;
10.
11. /**
12. * @author zhangyong
13. *
14. * @version 10:05:37 PM
15. *
16. */
17. public class AdminDAO extends JdbcDaoSupport {
18.
19. private final String TABLE_NAME = "admin";
20.
21. /**
22. * 查询记录总数<br/>
23. */
24. public List<Admin> queryAll() {
25. final String sql = "Select * from " + TABLE_NAME;
26.
27. return getJdbcTemplate().query(sql, new BeanPropertyRowMapper(Admin.class));
28. }
29. }
Sprin会为我们自动映射……显然这样比以前方便多了。我们还可以把它用在其它任何使用RowMapper的场合……毕竟它继承自RowMapper……
需要注意的是:BeanPropertyRowMapper是根据字段名和实体类中的标准Setter方法进行映射滴。也就是说,我们需要使表中的字段名和实体类的成员变量名称一致。需要加载Spring2.5以上版本支持,Spring2.06中无BeanPropertyRowMapper类。
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.
一个具体的例子如下:
假如有这样一个表,SQL-Server2000的建表脚本如下:
1. /*
2. 管理员表
3. */
4. CREATE TABLE admin(
5. id int identity(1,1) primary key,
6. username varchar(20) not null,
7. password varchar(32) not null,
8. )
javaBean:
1. /**
2. *
3. */
4. package db.demo;
5.
6.
7. /**
8. * @author zhangyong
9. *
10. * @version 8:11:57 PM
11. *
12. */
13. public class Admin {
14. private int id;
15. private String username;
16. private String password;
17.
18. public int getId() {
19. return id;
20. }
21.
22. public void setId(int id) {
23. this.id = id;
24. }
25.
26. public String getUsername() {
27. return username;
28. }
29.
30. public void setUsername(String username) {
31. this.username = username;
32. }
33.
34. public String getPassword() {
35. return password;
36. }
37.
38. public void setPassword(String password) {
39. this.password = password;
40. }
41. }
以前,在相应的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;
#
# /**
# * @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我们只是需要这样:
1. /**
2. *
3. */
4. package db.demo;
5.
6. import java.util.List;
7.
8. import org.springframework.jdbc.core.BeanPropertyRowMapper;
9. import org.springframework.jdbc.core.support.JdbcDaoSupport;
10.
11. /**
12. * @author zhangyong
13. *
14. * @version 10:05:37 PM
15. *
16. */
17. public class AdminDAO extends JdbcDaoSupport {
18.
19. private final String TABLE_NAME = "admin";
20.
21. /**
22. * 查询记录总数<br/>
23. */
24. public List<Admin> queryAll() {
25. final String sql = "Select * from " + TABLE_NAME;
26.
27. return getJdbcTemplate().query(sql, new BeanPropertyRowMapper(Admin.class));
28. }
29. }
Sprin会为我们自动映射……显然这样比以前方便多了。我们还可以把它用在其它任何使用RowMapper的场合……毕竟它继承自RowMapper……
需要注意的是:BeanPropertyRowMapper是根据字段名和实体类中的标准Setter方法进行映射滴。也就是说,我们需要使表中的字段名和实体类的成员变量名称一致。需要加载Spring2.5以上版本支持,Spring2.06中无BeanPropertyRowMapper类。
相关文章推荐
- android源码解析(十八)-->Activity布局绘制流程
- Android自定义View(一)实现文字验证码
- 作业——在线学习Android课程之第九周(进程与服务)
- iOS边练边学--AFNetWorking框架GET、Post、Download、Upload,数据解析模式以及监控联网状态
- Appcan与后台数据交互,登录例子
- 【Unity3d】浅谈异步加载场景
- 监听应用的启动
- Unity3d之Animation(动画系统)
- Windows版微信表情第二个标签页中表情的奇怪问题
- 类似Tinder APP的配对逻辑
- App Store如何申请一次通过?
- 编译可在Nexus5上运行的CyanogenMod13.0(基于Android6.0)ROM
- Spring中ApplicationContext的事件机制
- Unity Official Tutorial OF PICKING UP COLLECTABLES --- Camera Follows Player
- iOS9 新增特性(汇)
- Android之内部存储读取数据
- Unity 中一些图形学知识
- iOS边练边学--NSURLSession、NSURLSessionTask的介绍与使用以及url中包含了中文的处理方法
- android第一行代码-3.activity之间的调用跟数据传递
- Android学习之界面篇(六)ViewPager学习与Android引导界面的实现