mybatis/mybatis-dynamic-sql

How to set the case of column names when the return value is hashmap?

mmm8955405 opened this issue · 5 comments

111
222
444

There are some methods that may be possible to achieve:
https://stackoverflow.com/questions/11720856/mybatis-3-1-1-how-to-override-the-resultmap-returned-from-mybatis/11732674#11732674

A universal case hump conversion:
555

If you need to manually map the return values of multiple complex queries for each field, the workload is really too high. If you modify the database field name, there are too many mapping areas that the program needs to modify.

Directly return the field name of the database as a JSON name to the front-end, and in the case of MySQL, it will automatically become uppercase.

The following requirements:

The field name needs to be changed to lowercase, without any conversion related to the slider;

The field name needs to be capitalized without any conversion related to the slider;

Require lowercase and hump

Require capital and hump

Any solution?

The stack overflow question you refer to is a good explanation of the problem, The default case of returned column names is database dependent and there is no consistency in how databases handle this.

You can try to modify the query so that it returns the columns in the case you want by escaping the alias...

select FIRST_NAME as `firstName`
from foo

But even this method of escaping a column name is non-standard and inconsistent across databases.

The best option is to use a MyBatis result map if possible. Otherwise you will need to write some kind of utility that can read the HashMap of names in your preferred style.

Do you consider providing methods to support case conversion of column names or camel hump naming. I believe this is necessary. Can improve developers' development speed.

select FIRST_NAME as firstName from foo

Something like .......
BasicColumn[] basicColumns= BasicColumn.columnList(r.allColumns());
toLow(basicColumns)
toUp(basicColumns)
toCamel(basicColumns)

Sorry, I only saw your reply today. Long hours of work have deprived me of time to come and see the information

SqlColumn already has a utility method asCamelCase that might help...

select(id.asCamelCase())
    .from(foo)
    .build()
    .render(RenderingStrategies.MYBATIS3);

This is not guaranteed to work in all databases, but it does work in most.

Other than this method, your best option is to use a MyBatis result map.

SqlColumn already has a utility method asCamelCase that might help...

select(id.asCamelCase())
    .from(foo)
    .build()
    .render(RenderingStrategies.MYBATIS3);

This is not guaranteed to work in all databases, but it does work in most.

Other than this method, your best option is to use a MyBatis result map.

This is just a competition for a single column. There are no methods that compete for all columns. Anyway, thank you very much
I have used the CaseInsensiveMap class to solve the lowercase problem