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?
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With