我在使用 Spring 3 返回多个值的查询时遇到问题。
我使用它来检索值,但从数据库返回的 TemplateFlow
对象始终为 null(所有字段都包含 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
要求是这样的:
Column values are mapped based on matching the column name as obtained from result set metadata to public setters for the corresponding properties.
例如,您应该有
int getId_templateflow()
void setId_templateflow(int)
而不是
int id_templateflow()
void id_templateflow(int)
但是,我建议完全不要使用 ParameterizedBeanPropertyRowMapper
- 它将数据库与代码耦合得太紧密,这不是一件好事。考虑编写您自己的 RowMapper
实现。
关于java - 查询返回多条记录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5855722/