隨筆 mybatis collection簡單使用

返回實體類中有一個集合,該集合需要映射的時候進行collection處理

public class GoodVo {
    public int id;
    public String name;

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Set<Order> orderList;

    public Set<Order> getOrderList() {
        return orderList;
    }

    public void setOrderList(Set<Order> orderList) {
        this.orderList = orderList;
    }
}

mapper.xml部分文件:

就算字段和數據庫一致,也需要resultMap去挨個映射一遍,然後集合用collection對應。因爲返回的是一個對象,所以當返回是多個對象就會出錯,如Expected one result (or null) to be returned by selectOne(), but found: 2

 <resultMap type="mptest.mybatistest.entity.GoodVo" id="userMap" >
        <result column="id" property="id" />
        <result column="name" property="name" />
        <collection property="orderList" ofType="mptest.mybatistest.entity.Order">
            <result column="orderId" property="orderId" />
            <result column="orderFee" property="orderFee" />
        </collection>
    </resultMap>

    <select id="getGoodsList" resultMap="userMap" >
        select u.id,
        u.name,
        o.orderId,
        o.orderFee
        FROM
        user_test u left join  order_id_test  o on u.id=o.id
        where u.id = 1
    </select>

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章