MyBatis - 原始类型列表

时间:2012-02-07 16:25:38

标签: mybatis

这似乎让我望而却步。我知道我可以使用地图从myBatis查询中返回一组vanilla,但是如何使用 list 的基本类型?

e.g。如果我有SQL那样:

select product_price from products

这需要resultMap吗?我试图使用java.util.ArrayList作为结果类型但是找不到类错误。

与此类似,如何将项列表作为参数传递给查询。

任何输入,指向文档的指针都表示赞赏。

4 个答案:

答案 0 :(得分:8)

只需将resultType声明为您想要的基本类型,在您的情况下为Long。它将作为列表返回。

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

在mapper界面中,您应该返回Long列表。

List<Long> getPrice();

答案 1 :(得分:1)

尝试使用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> 

它会给你一个List priceList。

答案 2 :(得分:0)

Trey this:

如果你这样写,你会得到它作为地图列表:

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

其中键将是列名。每个地图都包含一个条目。如果您想要一个ArrayList,那么您可以编写一个函数将此映射转换为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;
}

希望这会有所帮助:)

答案 3 :(得分:0)

尝试在resultmap中使用以下代码段进行product_price列映射 -

  <collection property="price" ofType="java.lang.Long">
              <result property="price" column="product_price"/> 
  </collection>
相关问题