mybatis/mybatis-dynamic-sql

Support ColumnAliasPrefix for AliasableSqlTable to simplify join query mappings

Closed this issue · 2 comments

<resultMap id="joinMap" type="xxx.Main"> 
    <id property="id" column="id"/>
    <collection property="subs" ofType="xxx.Sub" 
                columnPrefix="sub_"> 
        <id property="id" column="id"/>
    </collection>
</resultMap>
// current 
MainSqlSupport.Main mainTable = MainSqlSupport.Main.withAlias("main");
SubSqlSupport.Sub subTable = SubSqlSupport.Sub.withAlias("sub");

select(mainTable.allColumns(), subTable.id.as("sub_id"),subTable.foo.as("sub_foo"),subTable.bar.as("sub_bar"),subTable.xxx.as("sub_xxx"));


// expected
MainSqlSupport.Main mainTable = MainSqlSupport.Main.withAlias("main");
SubSqlSupport.Sub subTable = SubSqlSupport.Sub.withAlias("sub").withColumnAliasPrefix("sub_");

select(mainTable.allColumns(), subTable.id,subTable.foo,subTable.bar,subTable.xxx);

This feature is also helpful for modifying column names. Only need to update the column name in xxxSqlSupport.

I understand your motivation here, but there are corner cases where it would break things (for example, an insert statement with a select).

I continue to believe that column aliases should be specified in the context of an individual select statement.

If you want a column to have a permanent alias, you can configure that currently in the xxxSqlSupport file. You would just need to test carefully to make sure it didn't break anything. Java makes this a little tricky because it doesn't propagate the generic type in chained method calls, but you can do it like this:

    SqlColumn<Integer> col = column("id", JDBCType.INTEGER);
    col = col.as("sub_id");