0

I have a select SQL in MyBatis like this:

SELECT * FROM MYTABLE WHERE id IN (#{ids})

The parameter ids I passed in is correct, which I can get while I'm debugging. And also, I can run this SQL in MySQL with the parameter correctly and get the results.

But nothing is gotten running with Java code.

I printed the SQL of MyBatis and the SQL is like this:

SELECT * FROM MYTABLE WHERE id IN (?)

and the parameters shown in log is correct too('1', '2', '3').

Why can't I get the data by code?

I'd be appreciated if anyone can help.

Sky
  • 7,343
  • 8
  • 31
  • 42

1 Answers1

0

You need to use <foreach> as described in the documentation.

See this other SO question if you are using annotations.

Community
  • 1
  • 1
Bogdan
  • 23,890
  • 3
  • 69
  • 61
  • Thanks, Bogdan. This works. Is this the only way doing this? Can't I just pass in the parameter with `String`? – Sky May 17 '16 at 01:27
  • @Sky: Your SQL with a String just ends up like this: `WHERE id IN (?)`. What you actually want is something like this: `WHERE id IN (?, ?, ... ,?, ?)` which you get by iterating the list. – Bogdan May 17 '16 at 19:40
  • Yes, but I have the correct format for it, the parameter string is '1', '2', '3', exactly the one the `IN` parses. – Sky May 18 '16 at 02:37