RE: Condition on configuration parameter

201 views
Skip to first unread message

Poitras Christian

unread,
Jul 25, 2012, 10:16:45 AM7/25/12
to mybati...@googlegroups.com

Hi,

 

You can do this using one of these two tricks.

1)      You can pass the configuration parameter to the query by adding it as a parameter to the Mapper.

2)      You can add an Interceptor that will add the value to the map that is passed to the query.

 

Christian

 

De : mybati...@googlegroups.com [mailto:mybati...@googlegroups.com] De la part de Flavio
Envoyé : July-25-12 3:25 AM
À : mybati...@googlegroups.com
Objet : Condition on configuration parameter

 

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). Is it possible to do this?

Yoel Benharrous

unread,
Jul 25, 2012, 10:19:04 AM7/25/12
to mybati...@googlegroups.com
Hello Flavio

Why you don't try :

<sql id="frag">   
   SELECT * FROM TABLE WHERE
    <if test="col_name != null">
         
${col.name}=#{value}
    </if>
    <if test="col_name == null">
        SAMPLECOL=#{value}
    </if>
</sql>


All the best,

Yoel

2012/7/25 Flavio <flavio....@gmail.com>

Flavio

unread,
Jul 25, 2012, 12:09:47 PM7/25/12
to mybati...@googlegroups.com
The Interceptor is a bit awkward but works, thank you.

Flavio


On Wednesday, July 25, 2012 4:16:45 PM UTC+2, christia...@ircm.qc.ca wrote:

Hi,

 

You can do this using one of these two tricks.

1)      You can pass the configuration parameter to the query by adding it as a parameter to the Mapper.

2)      You can add an Interceptor that will add the value to the map that is passed to the query.

 

Christian

 

De : mybati...@googlegroups.com [mailto:mybatis-user@googlegroups.com] De la part de Flavio


Envoyé : July-25-12 3:25 AM
À : mybati...@googlegroups.com
Objet : Condition on configuration parameter

Reply all
Reply to author
Forward
0 new messages