简体   繁体   English

使用NamedParamterJdbcTemplate对象指定要查询的mysql字段和值

[英]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: 我的问题如下:

  1. Is there a way to accomplish what I am attempting to do above, using the jdbc NamedParameterJdbcTemplate class? 有没有一种方法可以使用jdbc NamedParameterJdbcTemplate类来完成上述工作?

  2. 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.

相关问题 如何指定一个字段,以便在查询时使用字段的Joda LocalDate或LocalDateTime值将其映射到同一列? - How to specify a field, so that when we query, using Joda LocalDate or LocalDateTime value for the field, it is mapped to same column? 使用Java按数组对象字段值查询记录的Mongo - Mongo Query of Record by Array Object Field Value Using Java 如何在Java中以紧凑的方式指定复杂对象的字段值? - How to specify in Java the value of the field for complex object in a compact way? Java中的MySQL:在varbinary字段上使用LIKE查询? - MySQL in Java: using a LIKE query on a varbinary field? MongoDB使用Java使用正则表达式查询字段中的值 - MongoDB query for value in field with regex using Java 在MySQL中的SELECT查询中使用JTextBox中的值 - Using value from a JTextBox in SELECT query in mysql 如何通过选择异物字段的值来构建查询 - How to build query with selecting by value of foreign object's field Play Framework 2 Ebean指定字段的默认值 - Play Framework 2 Ebean specify default value for field 使用变量指定R对象 - Using variable to specify the R object 如何使用Javafx应用程序更改MySql数据库特定字段的值? - How to change value of specific field of MySql database using Javafx application?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM