weixin_42347354 2009-09-10 00:07
浏览 370
已采纳

Ibatis一对多,返回结果为resultMap可以取出多,为resultClass时取不出多。能在resultClass时也取出多么?

我的配置文件:
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE sqlMap PUBLIC "-//ibatis.apache.org//DTD SQL Map 2.0//EN" "http://ibatis.apache.org/dtd/sql-map-2.dtd" >
<sqlMap namespace="tablenewsandkind">
 <typeAlias alias="tablenews" type="com.zzxy.po.TableNews" />
 <typeAlias alias="tablenewskind" type="com.zzxy.po.TableNewsKind" />
 <resultMap class="tablenews" id="TableNewsResult">
  <result column="ID" jdbcType="DECIMAL" property="id" />
  <result column="KIND_ID" property="kind"
   select="tablenewsandkind.getKindById" />
  <result column="TITLE" jdbcType="VARCHAR" property="title" />
  <result column="CONTENT" jdbcType="VARCHAR" property="content" />
  <result column="ITIME" jdbcType="VARCHAR" property="itime" />
  <result column="IFROM" jdbcType="VARCHAR" property="ifrom" />
  <result column="ADDRESS" jdbcType="VARCHAR" property="address" />
  <result column="IMG" jdbcType="VARCHAR" property="img" />
  <result column="ISSTATIC" jdbcType="VARCHAR"
   property="isstatic" />
  <result column="ISPASS" jdbcType="VARCHAR" property="ispass" />
 </resultMap>
 <resultMap class="tablenewskind" id="TableNewsKindResult">
  <result column="ID" jdbcType="DECIMAL" property="id" />
  <result column="NAME" jdbcType="VARCHAR" property="name" />
  <result column="DESP" jdbcType="VARCHAR" property="desp" />
  <result column="ID" property="news"
   select="tablenewsandkind.getNewsByKindId" />
 </resultMap>
 <select id="getAllNews" resultMap="TableNewsResult">
  <![CDATA[
select ID, KIND_ID, TITLE, CONTENT, ITIME, IFROM, ADDRESS, IMG, ISSTATIC, ISPASS from TABLE_NEWS
]]>
 </select>
 <select id="getKindById" resultClass="tablenewskind">
  <![CDATA[
select ID, NAME, DESP from TABLE_NEWS_KIND where ID = #value#
]]>
 </select>
 <select id="getAllKind" resultMap="TableNewsKindResult">
  <![CDATA[
select ID, NAME, DESP from TABLE_NEWS_KIND
]]>
 </select>
 <select id="getNewsByKindId" resultMap="TableNewsResult">
  <![CDATA[
select ID, KIND_ID, TITLE, CONTENT, ITIME, IFROM, ADDRESS, IMG, ISSTATIC, ISPASS from TABLE_NEWS where KIND_ID = #value#
]]>
 </select>
</sqlMap>

 

情况是这样的:

我用上面的id=getKindById查询时取不出来news(news为List类型),代码如下:

<select id="getKindById" resultClass="tablenewskind">
<![CDATA[
select ID, NAME, DESP from TABLE_NEWS_KIND where ID = #value#
]]>

但是我把这部分换成如下:

<select id="getKindById" resultMap="TableNewsKindResult">
<![CDATA[
select ID, NAME, DESP from TABLE_NEWS_KIND where ID = #value#
]]>
</select>

可以取出来news,也就是说,当返回结果为resultMap时可以取出来news。

这个问题是怎么回事,请各位不吝赐教。

 

  • 写回答

1条回答

  • 阿取 2009-09-10 12:49
    关注

    resultMap映射时,你指定了主从表的关联关系:

    所以iBATIS知道怎么取出newsKind对应的news

    resultClass映射时,你没有指定主从表的关联关系,自然iBATIS无从知道怎么取出newsKind对应的news

    了解iBATIS没多久,刚翻了下官方指南,好像没给出resultClass映射时取出关联对象的方法

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥88 实在没有想法,需要个思路
  • ¥15 MATLAB报错输入参数太多
  • ¥15 python中合并修改日期相同的CSV文件并按照修改日期的名字命名文件
  • ¥15 有赏,i卡绘世画不出
  • ¥15 如何用stata画出文献中常见的安慰剂检验图
  • ¥15 c语言链表结构体数据插入
  • ¥40 使用MATLAB解答线性代数问题
  • ¥15 COCOS的问题COCOS的问题
  • ¥15 FPGA-SRIO初始化失败
  • ¥15 MapReduce实现倒排索引失败