[英]Using a NamedParamterJdbcTemplate object to specify a mysql field and value to query on
I am currently in the process of learning the Java Spring Framework, and I am having difficulty understanding why the following query is failing to return any results from the database. 我目前正在学习Java Spring框架,并且很难理解为什么以下查询未能从数据库返回任何结果。
I am ultimately trying to create a where
method in my OffersDAO
class that allows my to query on a specific field, for a specific value. 最终,我最终尝试在
OffersDAO
类中创建一个where
方法, where
方法允许我在特定字段中查询特定值。
public List<Offer> where(String field, String value){
MapSqlParameterSource params = new MapSqlParameterSource();
params.addValue("field", field);
params.addValue("value", value);
String sql = "select * from offers where :field = :value";
return jdbc.query(sql, params, new RowMapper<Offer>(){
public Offer mapRow(ResultSet rs, int arg1) throws SQLException {
Offer offer = new Offer();
offer.setId(rs.getInt("id"));
offer.setName(rs.getString("name"));
offer.setText(rs.getString("text"));
offer.setEmail(rs.getString("email"));
return offer;
}
});
}
I am able to successfully query the database for results when I specify the field explicitly, as follows: 当我明确指定字段时,能够成功查询数据库的结果,如下所示:
String sql = "select * from offers where name = :value";
Obviously there is something wrong with specifying the field name dynamically. 显然,动态指定字段名称存在问题。 My guess is it is most likely due to the fact that the
field
key is being inserted as a mysql string (with ''
), when in fact mysql expects a column name for the :field placeholder. 我的猜测是,最有可能是由于将
field
键作为mysql字符串(带有''
)插入的事实,而事实上mysql期望:field占位符的列名。
My questions are as follows: 我的问题如下:
Is there a way to accomplish what I am attempting to do above, using the jdbc NamedParameterJdbcTemplate class? 有没有一种方法可以使用jdbc NamedParameterJdbcTemplate类来完成上述工作?
If I cannot accomplish the above, by what means can I? 如果我不能完成上述任务,我可以通过什么方式?
Thank you 谢谢
Edit: No exceptions are thrown. 编辑:不会引发异常。 In the case when I am attempting to supply the column name, a empty result set is returned.
在尝试提供列名的情况下,将返回空结果集。
You can't specify the field name in a parameter - only the field value. 您不能在参数中指定字段名称-只能指定字段值。 Since you know the DB schema when you're writing the code, this shouldn't be much of a problem.
由于您在编写代码时就知道数据库模式,因此这不是什么大问题。
What about include all possible fields in the filter but restricting their usage by field name param. 该如何在过滤器中包括所有可能的字段,但通过字段名称参数限制其使用。 Like this:
像这样:
select * from offers where
('name'=:field and name = :value)
OR
('field2'=:field and field2 = :value)
OR
('field3'=:field and field3 = :value)
I don't know how You can implement it with spring (I mean use variable column names) but I can suggest to use the following principle. 我不知道如何用spring来实现它(我的意思是使用变量列名),但是我建议使用以下原理。
Keep your query like template: 保持查询像模板一样:
String sql = "select * from offers where ##field = :value";
And every time before execution replace ##value parameter
with the column You want. 并且每次执行前,
replace ##value parameter
为所需的列。 And then You are gone. 然后你走了。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.