mybatis generator: can we use ${alias} parameters

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

mybatis generator: can we use ${alias} parameters

Ilko
Hi,

is it possible to configure the generator to generate the alias as parameter as in the mybatis example:

<sql id="Base_Column_List">
    <!--
      WARNING - @mbggenerated
      This element is automatically generated by MyBatis Generator, do not modify.
      This element was generated on Tue Jan 26 16:25:45 CET 2016.
    -->

    ${alias}.COL1, ${alias}.COL2, ...

 </sql>

As far as I can see mybatis does not treat the ${alias} as optional at the moment and all the generated sql statements must provide valid alias in the include element, so either this should be done in the generator as well or mybatis could be extended to treat the parameter as optional - if not provided the COL1, COL2 should be used without prefix.
This will ensure single point of change if the underlying tables are changed automatically - the Base_Column_List definition can be used very flexible in all queries.
I saw the alias attribute in the table element but this works statically at the moment.

Any ideas how this can be accomplished?

TIA,
Ilko

--
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: mybatis generator: can we use ${alias} parameters

nclemeur

Yes, I agree that going this path would be potentially very useful. You could imagining using different aliases when joining the same table twice and still be able to reuse the Base Column List. Also, you could think of extending the generated Example classes to have an alias as a constructor argument. We would then only need an api selectByExample that could take a "smart" example class that now how to combine multiple example together based on aliases. I know this is not really clear, but I think if we could create SQL query with JOINS and other more complex statement using the generated columns lists and examples would be a real plus for the generator...

Nicolas


On Wednesday, 2 March 2016 03:28:04 UTC+10, Ilko wrote:
Hi,

is it possible to configure the generator to generate the alias as parameter as in the mybatis example:

<sql id="Base_Column_List">
    <!--
      WARNING - @mbggenerated
      This element is automatically generated by MyBatis Generator, do not modify.
      This element was generated on Tue Jan 26 16:25:45 CET 2016.
    -->

    ${alias}.COL1, ${alias}.COL2, ...

 </sql>

As far as I can see mybatis does not treat the ${alias} as optional at the moment and all the generated sql statements must provide valid alias in the include element, so either this should be done in the generator as well or mybatis could be extended to treat the parameter as optional - if not provided the COL1, COL2 should be used without prefix.
This will ensure single point of change if the underlying tables are changed automatically - the Base_Column_List definition can be used very flexible in all queries.
I saw the alias attribute in the table element but this works statically at the moment.

Any ideas how this can be accomplished?

TIA,
Ilko

--
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: mybatis generator: can we use ${alias} parameters

Ilko
sorry for the late reply - yes, I'm struggling already with this limitation - any ideas how can we proceed here - I can imagine I can invest some time to contribute here. At the very beginning I think it should be clarified if this could / should be done as plugin in the generator or it is not possible and should be in the generator itself?
Some guidance in any case will be very helpful.


On Wednesday, March 16, 2016 at 3:00:31 PM UTC+1, nclemeur wrote:

Yes, I agree that going this path would be potentially very useful. You could imagining using different aliases when joining the same table twice and still be able to reuse the Base Column List. Also, you could think of extending the generated Example classes to have an alias as a constructor argument. We would then only need an api selectByExample that could take a "smart" example class that now how to combine multiple example together based on aliases. I know this is not really clear, but I think if we could create SQL query with JOINS and other more complex statement using the generated columns lists and examples would be a real plus for the generator...

Nicolas


On Wednesday, 2 March 2016 03:28:04 UTC+10, Ilko wrote:
Hi,

is it possible to configure the generator to generate the alias as parameter as in the mybatis example:

<sql id="Base_Column_List">
    <!--
      WARNING - @mbggenerated
      This element is automatically generated by MyBatis Generator, do not modify.
      This element was generated on Tue Jan 26 16:25:45 CET 2016.
    -->

    ${alias}.COL1, ${alias}.COL2, ...

 </sql>

As far as I can see mybatis does not treat the ${alias} as optional at the moment and all the generated sql statements must provide valid alias in the include element, so either this should be done in the generator as well or mybatis could be extended to treat the parameter as optional - if not provided the COL1, COL2 should be used without prefix.
This will ensure single point of change if the underlying tables are changed automatically - the Base_Column_List definition can be used very flexible in all queries.
I saw the alias attribute in the table element but this works statically at the moment.

Any ideas how this can be accomplished?

TIA,
Ilko

--
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: mybatis generator: can we use ${alias} parameters

Iwao AVE!
Hi guys,

Just an idea, but how about 1) omitting 'alias' attribute of 'table'
element in the generator config file and 2) adding ${alias} to
Base_Column_List using a custom plugin?

${alias}col1, ${alias}col2, ...

All the generated statements, example classes reference columns
without alias and will work fine.

Here's an example implementation of the plugin.
https://gist.github.com/harawata/d29e49f2c75c5500ae0acf19b6a7d090

Regards,
Iwao

2016-04-01 20:59 GMT+09:00 Ilko <[hidden email]>:

> sorry for the late reply - yes, I'm struggling already with this limitation
> - any ideas how can we proceed here - I can imagine I can invest some time
> to contribute here. At the very beginning I think it should be clarified if
> this could / should be done as plugin in the generator or it is not possible
> and should be in the generator itself?
> Some guidance in any case will be very helpful.
>
>
>
> On Wednesday, March 16, 2016 at 3:00:31 PM UTC+1, nclemeur wrote:
>>
>>
>> Yes, I agree that going this path would be potentially very useful. You
>> could imagining using different aliases when joining the same table twice
>> and still be able to reuse the Base Column List. Also, you could think of
>> extending the generated Example classes to have an alias as a constructor
>> argument. We would then only need an api selectByExample that could take a
>> "smart" example class that now how to combine multiple example together
>> based on aliases. I know this is not really clear, but I think if we could
>> create SQL query with JOINS and other more complex statement using the
>> generated columns lists and examples would be a real plus for the
>> generator...
>>
>> Nicolas
>>
>>
>> On Wednesday, 2 March 2016 03:28:04 UTC+10, Ilko wrote:
>>>
>>> Hi,
>>>
>>> is it possible to configure the generator to generate the alias as
>>> parameter as in the mybatis example:
>>>
>>> <sql id="Base_Column_List">
>>>     <!--
>>>       WARNING - @mbggenerated
>>>       This element is automatically generated by MyBatis Generator, do
>>> not modify.
>>>       This element was generated on Tue Jan 26 16:25:45 CET 2016.
>>>     -->
>>>
>>>     ${alias}.COL1, ${alias}.COL2, ...
>>>
>>>  </sql>
>>>
>>> As far as I can see mybatis does not treat the ${alias} as optional at
>>> the moment and all the generated sql statements must provide valid alias in
>>> the include element, so either this should be done in the generator as well
>>> or mybatis could be extended to treat the parameter as optional - if not
>>> provided the COL1, COL2 should be used without prefix.
>>> This will ensure single point of change if the underlying tables are
>>> changed automatically - the Base_Column_List definition can be used very
>>> flexible in all queries.
>>> I saw the alias attribute in the table element but this works statically
>>> at the moment.
>>>
>>> Any ideas how this can be accomplished?
>>>
>>> TIA,
>>> Ilko

--
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: mybatis generator: can we use ${alias} parameters

Ilko
I'll give it a try on Monday. However, if the memory serves me well I think that the generated  selectById statements are referencing the Base_Column_List. Nevertheless, I can use the idea as a starting point - I think we could create a copy of the whole element as an Aliased_Base_Column_List...
Thanks for sharing the idea.

--
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: mybatis generator: can we use ${alias} parameters

Iwao AVE!
Hi Ilko,

Yes, some generated select statements reference Base_Column_List, but
it shouldn't be a problem because ${alias} will be an empty string if
there is no property defined.

So, for example, with a Base_Column_List like this

<sql id="Base_Column_List">
${alias}col1, ${alias}col2
</sql>

, generated select such as...

select
<include refid="Base_Column_List" />
from ...

will execute...

select col1, col2 from ...

In reality, though, Base_Column_List may need column aliases as well.

<sql id="Base_Column_List">
${alias}col1 as ${prefix}col1, ${alias}col2 as ${prefix}co2
</sql>

Let us know how it goes :-)

Cheers,
Iwao


2016-04-03 5:24 GMT+09:00 Ilko <[hidden email]>:
> I'll give it a try on Monday. However, if the memory serves me well I think that the generated  selectById statements are referencing the Base_Column_List. Nevertheless, I can use the idea as a starting point - I think we could create a copy of the whole element as an Aliased_Base_Column_List...
> Thanks for sharing the idea.
>
> --
> 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: mybatis generator: can we use ${alias} parameters

Ilko
Hi,

I've posted my version as reply to your gist - I've ended with second element as copy of the Base_Column_List. I think that it is better suited to be added to the BaseColumnListGenerator, which I've used in the plugin...

as I wrote improvements are highly appreciated because it is my first plugin ...

On Sunday, April 3, 2016 at 1:29:19 AM UTC+2, Iwao AVE! wrote:
Hi Ilko,

Yes, some generated select statements reference Base_Column_List, but
it shouldn't be a problem because ${alias} will be an empty string if
there is no property defined.

So, for example, with a Base_Column_List like this

<sql id="Base_Column_List">
${alias}col1, ${alias}col2
</sql>

, generated select such as...

select
<include refid="Base_Column_List" />
from ...

will execute...

select col1, col2 from ...

In reality, though, Base_Column_List may need column aliases as well.

<sql id="Base_Column_List">
${alias}col1 as ${prefix}col1, ${alias}col2 as ${prefix}co2
</sql>

Let us know how it goes :-)

Cheers,
Iwao


2016-04-03 5:24 GMT+09:00 Ilko <<a href="javascript:" target="_blank" gdf-obfuscated-mailto="MxjYf4mSBwAJ" rel="nofollow" onmousedown="this.href=&#39;javascript:&#39;;return true;" onclick="this.href=&#39;javascript:&#39;;return true;">ilkom...@...>:
> I'll give it a try on Monday. However, if the memory serves me well I think that the generated  selectById statements are referencing the Base_Column_List. Nevertheless, I can use the idea as a starting point - I think we could create a copy of the whole element as an Aliased_Base_Column_List...
> Thanks for sharing the idea.
>
> --
> 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="MxjYf4mSBwAJ" 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.
Reply | Threaded
Open this post in threaded view
|

Re: mybatis generator: can we use ${alias} parameters

Nicola Menegazzo
Hi guys, sorry for posting on a such old thread but I wasn't able to find a solution elsewhere...

Do new versions of myBatis allow managing prefixes in a native and optimal way? I got some myBatis Generator "column_list"s that I need to prefix because of joined tables field ambiguity.
I think it should be quite straightforward to add a parameter to an include statement to automatically prefix all listed columns.

A possibile example:

  <sql id="Base_Column_List">
    id, code, name, address1, address2,
    city_id, city_name, country_code, postal_code, latitude, longitude
  </sql>

then

<include item_prefix="columnPrefix." refid="Base_Column_List" />

would generate

columnPrefix.id,
columnPrefix.code,
columnPrefix.name,
columnPrefix.address1,
columnPrefix.address2,
columnPrefix.city_id,
columnPrefix.city_name,
columnPrefix.country_code,
columnPrefix.postal_code,
columnPrefix.latitude,
columnPrefix.longitude

above all, without the need to modify the original <sql id="Base_Column_List">.


Hope someone is listening on the other side :)
Thanks anyway

Regards


Il giorno martedì 5 aprile 2016 14:25:59 UTC+2, Ilko ha scritto:
Hi,

I've posted my version as reply to your gist - I've ended with second element as copy of the Base_Column_List. I think that it is better suited to be added to the BaseColumnListGenerator, which I've used in the plugin...

as I wrote improvements are highly appreciated because it is my first plugin ...

On Sunday, April 3, 2016 at 1:29:19 AM UTC+2, Iwao AVE! wrote:
Hi Ilko,

Yes, some generated select statements reference Base_Column_List, but
it shouldn't be a problem because ${alias} will be an empty string if
there is no property defined.

So, for example, with a Base_Column_List like this

<sql id="Base_Column_List">
${alias}col1, ${alias}col2
</sql>

, generated select such as...

select
<include refid="Base_Column_List" />
from ...

will execute...

select col1, col2 from ...

In reality, though, Base_Column_List may need column aliases as well.

<sql id="Base_Column_List">
${alias}col1 as ${prefix}col1, ${alias}col2 as ${prefix}co2
</sql>

Let us know how it goes :-)

Cheers,
Iwao


2016-04-03 5:24 GMT+09:00 Ilko <[hidden email]>:
> I'll give it a try on Monday. However, if the memory serves me well I think that the generated  selectById statements are referencing the Base_Column_List. Nevertheless, I can use the idea as a starting point - I think we could create a copy of the whole element as an Aliased_Base_Column_List...
> Thanks for sharing the idea.
>
> --
> 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 mybatis-user...@googlegroups.com.
> For more options, visit <a href="https://groups.google.com/d/optout" rel="nofollow" target="_blank" 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].
To view this discussion on the web visit https://groups.google.com/d/msgid/mybatis-user/46a5fc04-f5ab-4652-a419-74416c4b7965%40googlegroups.com.