标签:smd sso acea 自定义 template auto size hash long
public T mapRow(ResultSet rs, int rowNumber) throws SQLException {
T mappedObject = BeanUtils.instantiate(this.mappedClass);
BeanWrapper bw = PropertyAccessorFactory.forBeanPropertyAccess(mappedObject);
ResultSetMetaData rsmd = rs.getMetaData();
int columnCount = rsmd.getColumnCount();
Set<String> populatedProperties = (isCheckFullyPopulated() ? new HashSet<String>() : null);
for (int index = 1; index <= columnCount; index++) {
String column = JdbcUtils.lookupColumnName(rsmd, index);
PropertyDescriptor pd = this.mappedFields.get(column.replaceAll(" ", "").toLowerCase());
if (pd != null) {
try {
Object value = getColumnValue(rs, index, pd);
bw.setPropertyValue(pd.getName(), value);
if (populatedProperties != null) {
populatedProperties.add(pd.getName());
}
}
}
}
if (populatedProperties != null && !populatedProperties.equals(this.mappedProperties)) {
throw new InvalidDataAccessApiUsageException("Given ResultSet does not contain all fields " +
"necessary to populate object of class [" + this.mappedClass + "]: " + this.mappedProperties);
}
return mappedObject;
}
注意到mapRow方法里面,populatedProperties变量是用来检查是否所有的property都正确地实现了赋值
/**
* 实现数据表与字段的映射
*
* @author andy
*
*/
public class UserInfo implements RowMapper<UserInfo>, Serializable {
/**
*
*/
private static final long serialVersionUID = -8823504831198719837L;
private Integer id;
private String uname;
private Integer unumber;
private Date uRegisterTime;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getUname() {
return uname;
}
public void setUname(String uname) {
this.uname = uname == null ? null : uname.trim();
}
public Integer getUnumber() {
return unumber;
}
public void setUnumber(Integer unumber) {
this.unumber = unumber;
}
public Date getuRegisterTime() {
return uRegisterTime;
}
public void setuRegisterTime(Date uRegisterTime) {
this.uRegisterTime = uRegisterTime;
}
@Override
public UserInfo mapRow(ResultSet rs, int rowNum) throws SQLException {
UserInfo userInfo = new UserInfo();
userInfo.setId(rs.getInt("id"));
userInfo.setUname(rs.getString("uname"));
userInfo.setUnumber(rs.getInt("unumber"));
userInfo.setuRegisterTime(rs.getDate("uregister_time"));
return userInfo;
}
}
@Autowired
private JdbcTemplate jdbcTemplate;
@Autowired
private NamedParameterJdbcTemplate namedParameterJdbcTemplate;
@Override
public UserInfo getById(Integer id) {
String sql = "SELECT * FROM user_info WHERE id = ?";
UserInfo userInfo = jdbcTemplate.queryForObject(sql, new UserInfo(),
new Object[] { id });
return userInfo;
}
@Override
public List<UserInfo> findAll() {
String sql = "SELECT * FROM user_info";
List<UserInfo> userInfos = jdbcTemplate.query(sql, new UserInfo());
return userInfos;
}
=======================================================================================
三、第三种方法
使用很少人使用的RowSet
SqlRowSet set = queryForRowSet(sql,对象参数数组);
if(set==null){
new AuthenticationException("null");
}
while(set.next){
遍历:
set.getString(此处参数类型为整形,表示你想要查出来的结果,按照顺序,依次是1、2、3.......索引从1开始)
}
注意该方法适用于:查询单个对象或者List,Map结果的情况
必须谨记若用他查出来一条数据或者一个对象时:
在判断条件的时候必须加set.next&&在跟其他条件,否则会报错"光标位置失效"
标签:smd sso acea 自定义 template auto size hash long
原文地址:http://www.cnblogs.com/lhl-shubiao/p/6774270.html