MyBatis - lists of primitive types

It seems to have eluded me. I know that I can use a map to return a set of vanilla strings from myBatis request, but how to do this with a list of primitive types?

eg. If I had SQL:

select product_price from products 

Is resultMap required for this? I tried to use java.util.ArrayList as the result type, but get a class of not found errors.

In the same vein, how to pass a list of elements as an argument to a query.

Any input, pointers to documents are appreciated.

+4
source share
4 answers

Just declare resultType as the primitive type you want, which in your case is 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 return a Long list.

 List<Long> getPrice(); 
+6
source

try using resultMap

 <resultMap type="java.lang.Long" id="domainsResult"> <result property="" column="product_price"/> </resultMap> <select id="getPrice" resultMap="domainsResult"> select product_price from products </select> 

This will give you a list of priceList.

+1
source

Change this:

You will get it as a list of cards if you write like this:

 <select id="getPrice" resultType="Hashmap"> select product_price from products </select> 

If the key will have a column name. Each card will contain one entry. If you need an ArrayList, you can write a function to convert this map to an ArrayList:

 public List listFromListOfMap (List<Map> listOfMap ) { List<Integer> prices = new ArrayLisyt<Integer>(); for(int i = 0; i < listOfMap.size(); i++) { Integer value = (Integer)listOfMap.get(i).get("product_price"); prices.add(value); } return prices; } 

Hope this helps :)

0
source

Try using the code snippet below inside your final map to map product_price columns -

  <collection property="price" ofType="java.lang.Long"> <result property="price" column="product_price"/> </collection> 
0
source

Source: https://habr.com/ru/post/1395265/


All Articles