简体   繁体   中英

How to select a java.util.List of objects using a mybatis resultMap association and select

I have this almost working.

<mapper namespace="com.xyz.rb.dao.RBCellDao">
    <resultMap id="rbCellResultMap" type="com.xyz.rb.model.RBCell">
        <id property="id" column="id" />
        <result property="fieldIds" column="fieldIds" typeHandler="com.xyz.dao.typehandlers.CommaSeparatedLongListTypeHandler" />
        <association property="fields" column="fieldIds" javaType="java.util.List" select="selectFields"></association>
    </resultMap>

    <select id="selectFields" parameterType="String" resultType="com.xyz.abc.model.Field">
        SELECT * FROM fields WHERE id IN (#{value}) <!-- this appears to get the correct value ====> Parameters: 1,2,3(String) -->
    </select>
</mapper>

RBCell Table data looks like:

| ID | FIELDIDS      |  
|  1 | 1,2,3         | 
|  2 | 45,54321,9,78 |

This is returning a List as expect, HOWEVER, there is only ever one entry in the List no matter what value is in the FieldIds column...

What am I missing here? How can I get this to return the entire list of Field instead of what appears to be only the first one?

Between main query and associated query, the parameter is string exactly. So use parameter in associated query just like this:

SELECT * FROM fields WHERE id IN ('1,2,3');

if you want do the in thing, assumed you used mysql, you can try find_in_set :

SELECT * FROM fields WHERE FIND_IN_SET(id, #{value});

Else, you can try like to do this.

This ended up being simply to change:

SELECT * FROM fields WHERE id IN (#{value}) to SELECT * FROM fields WHERE id IN (${value})

It works now.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM