title: How to search string LIKE 'something%' with Java Spring Framework?
body: <p>I've got a MySQL table with Foos. Each Foo has a numeric non-unique code and a name. Now I need to find if any Foo with one of certain codes happens to have a name that starts with a given string. In normal SQL this would be trivial:</p>
  
  <pre class="lang-sql prettyprint-override"><code>select * from FOO where CODE in (2,3,5) and NAME like 'bar%';
  </code></pre>
  
  <p>But how would I properly do this in Spring now? Without the need for the 'like' operator I'd do it like this:</p>
  
  <pre class="lang-java prettyprint-override"><code>public List&lt;Foo&gt; getByName(List&lt;Integer&gt; codes, String namePart) {
      String sql = "select * from FOO where CODE in (:codes) and NAME=:name"
      Map&lt;String,Object&gt; params = new HashMap&lt;String,Object&gt;();
      params.put("codes", codes);
      params.put("name", namePart);
      return getSimpleJdbcTemplate().query(sql, new FooRowMapper(), params);
  }
  </code></pre>
  
  <p>However, with 'like' nothing seems to work: <code>NAME like :name%</code>, <code>NAME like ':name%'</code>, or <code>NAME like ?%</code> when using the placeholders instead of named parameters.</p>
  
  <p>I could be brutal and enter it as </p>
  
  <pre class="lang-java prettyprint-override"><code>String sql = "select * from FOO where CODE in (:codes) and NAME like '"+namePart+"%'";` 
  </code></pre>
  
  <p>but obviously it would be more than nice if Spring would sanitize the input parameters properly etc, you know...</p>
  
  <p>You'd think Spring would support this somehow but I cannot figure it out.</p>