-
Notifications
You must be signed in to change notification settings - Fork 12.9k
includeColumns element for select #136
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
Thank you for the suggestion! You should be able to achieve what you want by using the bind tag. <sql id="cols">
${tbl}a as ${prefix}a,
${tbl}b as ${prefix}b,
${tbl}c as ${prefix}c
</sql>
<select>
<bind name="tbl" value="'comment.'" />
<bind name="prefix" value="'comment_'" />
select
<include refid="cols"/>
from tab comment
</select> It's much more flexible, isn't it? Regards, |
Thank you for your remark. I hadn't considered using the tag. However, mapping all columns in the generated resultMap to the generated table objects is (I think) so common that I think it would warrant its own simple syntax. Also, if I have more than one generated column list (because I map more than one table object), they will both be generated with ${tbl} and ${col} (which makes it impossible to set different values). We could generate a different tbl and col per table, but that doesn't solve the issue where you'd want to link the same table object more than once. Greetings, |
Hi Jasper,
I presumed that the column lists are generated using MyBatis Generator with a custom plugin, is that correct?
True, but this use case would not be so common. Adding a new tag is a big deal to MyBatis and the use case seems to be too limited as I already wrote. And thank you again for your time! |
Hi Guys, I am not sure if this can help, but if you use mybatis-velocity you can Cheers, Frank. On Wed, Jan 29, 2014 at 8:20 PM, Iwao AVE! [email protected] wrote:
Frank D. Martínez M. |
Thank you @mnesarco for the hint! @litpho, Have you tried Frank's idea? The directive name is columnalias and it takes two parameters (tableAlias and prefix). <sql id="cols">a, b, c</sql>
<select lang="velocity">
select
#columnalias("comment", "comment_")
<include refid="cols" />
#end
from tab comment
</select> And it generates the following SQL: select
comment.a AS comment_a, comment.b AS comment_b, comment.c AS comment_c
from tab comment To use it,
Please see the mybatis-velocity doc for the details: Let us know what you think! Regards, |
Thank you all for your thoughts on this matter and my apologies for not getting back sooner. I think the solution @harawata came up with will serve our needs very well, so as far as I'm concerned this pull request can be closed. Once again, thanks for considering and I hope to be able to support Mybatis in the future again. Greetings, |
@litpho Thank you very much for the feedback! (and sorry for a late reply) |
Collections and assocations have the option to specify a columnPrefix, making resultMaps easier to reuse.
However, column lists such as the Base_Column_List generated by Mybatis Generator don't have that option.
I created a separate includeColumns element which takes a tableAlias and an optional columnAliasPrefix. I chose not to fold this functionality into the generic include element, because it's only useful for very specific cases.
If you have the following column list:
and you include it in the select statement as follows:
it will render (on one line to save space):
with an optional columnAliasPrefix "c" it will render:
This functionality will make reuse of column lists much easier, especially when working with associations or when linking the same table more than once in the same query.