2012-02-07 76 views
4

这似乎没有我。我知道我可以使用map来从myBatis查询中返回一组vanilla行,但如何使用原始类型的列表来做到这一点?MyBatis - 原始类型列表

例如如果我有SQL:

select product_price from products 

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

以类似的方式,我如何将项目列表作为参数传递给查询。

任何输入,赞赏文档的指针。

回答

0

三分球这样的:

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

<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; 
} 

希望这有助于:)

+0

谢谢。原来,如果你简单地将resultType设置为Class def,它将给出这些对象的列表(如果你有自定义类)。我想知道原始类型,如果我只是将resultType设置为java.lang.Integer它会解决。我必须尝试一下。感谢上面的代码。 – Joel 2012-02-16 23:55:11

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。

+0

我在resultmap中的元素中使用了空白属性技巧,它解决了我的问题(与帖子不同)。非常感谢! – realjin 2014-12-10 11:28:45

+0

请标记任何合适的答案并关闭此问题。它也会帮助其他人也有类似的问题。 – 2015-10-05 04:21:25

6

只要声明resultType作为你想要的原始类型,在你的情况下是Long。它将作为列表返回。

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

在映射器界面中,您应该预计会得到一个Long的列表。

List<Long> getPrice(); 
0

尝试使用下面的代码片段的结果映射内为PRODUCT_PRICE列映射 -

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