MyBatis在多个返回列的POJO对象中填充列表<! - ? - >字段

时间:2017-11-28 10:56:10

标签: java sql list mapping mybatis

我正在使用MyBatis,并希望根据多个返回的列填充生成的POJO对象中的List字段。

例如,我生成的POJO如下所示:

public class Garage {
    public List<Car> cars
}

Car实例定义:

public class Car {
    public String name;
    public Integer year;
}

因此在我的示例数据库上运行SQL语句:

SELECT
    sc.name AS "speedy_car_name"
    sc.year AS "speedy_car_year"
    bc.name AS "default_car_name"
    bc.year AS "default_car_year"
FROM sport_cars sc
    JOIN basic_cars bc ON sc.year = bc.year
WHERE year = 2017;

结果会给我:

porsche, 2017, prius, 2017

是否可以根据SQL语句返回的多列来即时添加List对象中的条目?例如,像这样(只是一个不工作映射的例子):

<select id="getMyCars" resultMap="garageTypeMap"
SELECT
    sc.name AS "speedy_car_name"
    sc.year AS "speedy_car_year"
    bc.name AS "default_car_name"
    bc.year AS "default_car_year"
FROM sport_cars sc
    JOIN basic_cars bc ON sc.year = bc.year
WHERE year = 2017;
</select>

<resultMap id="garageTypeMap" type="Garage">
  <collection property="cars" ofType="Car">
    <collection property="car">
        <result property="name" column="speedy_car_name"/>
        <result property="year" column="speedy_car_year"/>
    </collection>
    <collection property="car">
        <result property="name" column="default_car_name"/>
        <result property="year" column="default_car_year"/>
    </collection>
</resultMap>

我试图在这里找到任何关于它的信息:http://www.mybatis.org/mybatis-3/sqlmap-xml.html(收藏和关联),但无法找到我的问题的答案。可能吗?或者我需要稍微重构一下我的课程? (无法触及数据库结构)。

0 个答案:

没有答案