MyBatis where in Enum

Viewed 30

I'm using Java spring and MyBatis. I have an Enum like this

public enum MyEnum {
   A,B,C,D,E
}

now I want to write a query and in my query I want to have this :

SELECT * FROM table where myColumn in ("A", "B", "C", "D", "E")

How can I achieve this? I tried converting it to array of strings in my java code and pass it as parameter to my query, and I created this query :

<foreach item="item" index="index" collection="#{searchParams.stringValues}" open="AND table.myColumn IN ('" separator="','" close="')">
    #{item}
</foreach>

and I receive following error :

Try setting a different JdbcType for this parameter or a different configuration property. Cause: org.postgresql.util.PSQLException: The column index is out of range: 11, number of columns: 10.\"

BTW, I'm using xml files for my queries. I've checked this answer : Mybatis foreach iteration over list of integers within a complex object parameter which appears to work for list of integers but apparently not for list of strings or enums.

1 Answers

First comment solved my issue, so I should replace this:

<foreach item="item" index="index" collection="#{searchParams.stringValues}" open="AND table.myColumn IN ('" separator="','" close="')">
    #{item}
</foreach>

with this:

<foreach item="item" index="index" collection="searchParams.stringValues" open="AND table.myColumn IN (" separator="," close=")">
    #{item}
</foreach>

Related