oracle 使用 MyBatis 返回 Object 内的 Object 列表

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/31220726/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-19 02:56:22  来源:igfitidea点击:

Return list of Object inside Object with MyBatis

javasqlspringoraclemybatis

提问by Yuriy Kravets

I encountered problems when returning a list of Objects inside another Object when using MyBatis. My main object looks like this:

我在使用 MyBatis 时在另一个对象中返回对象列表时遇到问题。我的主要对象是这样的:

private Long id;

private String symbol;

private List<TypePermission> typePermissions;

and my mapper looks like this

我的映射器看起来像这样

<resultMap type="CalendarType" id="calendarTypeMap">
    <result column="id" property="id"/>
    <result column="symbol" property="symbol"/>
    <collection property="TypePermissions" resultMap="TypePermissions"/>
</resultMap>

<resultMap id="TypePermissions" type="TypePermission">
    <result property="roleId" column="roleId"/>
    <result property="permissionSymbol" column="permissionSymbol"/>
</resultMap>

My goal is to get an object like this:

我的目标是获得这样的对象:

content:[
    "id":id,
    "symbol":symbol,
    "TypePermissions":{
        "roleId":roleId,
        "permissionSymbol":permissionSymbol
    }
]

When I execute the sql query I get the following an error cannot find symbol TypePermissions, because the main SELECT tries to select rows such as TYPEPERMISSIONS, ID, SYMBOL

当我执行 sql 查询时,我收到以下错误cannot find symbol TypePermissions,因为主 SELECT 尝试选择诸如 TYPEPERMISSIONS、ID、SYMBOL 之类的行

I searched over the internet, but failed to find anything useful. Could you help me and point out what am I doing wrong?

我在互联网上搜索,但没有找到任何有用的东西。你能帮我指出我做错了什么吗?

回答by PhoenixYip

Please post your select snippet, I think this will ok:

请发布您的选择片段,我认为这可以:

<select id="selectCalendarType" parameterType="int" resultMap="calendarTypeMap">
    SELECT c.id,
    c.symbol
    t.roleId,
    t.permissionSymbol
    FROM CalendarType c
    LEFT JOIN TypePermission t ON c.id = t.c_id
    WHERE c.id = #{id}
</select>

And I think what you will get is actully something like this:

我认为你会得到的实际上是这样的:

content:{
  "id":id,
  "symbol":symbol,
  "TypePermissions":[{
    "roleId":roleId,
    "permissionSymbol":permissionSymbol
  }]
}

And more about this you can read this example Nested_Results_for_Collection

更多关于这个你可以阅读这个例子Nested_Results_for_Collection