在Mybatis中的另一个结果Map中选择整数列表作为集合

vai*_*092 5 collections mybatis

我想在Mybatis的Result Map中选择Collection of Integers作为Collection.我无法找到解决这个问题的方法.

结果类是

class Mapping {
private String name;
private List<Integer> ids;
}
Run Code Online (Sandbox Code Playgroud)

Mybatis如下:

<resultMap id="mapping" type="some.package.Mapping">
        <result property="name" column="name"/>
        <collection property="ids" column="id" javaType="java.util.List" ofType="java.lang.Integer" />
</resultMap>

<select id="getMapping" resultMap="mapping">
        SELECT name, id
        FROM mapping
    </select>
Run Code Online (Sandbox Code Playgroud)

这段代码不适合我.我错过了什么?

Dar*_*usz 9

To get a list of Integers in a single resultMap you can use:

<id property="name" column="name"/>
<collection property="ids" ofType="Integer">
    <result column="id"/>
</collection>
Run Code Online (Sandbox Code Playgroud)

A nested select would also work, but it will execute N+1 queries, which may be a performance issue.


Per*_*sia 0

mybatis不知道如何选择id作为id列表。您可以使用嵌套选择

<collection property="ids" column="name" select="findIdByName" />

<select id="findIdByName" resultType="int">
        SELECT id
        FROM mapping where name = #{name}
    </select>
Run Code Online (Sandbox Code Playgroud)