Skip to content

mybatis can not parse #{list[0][0]} correctly #2103

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

Open
daodefengshang opened this issue Nov 10, 2020 · 8 comments · May be fixed by #2136
Open

mybatis can not parse #{list[0][0]} correctly #2103

daodefengshang opened this issue Nov 10, 2020 · 8 comments · May be fixed by #2136

Comments

@daodefengshang
Copy link

MyBatis version

3.4.4

Database vendor and version

All

Test case or example project

CREATE TABLE
TABLE_DEMO
(
ID NUMBER(6) NOT NULL,
NAME VARCHAR2(24) NOT NULL,
PRIMARY KEY (ID)
);
INSERT INTO TABLE_DEMO (ID, NAME) VALUES (1, '001');

List<Map<String, Object>> query(@param("list") List<List> list);

select id, name from table_demo where id = #{list[0][0], jdbcType=NUMERIC}

Steps to reproduce

public List<Map<String, Object>> query() {
List<List> lists = new ArrayList<>();
List li = new ArrayList<>();
li.add(1);
lists.add(li);
List<Map<String, Object>> list = mapper.query(lists);
return list;
}

Expected result

result: [{id: 1, name: "001"}]

Actual result

[org.springframework.web.servlet.handler.AbstractHandlerExceptionResolver.logException(AbstractHandlerExceptionResolver.java:186)]Handler execution resulted in exception: org.mybatis.spring.MyBatisSystemException: nested exception is org.apache.ibatis.exceptions.PersistenceException:
###Error querying database. Cause: java.lang.NumberFormatException: For input string: "0][0"
###Cause: java.lang.NumberFormatException: For input string: "0][0"

@hugbgithub
Copy link

Hi daodefengshang , this should be a bug . But I can't understand why you write the condition statement like that.

@daodefengshang
Copy link
Author

Sorry, I did not find the foreach method for @SelectProvider like the tag in xml. I found it when I wrote the nested foreach tool.

@juzi214032
Copy link
Contributor

I will try to fix this bug.

@leyewen
Copy link

leyewen commented Dec 25, 2020

I got the same error when upgrade project to jdk-11 and mybatis-3.5.6, It looks like the parser is not correctly recognized.
This bug has seriously affected the operation of project!

params.value[0][0]
org.apache.ibatis.exceptions.PersistenceException: 
### Error querying database.  Cause: java.lang.NumberFormatException: For input string: "0][0"
### Cause: java.lang.NumberFormatException: For input string: "0][0"
	at org.apache.ibatis.exceptions.ExceptionFactory.wrapException(ExceptionFactory.java:30)
	at org.apache.ibatis.session.defaults.DefaultSqlSession.selectList(DefaultSqlSession.java:149)
	at org.apache.ibatis.session.defaults.DefaultSqlSession.selectList(DefaultSqlSession.java:140)
	at org.apache.ibatis.session.defaults.DefaultSqlSession.selectOne(DefaultSqlSession.java:76)

@juzi214032
Copy link
Contributor

@leyewen #2136 This pr solves the problem, but no one has reviewed it yet

@harawata
Copy link
Member

harawata commented Dec 29, 2020

@leyewen ,
AFAIK, an expression like list[0][0] never worked in any version of MyBatis.
It's simply not supported.

Like @hugbgithub , I'm also wondering why you guys need to write list[0][0].
In general, the recommended way to iterate nested list is to use nested <foreach /> and it should be possible to use <foreach /> in a provided SQL since version 3.5.1 (see #1391 ).

@juzi214032
Copy link
Contributor

@harawata
I think expression like list[0][0] should be supported. Why have previous versions not been supported?

@leyewen
Copy link

leyewen commented Dec 31, 2020

@harawata Your right, I retest it in list of older version, results pay the same error For input string: "0][0".
And I make another test:

int value[][] = { { 1, 2, 3, 4 } };
String sql = "SELECT * FROM `user` WHERE `id`=#{id} AND `value`=#{value.[0].[0]} LIMIT 0,1;";
Map<String, Object> parameter = new HashMap<String, Object>();
parameter.put("id", 4);
parameter.put("value", value);

by using:

sqlSourceFactory.createSqlSource(sql, parameterType);

#{value.[0].[0]} will work for Two-dimensional array but not for List.

org.apache.ibatis.exceptions.PersistenceException: 
### Error querying database.  Cause: java.lang.UnsupportedOperationException
### Cause: java.lang.UnsupportedOperationException
	at org.apache.ibatis.exceptions.ExceptionFactory.wrapException(ExceptionFactory.java:30)
	at org.apache.ibatis.session.defaults.DefaultSqlSession.selectList(DefaultSqlSession.java:149)

Some time, we need to write params.value[0][0] or list[0][0] for Iterable value Object when runing through SqlSource but not through XML construction.

update
I further test again:

List<Integer> ids = new ArrayList<Integer>();
ids.add(1);
ids.add(2);
List<List<Integer>> list2 = new ArrayList<List<Integer>>();
list2.add(ids);
List<List<List<Integer>>> list = new ArrayList<List<List<Integer>>>();
list.add(list2);
String sql = "SELECT * FROM `user` WHERE `id`=#{id} AND `name`=${list[0][0][0]} LIMIT 0,1;";
Map<String, Object> parameter = new HashMap<String, Object>();
parameter.put("id", 4);
parameter.put("list", list);

${list[0][0][0]} will work, but #{list[0][0][0]} does not work.

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

Successfully merging a pull request may close this issue.

5 participants