I’m trying to have a conditional query fragment, based upon a parameter which comes from the mybatis configuration, rather than a query parameter. Something like this:
<sql id="frag">
<if test="col_name != null">
SELECT * FROM TABLE WHERE ${col.name}=#{value}
</if>
<if test="col_name == null">
SELECT * FROM TABLE WHERE SAMPLECOL=#{value}
</if>
</sql>
where the value of col_name is a global parameter, specified inside the .properties file read by the mybatis configuration.
Apparently this does not work; looking at the source code, it seems that the OGNL expression evaluator is not aware of the configuration properties (which instead are working when I have the parameter substitution, through ${...} inside the SQL). Did anybody find a way to do this?
I found out this is not currently possible; the OGNL has effectively no access to configuration properties.
As a workaround, as suggested in this post on the mybatis mailing list, I wrote a simple interceptor which reads configuration parameters and adds them to the query parameter map. Not exactly clean, but it works.
Interceptor code:
Example usage in configuration .xml:
With this setting, I was able to test the
_issuerLocationvariable in OGNL expressions like everything else.