【问题标题】:Spring Like clauseSpring Like 子句
【发布时间】:2011-03-15 21:05:24
【问题描述】:

我正在尝试使用 MapSqlParameterSource 创建使用 Like 子句的查询。

代码是这样的。包含它的函数接收 nameParam:

String namecount = "SELECT count(*) FROM People WHERE LOWER(NAME) LIKE :pname ";

String finalName= "'%" +nameParam.toLowerCase().trim() + "%'";

MapSqlParameterSource namedParams= new MapSqlParameterSource();

namedParams.addValue("pname", finalName);

int count= this.namedParamJdbcTemplate.queryForInt(namecount, namedParams);

这不能正常工作,当我应该收到数千个结果时,给我的结果介于 0-10 之间。我基本上希望最终查询看起来像:

SELECT count(*) FROM People WHERE LOWER(NAME) LIKE '%name%'

但这显然没有发生。任何帮助将不胜感激。

编辑:

我也尝试过将 '%'s 放在 SQL 中,例如

 String finalName= nameParam.toLowerCase().trim();

 String namecount = "SELECT count(*) FROM People WHERE LOWER(NAME) LIKE '%:pname%' "

;

但这也不起作用。

【问题讨论】:

    标签: java spring jdbc


    【解决方案1】:

    您是否尝试将 % 通配符放在您的 sql 字符串中(而不是绑定变量值本身):

    String finalName= nameParam.toLowerCase().trim();
    String namecount = "SELECT count(*) FROM People WHERE LOWER(NAME) LIKE '%:finalName%'";
    

    【讨论】:

    • 您能否建议一种修改现有代码的方法,以便我可以使用“?”占位符?我不相信您可以将它与 MapSqlParameterSource 一起使用。谢谢
    • 我试过了。它给了我相似(可能相同)的结果。我认为单引号或“%”符号一定有什么东西干扰了查询。
    • 你不能用 % 保留变量,所以你现在需要: String finalName= nameParam.toLowerCase().trim();通配符不会从 finalName 变量中删除。更新了我的答案。
    • 谢谢,我看到我的编辑不清楚,但我想反映的是,我基本上已经尝试过你的建议,但它仍然不起作用。您还有其他建议吗?
    【解决方案2】:

    您不希望在 finalName 字符串周围加上引号。使用命名参数,您无需指定它们。这应该有效:

    String namecount = "SELECT count(*) FROM People WHERE LOWER(NAME) LIKE :pname ";
    String finalName= "%" + nameParam.toLowerCase().trim() + "%";
    
    MapSqlParameterSource namedParams= new MapSqlParameterSource();
    namedParams.addValue("pname", finalName);
    
    int count= this.namedParamJdbcTemplate.queryForInt(namecount, namedParams);
    

    【讨论】:

    • 你是我的英雄。非常感谢。我已经为此工作了很长时间。
    • 将 % 符号连接到参数中与直接将其放入 sql 查询有什么好处? SELECT count(*) FROM People WHERE LOWER(NAME) LIKE '%'+:pname+'%'
    【解决方案3】:

    我们可以使用简单的 JdbcTemplate 代替 NamedParamJdbcTemplate

    String namecount = "SELECT count(*) FROM People WHERE LOWER(NAME) LIKE ? ";
    
    String finalName= "%" +nameParam.toLowerCase().trim() + "%"; //Notes: no quote
    
    getJdbcTemplate().queryForInt(namecount, new Object[] {finalName});
    

    希望对使用 JdbcTemplate 的人有所帮助

    【讨论】:

      【解决方案4】:

      这个解决方案对我有用。我将“%”放在 Object[] 参数列表中:

          String sqlCommand = "SELECT customer_id, customer_identifier_short, CONCAT(RTRIM(customer_identifier_a),' ', RTRIM(customer_identifier_b)) customerFullName "
              + " FROM Customer "
              + " WHERE customer_identifier_short LIKE ? OR customer_identifier_a LIKE ? "
              + " LIMIT 10";
      
      List<Customer> customers = getJdbcTemplate().query(sqlCommand, new Object[] { query + "%", query + "%"}, new RowMapper<Customer>() {
      
          public Customer mapRow(ResultSet rs, int i) throws SQLException {
      
              Customer customer = new Customer();
              customer.setCustomerFullName(rs.getString("customerFullName"));
              customer.setCustomerIdentifier(rs.getString("customer_identifier_short"));
              customer.setCustomerID(rs.getInt("customer_id"));                   
      
              return customer;
          }
      });
      
      return customers;
      

      【讨论】:

        猜你喜欢
        • 2011-08-28
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-07-28
        • 2022-01-08
        • 2013-06-30
        相关资源
        最近更新 更多