Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MyBatis - lists of primitive types

Tags:

mybatis

This seems to have eluded me. I know I can use a map to return a vanilla set of rows from a myBatis query but how to do it with a list of primitive types?

e.g. If I had SQL like:

select product_price from products

Does this require a resultMap? I've tried to use java.util.ArrayList as the result type but get class not found errors.

In a similar vein, how do I pass a list of items as an argument to a query.

Any input, pointers to docs appreciated.

like image 699
Joel Avatar asked Feb 07 '12 16:02

Joel


People also ask

What is the difference between iBATIS and MyBatis?

MyBatis is a fork from iBATIS, and according to Wikipedia most of iBATIS' developers moved over to MyBatis too. The iBATIS project is currently marked as Inactive, therefore you should go with MyBatis for new projects and only use iBATIS if you're maintaining an existing project which already uses iBATIS.

What is resultMap 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.

What is TypeHandler in MyBatis?

typeHandlers. Whenever MyBatis sets a parameter on a PreparedStatement or retrieves a value from a ResultSet, a TypeHandler is used to retrieve the value in a means appropriate to the Java type.

What is resultMap?

A Result Map lets you control how data is extracted from the result of a query, and how the columns are mapped to object properties. A Result Map can describe the column type, a null value replacement, and complex property mappings including Collections.


2 Answers

Try using below code snippet inside your resultmap for product_price column mapping -

  <collection property="price" ofType="java.lang.Long">
              <result property="price" column="product_price"/> 
  </collection>
like image 57
Sanchi Girotra Avatar answered Oct 26 '22 13:10

Sanchi Girotra


Just declare the resultType as the primitive type that you want, which in your case is a Long. It will be returned as a list.

<select id="getPrice" resultType="java.lang.Long">
  select product_price from products
</select>

In the mapper interface you should expect to get back a list of Long.

List<Long> getPrice();
like image 38
broc.seib Avatar answered Oct 26 '22 13:10

broc.seib