global variables

classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|

global variables

bokie
Hello,

Is this suppose to work?

  configuration.getVariables().put("global_var1", "12345");

then in Xmapper.xml:

  select a,b,c from t1 where b = ${global_var1}

According to this post in stackoverflow.com it should:
http://stackoverflow.com/questions/8392741/mybatis-defining-a-global-parameter/8422082#8422082

however,  ${global_var1} always resolves to null, any ideas?

Thanks,
Jorge
Reply | Threaded
Open this post in threaded view
|

Re: global variables

Eduard Gopp
Never used put probably #{global_var1}?

2011/12/20 Bokie <[hidden email]>
Hello,

Is this suppose to work?

 configuration.getVariables().put("global_var1", "12345");

then in Xmapper.xml:

 select a,b,c from t1 where b = ${global_var1}

According to this post in stackoverflow.com it should:
http://stackoverflow.com/questions/8392741/mybatis-defining-a-global-parameter/8422082#8422082

however,  ${global_var1} always resolves to null, any ideas?

Thanks,
Jorge

Reply | Threaded
Open this post in threaded view
|

Re: global variables

bokie
Thanks Eduard for trying but I already tried that and it didn't work.

The way I thought it worked was #{} and ${} where resolved from
different contexts:
 i) #{} = for params passed in via parameterType or parameterMap
ii) ${} = for params/vars passed in via configuration set on
SqlSessionfactory

I don't think (ii) works the way I've mentioned here unless I' missing
something.
The the docs do mention string substitution and using the ${} notation
but I can't figure out from the docs where to set these variables.

From the online docs...

String Substitution

By default, using the #{} syntax will cause MyBatis to generate
PreparedStatement properties and set the values safely against the
PreparedStatement parameters (e.g. ?). While this is safer, faster and
almost always preferred, sometimes you just want to directly inject a
string unmodified into the SQL Statement. For example, for ORDER BY,
you might use something like this:

ORDER BY ${columnName}

Thanks
Jorge

On Dec 21, 10:40 am, Eduard Gopp <[hidden email]> wrote:

> Never used put probably #{global_var1}?
>
> 2011/12/20 Bokie <[hidden email]>
>
>
>
>
>
>
>
> > Hello,
>
> > Is this suppose to work?
>
> >  configuration.getVariables().put("global_var1", "12345");
>
> > then in Xmapper.xml:
>
> >  select a,b,c from t1 where b = ${global_var1}
>
> > According to this post in stackoverflow.com it should:
>
> >http://stackoverflow.com/questions/8392741/mybatis-defining-a-global-...
>
> > however,  ${global_var1} always resolves to null, any ideas?
>
> > Thanks,
> > Jorge
Reply | Threaded
Open this post in threaded view
|

Re: global variables

Eduardo Macarron
In reply to this post by bokie
Jorge, make sure that config.getVariables().put() is called befor the
mapper xml files are parsed.

I do not know if this behaviour is intentional but varable replacement
work for mybatis-config.xml and also form mappers but just during
loading time.

On 20 dic, 20:31, Bokie <[hidden email]> wrote:

> Hello,
>
> Is this suppose to work?
>
>   configuration.getVariables().put("global_var1", "12345");
>
> then in Xmapper.xml:
>
>   select a,b,c from t1 where b = ${global_var1}
>
> According to this post in stackoverflow.com it should:http://stackoverflow.com/questions/8392741/mybatis-defining-a-global-...
>
> however,  ${global_var1} always resolves to null, any ideas?
>
> Thanks,
> Jorge
Reply | Threaded
Open this post in threaded view
|

Re: global variables

bokie
Thanks Eduardo,

That suggestion worked.

Thanks again,
Jorge

On Dec 21, 7:14 pm, Eduardo <[hidden email]> wrote:

> Jorge, make sure that config.getVariables().put() is called befor the
> mapper xml files are parsed.
>
> I do not know if this behaviour is intentional but varable replacement
> work for mybatis-config.xml and also form mappers but just during
> loading time.
>
> On 20 dic, 20:31, Bokie <[hidden email]> wrote:
>
>
>
>
>
>
>
> > Hello,
>
> > Is this suppose to work?
>
> >   configuration.getVariables().put("global_var1", "12345");
>
> > then in Xmapper.xml:
>
> >   select a,b,c from t1 where b = ${global_var1}
>
> > According to this post in stackoverflow.com it should:http://stackoverflow.com/questions/8392741/mybatis-defining-a-global-...
>
> > however,  ${global_var1} always resolves to null, any ideas?
>
> > Thanks,
> > Jorge