查询返回的多个记录
问题描述:
我有一个问题与使用Spring 3查询返回的多个记录
我使用这个检索值返回多个值的查询,但TemplateFlow
对象返回从DB总是空(所有字段包含null
或默认值)。
List<TemplateFlow> flows = (List<TemplateFlow>) getJdbcTemplate().query(
sqlString,
ParameterizedBeanPropertyRowMapper.newInstance(TemplateFlow.class)
);
TemplateFlow
是一个包含所有字段的类。我在更新后检索一些值,是否有可能需要提交更改? (但我没有使用任何交易)。
public class TemplateFlow {
private int id_templateflow;
private int id_templateprocess;
public int id_templateflow() { return this.id_templateflow; }
public void id_templateflow(int id_templateflow) { this.id_templateflow = id_templateflow; }
public int id_templateprocess() { return this.id_templateprocess; }
public void id_templateprocess(int id_templateprocess) { this.id_templateprocess = id_templateprocess; }
}
我试图直接在数据库上运行查询它返回两行。
感谢您的帮助! 安德烈
答
你TemplateFlow
类不符合JavaBean的图案,并且ParameterizedBeanPropertyRowMapper
需要此是这种情况:
列值是基于作为从结果集的元数据获得公共匹配列名映射设置相应的属性。
例如,你应该有
int getId_templateflow()
void setId_templateflow(int)
代替
int id_templateflow()
void id_templateflow(int)
不过,我建议不要使用ParameterizedBeanPropertyRowMapper
在所有 - 这将你的数据库太紧你的代码,这不是一件好事。请考虑编写您自己的RowMapper
实现。
+1
嗨,是啊!我创建了自己的RowMapper类并以这种方式调用:List
当您在数据库的查询工具中使用您正在使用的参数运行查询时会发生什么? – lobster1234 2011-05-02 09:54:17