You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user-java@ibatis.apache.org by XU Qiang <q....@cofinoga.fr> on 2005/06/24 10:01:16 UTC

Parameterized mapped-statement in iBatis

Hi, 
In Rick Reumann's site http://reumann.net/struts/ibatisLesson1/step5.do
<http://reumann.net/struts/ibatisLesson1/step5.do>  , he describes a common
use case for a query of list  (value , label)  from a set of different
tables. The sample map uses a list of very similar mapped-statement to
populate a result-map. The reuse here is only the result-map. Is there
someway to not list the queries but just parameterize mapped-statement with
table name as parameter ? I am new in using ibatis, so any suggestion and
pointer is valuable.
Thank you 
Qiang 


Re: Parameterized mapped-statement in iBatis

Posted by Larry Meadors <la...@gmail.com>.
Yes, if your tables all have an id field and a description fields, you
could use $tableName$ in the from part of your SQL:

<select id="getNameValueList">
select 
  description as name, 
  id as value
from $tableName$
</select>

Be careful doing this, because it is open to SQL injection attacks.

If you wanted, you could even do this (if your tables did not have all
the same id/description fields):

<select id="getNameValueList">
select 
  $descriptionField$ as name, 
  $idField$ as value
from $tableName$
</select>

That would let you grab any two fields (or rather field expressions)
from any table.

Nice trick.

HTH,
Larry


On 6/24/05, XU Qiang <q....@cofinoga.fr> wrote:
>  
> 
> Hi, 
> In Rick Reumann's site
> http://reumann.net/struts/ibatisLesson1/step5.do , he
> describes a common use case for a query of list  (value , label)  from a set
> of different tables. The sample map uses a list of very similar
> mapped-statement to populate a result-map. The reuse here is only the
> result-map. Is there someway to not list the queries but just parameterize
> mapped-statement with table name as parameter ? I am new in using ibatis, so
> any suggestion and pointer is valuable. 
> 
> Thank you 
> Qiang