Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

sql

mybatis

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

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

<!DOCTYPE mapper
        PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">

<mapper namespace="db">

    <select id="selectFlag" resultType="java.lang.Boolean" parameterType="java.util.ArrayList">
        select TOP 1 'true' from customers where id = ???
    </select>
</mapper>
like image 466
panagdu Avatar asked Mar 19 '23 05:03

panagdu


1 Answers

If your query uses one id, why are you sending a list and then picking the first one for the query?

Change your parameterType to an int (or whatever type the id has), don't send a list. This is a simple approach that also conveys the information that you are only retrieving data based on a single id. Your condition then changes to something like:

where id = #{id}

If you must absolutely send an ArrayList (for whatever reason) then MyBatis supports OGNL expressions so something like this should work:

where id = #{list[0]}

Unfortunately MyBatis lacks on the documentation side, so you can find out about things like this only by looking at the source code.

like image 132
Bogdan Avatar answered Mar 20 '23 18:03

Bogdan