how to pass multiple values to query using jdbcTemplate in spring

情到浓时终转凉″ 提交于 2019-12-23 19:05:39

问题


In my Spring Hibernate application i have all the sql queries in one common_queries.xml file,where some queries require 2 to 3 parameters shown as below

   <query id="mining.fuel" no-of-params="2">
select ms.id id,ms.name value,concat(ms.name,' ','  (',ms.code,')') label,ms.rate rate     from mining_fuel ms where ms.name like '?' and ms.fuel_type_id=?  LIMIT 10
 </query>   

In my daoImpl i get this query

lookupList = jdbcTemplate.queryForList(q1.getQuery());

I will get the query here,but how to pass the value of '?'s here, i have those 2 values with me in daoImpl.. pl send the code of how to achieve this.I dont want to use prepared statement.


回答1:


Use this overload which takes an Object vararg for passing the query parameters:

lookupList = jdbcTemplate.queryForList(q1.getQuery(), value1, value2, value3);



回答2:


I think that you only need to create an Object array with the params used by the query, take in mind that the order is important because value1 will be the first replacement for ? in the query.

lookupList = jdbcTemplate.queryForList(q1.getQuery(), new Object[]{value1, value2, value3});



回答3:


First calls the queryForList method on jdbctemplate reference and pass query and object type array in this object type array we must pass only objects means if we have id it is int type we have to convert to object type while putting inside object array.

lookupList = jdbcTemplate.queryForList(q1.getQuery, new Object[]{designation, new Integer(id), new Float(sal)}



来源:https://stackoverflow.com/questions/7009709/how-to-pass-multiple-values-to-query-using-jdbctemplate-in-spring

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!