Skip to content
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

Map receive returned incorrect result #2298

Closed
feng199464 opened this issue Jul 23, 2021 · 11 comments
Closed

Map receive returned incorrect result #2298

feng199464 opened this issue Jul 23, 2021 · 11 comments

Comments

@feng199464
Copy link

I called the stored procedure from XML. The result returned by the stored procedure contains special characters. The type received by XML is map. The result I got is incorrect. The "." in the figure below is split

image
image

@harawata
Copy link
Member

Hello @feng199464 ,

In MyBatis, dot is used describe nested objects.
You should use AS in the query to assign an alias.

Closing as this is a duplicate of #13 .

@feng199464
Copy link
Author

你好@feng199464

在 MyBatis 中,dot 用于描述嵌套对象。
您应该AS在查询中使用来分配别名。

关闭,因为这是#13的副本。
I have used the as alias, but the field value will be converted to the field name display. The field name converted from the field value is dynamic, which I can't control

@harawata
Copy link
Member

I think you are now talking about mapping from HashMap to JSON.
It has nothing to do with MyBatis.
Please elaborate if I misunderstood.

@feng199464
Copy link
Author

我认为您现在正在谈论从HashMap到 JSON 的映射。
它与 MyBatis 无关。
如果我误解了,请详细说明。
My problem is that the data format returned from mybatis to HashMap is incorrect

@harawata
Copy link
Member

harawata commented Jul 23, 2021

Okay, then please create a small demo project like these and share it on your repository.
I need it to understand your problem correctly.

@feng199464
Copy link
Author

Okay, then please create a small demo project like these and share it on your repository.
I need it to understand your problem correctly.

CREATE TABLE mybatis_demo (
id int NOT NULL,
name varchar(255) DEFAULT NULL,
age int DEFAULT NULL,
PRIMARY KEY (id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

INSERT INTO mybatis_demo VALUES (1, 'dsad', 13);

https://github.com/feng199464/mybatisdemo.git

image

image

@harawata
Copy link
Member

Ah...you assign an alias that contains a dot. 😅
I cannot think of a good solution, then.
You may have to assign a column alias that does not contain dot in the query and replace the map key afterwards.

@feng199464
Copy link
Author

Okay, then please create a small demo project like these and share it on your repository.
I need it to understand your problem correctly.

CREATE TABLE mybatis_demo (
id int NOT NULL,
name varchar(255) DEFAULT NULL,
age int DEFAULT NULL,
PRIMARY KEY (id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

INSERT INTO mybatis_demo VALUES (1, 'dsad', 13);

https://github.com/feng199464/mybatisdemo.git

image

image

In mybatis, if the field alias has a decimal point, it cannot be resolved if it is returned to the map, but my current business needs to dynamically convert the field row value into field column display, for example:


ID | subject | score | name |

1 | english | 90 | lucy |

2 | math | 90 | lucy |

3 | math | 100 | jack |

4 | chinese | 85 | tom |

                         ↓
                         ↓

english | math | chinese | name

90 | 90 | 0 | lucy

0 | 100 | 0 | jack

0 | 0 | 85 | tom

I drew a watch

The above figure shows a row of scores for each user. Now I will dynamically convert the account name value into field display

The following figure shows the converted data

I can't restrict users from adding special characters "."

@feng199464
Copy link
Author

Ah...you assign an alias that contains a dot. 😅
I cannot think of a good solution, then.
You may have to assign a column alias that does not contain dot in the query and replace the map key afterwards.

When users add data, they use the date as the name of the course. For example: 7.23 English class

@harawata
Copy link
Member

If you have no control over column aliases, there may be something wrong with your project's design.
You should check if the query is vulnerable to SQL injection.

Anyway, MyBatis is working as designed.

harawata added a commit to harawata/mybatis-3 that referenced this issue Jan 24, 2024
It's now  possible to write a custom Map wrapper that allows keys that contain period (dot) `.` or brackets `[]`.
Also, it's now possible to write a custom Map wrapper by extending the built-in `MapWrapper`.

Should fix mybatis#13 mybatis#2298 mybatis#3062
@harawata
Copy link
Member

Duplicate of #13

@harawata harawata marked this as a duplicate of #13 Jan 28, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants