8

I want to select Collection of Integers as Collection inside Result Map in Mybatis. I am unable to figure out a way for this.

Result class is

class Mapping {
private String name;
private List<Integer> ids;
}

Mybatis is as follows:

<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>

This code in not working out for me. What am I missing?

vaibhavvc1092
  • 2,767
  • 3
  • 17
  • 24

2 Answers2

10

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>

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

Dariusz
  • 20,650
  • 8
  • 71
  • 109
0

mybatis do not know how to select id as id list. you can use nested select

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

<select id="findIdByName" resultType="int">
        SELECT id
        FROM mapping where name = #{name}
    </select>
Persia
  • 795
  • 4
  • 8