Mybatis: How can I get the first item from the passed parameter?

Given that my parameterType is an ArrayList, is it possible to get the first element from this list and use it in the where clause?

<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE mapper
        PUBLIC "-// Mapper 3.0//EN"

<mapper namespace="db">

    <select id="selectFlag" resultType="java.lang.Boolean" parameterType="java.util.ArrayList">
        select TOP 1 'true' from customers where id = ???



source to share

1 answer

If your request uses a single identifier, why are you submitting a list and then selecting the first one to request?

Change parameterType

to int

(or whatever type it is), don't post the list. This is a simple approach that also communicates that you are only fetching data based on a single identifier. Your state will then change to something like:

where id = #{id}


If you must absolutely submit ArrayList

(for whatever reason), MyBatis supports OGNL expressions, so something like this should work:

where id = #{list[0]}


Unfortunately, MyBatis is lacking on the documentation side, so you can only find out about such things by looking at the source code.



All Articles