Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Select List of Integers as Collection inside another result Map in Mybatis

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?

like image 591
vaibhavvc1092 Avatar asked Jan 12 '16 11:01

vaibhavvc1092


People also ask

What is result map in MyBatis?

The resultMap element is the most important and powerful element in MyBatis. It's what allows you to do away with 90% of the code that JDBC requires to retrieve data from ResultSet s, and in some cases allows you to do things that JDBC does not even support.


Video Answer


1 Answers

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.

like image 192
Dariusz Avatar answered Nov 03 '22 02:11

Dariusz