IBATIS - inserting literals (removing quotes from your sql)

SQL, Development, Work — Chris on April 26, 2008 at 11:49 pm

When using IBATIS, use $parameter$ to inject literals in your sql maps. For instance, if you have a sql query that injects a comma separated list of ids, using #parameter# will put quotes in the sql, causing an error if you were using the “in” operator. Using $parameter$ removes the quotes and your sql is content.

Example with #:

select filter.id from filter where filtertypeid = #filterTypeId#
and typeid in (#typeIds#);

could yield

select filter.id from filter where filtertypeid = '2' and typeid in ('0,1,4');

Example with $:

select filter.id from filter where filtertypeid = #filterTypeId#
and typeid in ($typeIds$);

could yield

select filter.id from filter where filtertypeid = '2' and typeid in (0,1,4);

Conclusion:

Remember to use your dollar signs.

This work is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 2.5 License. | Chris Fierer