Accessing mapper field from xml

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

Accessing mapper field from xml

nclemeur
Hello,

I was wondering if there was a way to access a mapper field within the xml without having to specify the full class name + field name as @fullclassName@fieldName. So let's say I have a mapper that defined the field MY_CONSTANT like this:
package foo;

public interface MyFooMapper {
   
public String MY_CONSTANT = "mmm";

   
public List<Foo> select(@Param("params") Map<String,Object> params);
}

Then in the MyFooMapper.xml, I would like to do something like this:
<select id="select" parameterType="map">
     select * from MyTable
     
<if test="params[MY_CONSTANT]">
        ...
     
</if>    
</select>

The problem is that params[MY_CONSTANT] is not properly evaluated. I have tried a few different combination but nothing really worked other than specify the full classname as below:
<select id="select" parameterType="map">
     select * from MyTable
     
<if test="params[@foo.MyFooMapper@MY_CONSTANT]">
        ...<!-- works but cumbersome -->
     
</if>    
</select>

Ideally, something like below would be perfect, but it does not work:
<select id="select" parameterType="map">
     select * from MyTable
     
<if test="params[#this.MY_CONSTANT]">
       
     
</if>    
</select>

Any idea if something similar to this could be achieved?

Cheers

Nicolas

--
You received this message because you are subscribed to the Google Groups "mybatis-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: Accessing mapper field from xml

Iwao AVE!

Hi Nicolas,

If the purpose is to save some typing, you can declare a property in your MyBatis config…

<properties>
  <property name="myFooMapper" value="@foo.MyFooMapper@"/>
</properties>

…and use it in the statement.

<select id="select" parameterType="map">
     select * from MyTable
     <if test="params[${myFooMapper}.MY_CONSTANT]">

Hope this helps,
Iwao

2018-03-12 20:41 GMT+09:00 nclemeur <[hidden email]>:

Hello,

I was wondering if there was a way to access a mapper field within the xml without having to specify the full class name + field name as @fullclassName@fieldName. So let's say I have a mapper that defined the field MY_CONSTANT like this:
package foo;

public interface MyFooMapper {
   
public String MY_CONSTANT = "mmm";

   
public List<Foo> select(@Param("params") Map<String,Object> params);
}

Then in the MyFooMapper.xml, I would like to do something like this:
<select id="select" parameterType="map">
     select * from MyTable
     
<if test="params[MY_CONSTANT]">
        ...
     
</if>    
</select>

The problem is that params[MY_CONSTANT] is not properly evaluated. I have tried a few different combination but nothing really worked other than specify the full classname as below:
<select id="select" parameterType="map">
     select * from MyTable
     
<if test="params[@foo.MyFooMapper@MY_CONSTANT]">
        ...<!-- works but cumbersome -->
     
</if>    
</select>

Ideally, something like below would be perfect, but it does not work:
<select id="select" parameterType="map">
     select * from MyTable
     
<if test="params[#this.MY_CONSTANT]">
       
     
</if>    
</select>

Any idea if something similar to this could be achieved?

Cheers

Nicolas

--
You received this message because you are subscribed to the Google Groups "mybatis-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
For more options, visit https://groups.google.com/d/optout.

--
You received this message because you are subscribed to the Google Groups "mybatis-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: Accessing mapper field from xml

nclemeur

Yes that is helping, although I was hoping to be able to have access to "this" with "this" being the mapper instance. With default method in interface, I think this could be potentially useful in addition to my use case described in my previous email.

Cheers

Nicolas

On Thursday, 15 March 2018 22:38:40 UTC+10, Iwao AVE! wrote:

Hi Nicolas,

If the purpose is to save some typing, you can declare a property in your MyBatis config…

<properties>
  <property name="myFooMapper" value="@foo.MyFooMapper@"/>
</properties>

…and use it in the statement.

<select id="select" parameterType="map">
     select * from MyTable
     <if test="params[${myFooMapper}.MY_CONSTANT]">

Hope this helps,
Iwao

2018-03-12 20:41 GMT+09:00 nclemeur <<a href="javascript:" style="background-color:transparent;color:rgb(3,102,214);text-decoration:none" target="_blank" gdf-obfuscated-mailto="xsGRe_OiAgAJ" rel="nofollow" onmousedown="this.href=&#39;javascript:&#39;;return true;" onclick="this.href=&#39;javascript:&#39;;return true;">ncle...@...>:

Hello,

I was wondering if there was a way to access a mapper field within the xml without having to specify the full class name + field name as @fullclassName@fieldName. So let's say I have a mapper that defined the field MY_CONSTANT like this:
package foo;

public interface MyFooMapper {
   
public String MY_CONSTANT = "mmm";

   
public List<Foo> select(@Param("params") Map<String,Object> params);
}

Then in the MyFooMapper.xml, I would like to do something like this:
<select id="select" parameterType="map">
     select * from MyTable
     
<if test="params[MY_CONSTANT]">
        ...
     
</if>    
</select>

The problem is that params[MY_CONSTANT] is not properly evaluated. I have tried a few different combination but nothing really worked other than specify the full classname as below:
<select id="select" parameterType="map">
     select * from MyTable
     
<if test="params[@foo.MyFooMapper@MY_CONSTANT]">
        ...<!-- works but cumbersome -->
     
</if>    
</select>

Ideally, something like below would be perfect, but it does not work:
<select id="select" parameterType="map">
     select * from MyTable
     
<if test="params[#this.MY_CONSTANT]">
       
     
</if>    
</select>

Any idea if something similar to this could be achieved?

Cheers

Nicolas

--
You received this message because you are subscribed to the Google Groups "mybatis-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to <a href="javascript:" target="_blank" gdf-obfuscated-mailto="xsGRe_OiAgAJ" rel="nofollow" onmousedown="this.href=&#39;javascript:&#39;;return true;" onclick="this.href=&#39;javascript:&#39;;return true;">mybatis-user...@googlegroups.com.
For more options, visit <a href="https://groups.google.com/d/optout" target="_blank" rel="nofollow" onmousedown="this.href=&#39;https://groups.google.com/d/optout&#39;;return true;" onclick="this.href=&#39;https://groups.google.com/d/optout&#39;;return true;">https://groups.google.com/d/optout.

--
You received this message because you are subscribed to the Google Groups "mybatis-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
For more options, visit https://groups.google.com/d/optout.